7

I am trying to start share-point workflow using CSOM(Client side object model) by following means of code. But i got the following error from the line mark in comment. Please help me to figure out this error.

Error :- Cannot invoke method or retrieve property from null object. Object returned by the following call stack is null. "GetWorkflowDeploymentService new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager() "

 public bool StartWorkFlow()
 {

   string workflowName = "UpdateDealStatus";

   // connect to the workflow services via a CSOM client context
   var workflowServicesManager = new WorkflowServicesManager(_sharepoint,                  _sharepoint.Web);

  // connect to the deployment service
   var workflowDeploymentService = workflowServicesManager.GetWorkflowDeploymentService();

  // get all installed workflows
  var publishedWorkflowDefinitions =    workflowDeploymentService.EnumerateDefinitions(true);
            _sharepoint.Load(publishedWorkflowDefinitions);

   try
   {
      _sharepoint.ExecuteQuery();//Error thrown from this line
   }
   catch (Exception ex)
   {


   }

   // find the first workflow definition
   var firstWorkflowDefinition = publishedWorkflowDefinitions.First();

   // connect to the subscription service
   var workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService();

   // get all workflow associations
   var workflowAssociations = workflowSubscriptionService.EnumerateSubscriptionsByDefinition(firstWorkflowDefinition.Id);

   _sharepoint.Load(workflowAssociations);
   _sharepoint.ExecuteQuery();

   foreach (var association in workflowAssociations)
   {
       Console.WriteLine("{0} - {1}",
       association.Id, association.Name);
   }
    return true;
}
  • any luck with solving this? – Tiago Duarte Aug 27 at 13:27
2
var ctx = new ClientContext(webUrl))
ctx.Credentials = new SharePointOnlineCredentials(loginId, passWord);

var workflowServicesManager = new WorkflowServicesManager(ctx, ctx.Web);
var workflowInteropService = workflowServicesManager.GetWorkflowInteropService();
var workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService();
var workflowDeploymentService = workflowServicesManager.GetWorkflowDeploymentService();
var workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService();

var publishedWorkflowDefinitions = workflowDeploymentService.EnumerateDefinitions(true);
ctx.Load(publishedWorkflowDefinitions);
ctx.ExecuteQuery();

var def = from defs in publishedWorkflowDefinitions
          where defs.DisplayName == workflowName
          select defs;

WorkflowDefinition workflow = def.FirstOrDefault();

if(workflow != null) 
{ 
    // get all workflow associations
    var workflowAssociations = workflowSubscriptionService.EnumerateSubscriptionsByDefinition(workflow.Id);
    ctx.Load(workflowAssociations);
    ctx.ExecuteQuery();

    // find the first association
    var firstWorkflowAssociation = workflowAssociations.First();

    // start the workflow
    var startParameters = new Dictionary<string, object>();

    if (ctx.Web.ListExists(listName)) {
        List list = ctx.Web.GetListByTitle(listName);

        CamlQuery query = CamlQuery.CreateAllItemsQuery();
        ListItemCollection items = list.GetItems(query);

        // Retrieve all items in the ListItemCollection from List.GetItems(Query).
        ctx.Load(items);
        ctx.ExecuteQuery();
        foreach (ListItem listItem in items) {
            Console.WriteLine("Starting workflow for item: " + listItem.Id);
            workflowInstanceService.StartWorkflowOnListItem(firstWorkflowAssociation, listItem.Id, startParameters);
            ctx.ExecuteQuery();
        }
    }
}

Complete reference: Here

0

Hi please have a look at example below:

 public class WorkflowRepository 
    {
        private InteropService _interopService;
        private WorkflowInstanceService _instanceService;
        private WorkflowSubscriptionCollection _subscriptions;
        public ClientContext Context;

        public WorkflowRepository(ClientContext clientContext) 
        {
            Context          = clientContext;
            var wfMngr       = new WorkflowServicesManager(Context, Context.Web);
            var subsrService = wfMngr.GetWorkflowSubscriptionService();
            _interopService  = wfMngr.GetWorkflowInteropService();
            _instanceService = wfMngr.GetWorkflowInstanceService();
            _subscriptions   = subsrService.EnumerateSubscriptions();

            Context.Load(_subscriptions);
            Context.ExecuteQuery();
        }

        #region basic methods 
        public Guid RunSiteWorkflow45(string wfName, IDictionary<string, object> payload)
        {
            var subscription = _subscriptions.FirstOrDefault(sub => sub.Name == wfName);
            var wf = _instanceService.StartWorkflow(subscription, payload);

            Context.ExecuteQuery();
            return wf.Value;
        }

        public Guid RunListWorkflow45(string list, int itemId, string wfName, IDictionary<string, object> payload)
        {
            var subscription = _subscriptions.FirstOrDefault(sub => sub.Name == wfName);
            if (payload == null) //StartWorkflowOnListItem will throw exception if null
                payload = new Dictionary<string, object>();

            var wf = _instanceService.StartWorkflowOnListItem(subscription, itemId, payload);

            Context.ExecuteQuery();
            return wf.Value;
        }
        #endregion
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.