问题
Is there a way to call an action via javascript without the use of third party scripts?
I found this https://github.com/PaulNieuwelaar/processjs
However, I cannot use third party libraries.
UPDATE:
Here is some sample code that demonstrates an asynchronous call to an action via JavaScript. A important point to remember is to make the last parameter of the open method of the request to true.
req.open(consts.method.post, oDataEndPoint, true);
// plugin
public class RunAsync : CodeActivity
{
[Input("input")]
public InArgument<string> Input { get; set; }
[Output("output")]
public OutArgument<string> Output { get; set; }
protected override void Execute(CodeActivityContext executionContext)
{
try
{
Thread.Sleep(20000);
Output.Set(executionContext, $"Result:{Input.Get(executionContext)}");
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
// javascript
function callAction(actionName, actionParams, callback) {
var result = null;
var oDataEndPoint = encodeURI(window.Xrm.Page.context.getClientUrl() + consts.queryStandard + actionName);
var req = new XMLHttpRequest();
req.open(consts.method.post, oDataEndPoint, true);
req.setRequestHeader(consts.odataHeader.accept, consts.odataHeader.applicationJson);
req.setRequestHeader(consts.odataHeader.contentType, consts.odataHeader.applicationJson + ";" + consts.odataHeader.charset_utf8);
req.setRequestHeader(consts.odataHeader.odataMaxVersion, consts.odataHeader.version);
req.setRequestHeader(consts.odataHeader.odataVersion, consts.odataHeader.version);
req.onreadystatechange = function () {
if (req.readyState === 4) {
req.onreadystatechange = null;
if (req.status === 200) {
if (callback) {
result = JSON.parse(this.response);
callback(result);
}
} else {
console.log(JSON.parse(this.response).error);
}
}
};
req.send(JSON.stringify(actionParams));
}
function onLoad() {
console.log('call action...');
var actionParams = {
Input: 'test1234'
};
callAction('TestAsyncAction',actionParams, function(data){
console.log('action callback triggered...');
console.log(JSON.stringify(data));
});
console.log('action called...');
}
// Action
回答1:
You can use webapi to execute custom Action. This is wrapped in XMLHttpRequest
& can be called asynchronous.
/api/data/v8.2/Action_Name
For asynchronous run:
req.open(....., true);
The same using soap call (not recommended).
Processjs
uses Organization.svc/web
which is going to be deprecated.
来源:https://stackoverflow.com/questions/45829566/microsoft-dynamics-crm-365-calling-an-action-via-javascript-asynchronously