I noticed that we cannot call a 2013 designer WF from within a 2013 designer WF. Can a workflow be called using rest api. If so are there any examples on how to do it?
1 Answer
You can also call the workflow from code. I have a custom .aspx and in there I called a 2013 WF via a button.
Here how I start the workflow:
$("#anmelden"+id).click(function(e) {
startWorkflow(id);
e.preventDefault();
});
And here the function:
function startWorkflow(itemID) {
//alert("go for it");
var subID = "5a7f927a-3137-47e8-85d7-39f03f85521d";
var context = SP.ClientContext.get_current();
var web = context.get_web();
// Get Current User
var loginName = "";
var userid = _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
headers : requestHeaders,
success : onSuccessA,
error : onErrorA
});
function onSuccessA(data, request){
var loginName = data.d.Title;
//alert("Currently logged in: " + loginName);
if (loginName != ""){
var wfServiceManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
var subscription = wfServiceManager.getWorkflowSubscriptionService().getSubscription(subID);
context.load(subscription);
context.executeQueryAsync(
function(sender, args){
console.log("Subscription load success. Attempting to start workflow.");
var inputParameters = {};
wfServiceManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemID, inputParameters);
context.executeQueryAsync(
function(sender, args){
console.log("Successfully starting workflow.");
location.reload();
},
function(sender, args){
console.log("Failed to start workflow.");
console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
}
);
},
function(sender,args){
console.log("Failed to load subscription.");
console.log("Error: " + args.get_message() + "\n" + args.get_stackTrace());
}
);
}
else{
alert("Error");
}
}
function onErrorA(error) {
alert(error);
}
}
The variable subID
is the ID of your workflow. You can get it if you go to your published workflows and copy the link of the workflow button.
If it is a SharePoint 2013 Workflow you should get a URL similar to this:
javascript:StartWorkflow4('5a7f927a-3137-47e8-85d7-39f03f85521d', '15', '{74AC618F-3714-43B8-90AB-349EA4F6D8DB}')
As you see the first part starting with 5a7... is the ID you need.
Hope this helps to get you started.
-
:) Thank you @Patrick. This is good to know, but I am looking for a SP designer solution. Thanks again.– AamirCommented Mar 23, 2016 at 17:27