Javascript :)
function StartWorkflow() {
var errorMessage = "An error occured when starting the workflow.";
var subscriptionId = "", itemId = "", redirectUrl = "";
var urlParams = GetUrlParams();
if (urlParams) {
itemId = urlParams["ID"];
redirectUrl = urlParams["Source"];
subscriptionId = urlParams["TemplateID"];
}
if (subscriptionId == null || subscriptionId == "") {
alert(errorMessage + " Could not find the workflow subscription id.");
RedirFromInitForm(redirectUrl);
}
else {
var wfParams = new Object();
var html = $("#ctl00_PlaceHolderMain_docReviewerUser_upLevelDiv");
wfParams['DocReviewerLoginName'] = $("#divEntityData", html).attr("key");
var html = $("#ctl00_PlaceHolderMain_docEditorUser_upLevelDiv");
wfParams['DocEditorLoginName'] = $("#divEntityData", html).attr("key");
var context = SP.ClientContext.get_current();
var wfManager = SP.WorkflowServices.WorkflowServicesManager.newObject(context, context.get_web());
var wfDeployService = wfManager.getWorkflowDeploymentService();
var subscriptionService = wfManager.getWorkflowSubscriptionService();
context.load(subscriptionService);
context.executeQueryAsync(
function (sender, args) {
var subscription = null;
if (subscriptionId)
subscription = subscriptionService.getSubscription(subscriptionId);
if (subscription) {
if (itemId != null && itemId != "") {
wfManager.getWorkflowInstanceService().startWorkflowOnListItem(subscription, itemId, wfParams);
}
else {
wfManager.getWorkflowInstanceService().startWorkflow(subscription, wfParams);
}
context.executeQueryAsync(
function (sender, args) {
RedirFromInitForm(redirectUrl);
},
function (sender, args) {
alert(errorMessage + " " + args.get_message());
RedirFromInitForm(redirectUrl);
}
)
}
else {
alert(errorMessage + " Could not load the workflow subscription.");
RedirFromInitForm(redirectUrl);
}
},
function (sender, args) {
alert(errorMessage + " " + args.get_message());
RedirFromInitForm(redirectUrl);
}
)
}
}
function RedirFromInitForm(redirectUrl) {
window.location = redirectUrl;
}
function GetUrlParams() {
var urlParams = null;
if (urlParams == null) {
urlParams = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
urlParams[key] = decodeURIComponent(value);
});
}
return urlParams;
}
C#
var WSM = new WorkflowServicesManager(web);
var wfSubscriptionService = WSM .GetWorkflowSubscriptionService();
//get all workflows associated with the list
var subscriptions = wfSubscriptionService .EnumerateSubscriptionsByList(listId);
//run all workflows associated with the list
foreach (var workflowSubscription in subscriptions)
{
//initiation parameters
var inputParameters = new Dictionary();
inputParameters.Add("WFProperty", "Value");
WSM .GetWorkflowInstanceService().StartWorkflowOnListItem(workflowSubscription, itemId, inputParameters);
}
Reference :
http://code.msdn.microsoft.com/office/SharePoint-2013-Approval-f5ac5eb2
1 comments:
Hi,
If I try to get an instance of WorkflowServiceManager() in a console application in an onpremise sharepoint infra. It throws an error -- cannot invoke method or retrieve property of null object. Object returned by following call stack is null.
The same csom code works fine with sharepoint online infra.
Do you have any idea on how to resolve this?
Post a Comment