How do I make calls to custom actions from PCF ? Any suggestions , please help.
Thanks
Just use ts ignore. It will work.
Hi @skoofy5 ,
For the Xrm.WebApi there are not only the "online" or "offline" modes, but also we find the methods directly in the Xrm.WebApi namespace, which takes care by themselves if the user is online or offline and calls the appropriate method: https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi?WT.mc_id=BA-MVP-5004107#methods
I think this is similar with the PCF webAPI methods. It's true that is not specified in the docs, but it's also not stated that it won't work offline.
I've actually made a test using the WebAPI Sample PCF: https://docs.microsoft.com/en-us/powerapps/developer/component-framework/sample-controls/webapi-control?WT.mc_id=BA-MVP-5004107. It worked in offline mode without problems. Also the Dataset .save() method worked in offline mode too.
In both cases, the changes were sync as soon I've changed to online mode.
@DianaBirkelbach wrote:
I still prefer the context.webApi.execute, since the xhr won't work offline.
Anothe problem is setting up the url for the webAPI xhr request. At least the version from the url will change some time.
Integrated ways are the best.
Integrated is definitely the way to go, but only when they're documented or there has been communication that things are stable enough to start implementing. Unfortunately this is all I've seen, but would be really happy to hear that it's safe/stable now!
Also, you mention execute being used for offline mode, but the client api warns that it's only available for online - is that not the case for the component framework implementation? That would be awesome!
I still prefer the context.webApi.execute, since the xhr won't work offline.
Anothe problem is setting up the url for the webAPI xhr request. At least the version from the url will change some time.
Integrated ways are the best.
I haven't checked it, but I feel like the xhr wouldn't be difficult - something like this:
function callCustomAction(newAreaCode: string, customapiname:string) {
var data = {
"AreaCode": newAreaCode
};
const url = "/api/data/v9.2/" + customapiname;
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function() {
if (this.readyState == 4) {
req.onreadystatechange = null;
if (this.status == 200) {
//callback or processing goes here
}
}
};
req.send(JSON.stringify(data));
}
I am quite new with PCF controls and had a task to call a Custom API from PCF control which is built using React, Typescript and Fluent UI. @ScottDurow is right webAPI (which is pulled from context (ComponentFramework.Context<IInputs>) does have execute method in it.
This method is not part of Typescript but if you debug the control you should be able to see it on the WebAPI.
then I used "//@ts-ignore" to ignore typescript as you can see below. It works like a charm
var apiRequest = new class {
AreaCode = inputText;
getMetadata(): any {
return {
parameterTypes: {
"AreaCode ": {
"typeName": "Edm.String",
"structuralProperty": 1
}
},
operationType: 0,
operationName: "customapiname",
};
}
}();
//@ts-ignore
this.context.webAPI.execute(apiRequest).then(
function success(response: any) {
console.log(response);
console.log(response.json());
},
function (errorResponse: any) {
// Error handling code here
console.log(errorResponse);
}
);
A small improvement of the code above. This example uses casting to enable calling the execute method:
//execute custom api action
/*
<Action Name="sos_customapi_getcontactinfo">
<Parameter Name="ContactID" Type="Edm.Guid" Nullable="false"/>
<ReturnType Type="mscrm.sos_customapi_getcontactinfoResponse" Nullable="false"/>
</Action>
*/
// Execute request
var apiRequest = new class {
ContactID = {
guid: "934fd982-988c-eb11-b1ac-0022489bcf55"
};
getMetadata(): any {
return {
parameterTypes: {
ContactID: {
typeName: "Edm.Guid",
structuralProperty: 1
}
},
operationType: 0,
operationName: "sos_customapi_getcontactinfo"
};
}
}();
var anyWebAPI = <any>this.context.webAPI;
anyWebAPI.execute(apiRequest).then(
function success(response: any) {
console.log(response);
console.log(response.json());
},
function (errorResponse: any) {
// Error handling code here
console.log(errorResponse);
}
);
This is how I implemented PCF call to custom action / Custom API:
//execute custom api action
/*
<Action Name="sos_customapi_getcontactinfo">
<Parameter Name="ContactID" Type="Edm.Guid" Nullable="false"/>
<ReturnType Type="mscrm.sos_customapi_getcontactinfoResponse" Nullable="false"/>
</Action>
*/
// Execute request
var apiRequest = new class {
ContactID = {
guid: "934fd982-988c-eb11-b1ac-0022489bcf55"
};
getMetadata(): any {
return {
parameterTypes: {
ContactID: {
typeName: "Edm.Guid",
structuralProperty: 1
}
},
operationType: 0,
operationName: "sos_customapi_getcontactinfo"
};
}
}();
// <li-user uid="7429" login="TS"></li-user>-ignore
this.context.webAPI.execute(apiRequest).then(
function success(response: any) {
console.log(response);
},
function (errorResponse: any) {
// Error handling code here
console.log(errorResponse);
}
);
HI @Anonymous ,
If you are able to make custom action call from D365 , I would suggest solution by @ben-thompson , I used the same xmlhttp call as I was not sure about Xrm.Webapi being suppported for custom action call.
Thanks
I'm unable to call a custom action following the ms documentation on using custom actions and this thread. The following is my query I append to the url:
const query = `/api/data/v9.0/incidents(${this._entityId})/Microsoft.Dynamics.CRM.my_CustomAction`;
I'm now getting this error:
{"error":{"code":"","message":"No HTTP resource was found that matches the request URI 'https://XXX.crm4.dynamics.com/api/data/v9.0/incidents(1234567890)/Microsoft.Dynamics.CRM.my_CustomAction'."}}
What am I missing?
let response = await fetch(query, {
method: 'post',
headers: headers,
body: body
});
WarrenBelz
87
Most Valuable Professional
mmbr1606
71
Super User 2025 Season 1
Michael E. Gernaey
65
Super User 2025 Season 1