protected override void ExecuteCmdlet() { int ListItemID; if (ListItem != null) { if (ListItem.Id != uint.MinValue) { ListItemID = (int)ListItem.Id; } else if (ListItem.Item != null) { ListItemID = ListItem.Item.Id; } else { throw new PSArgumentException("No valid list item specified.", nameof(ListItem)); } } else { throw new PSArgumentException("List Item is required", nameof(ListItem)); } var subscription = Subscription.GetWorkflowSubscription(SelectedWeb) ?? throw new PSArgumentException($"No workflow subscription found for '{Subscription}'", nameof(Subscription)); var inputParameters = new Dictionary <string, object>(); WorkflowServicesManager workflowServicesManager = new WorkflowServicesManager(ClientContext, SelectedWeb); WorkflowInstanceService instanceService = workflowServicesManager.GetWorkflowInstanceService(); instanceService.StartWorkflowOnListItem(subscription, ListItemID, inputParameters); ClientContext.ExecuteQueryRetry(); }
public void ProcessOneWayEvent(SPRemoteEventProperties properties) { if (properties.EventType != SPRemoteEventType.ItemAdded) { return; } // build client context using S2S using (ClientContext context = TokenHelper.CreateRemoteEventReceiverClientContext(properties)) { Web web = context.Web; // create a collection of name/value pairs to pass to the workflow upon starting var args = new Dictionary <string, object>(); args.Add("RemoteEventReceiverPassedValue", "Hello from the Remote Event Receiver! - " + DateTime.Now.ToString()); // get reference to Workflow Service Manager (WSM) in SP... WorkflowServicesManager wsm = new WorkflowServicesManager(context, web); context.Load(wsm); context.ExecuteQuery(); // get reference to subscription service WorkflowSubscriptionService subscriptionService = wsm.GetWorkflowSubscriptionService(); context.Load(subscriptionService); context.ExecuteQuery(); // get the only workflow association on item's list WorkflowSubscription association = subscriptionService.EnumerateSubscriptionsByList(properties.ItemEventProperties.ListId).FirstOrDefault(); // get reference to instance service (to start a new workflow) WorkflowInstanceService instanceService = wsm.GetWorkflowInstanceService(); context.Load(instanceService); context.ExecuteQuery(); // start the workflow instanceService.StartWorkflowOnListItem(association, properties.ItemEventProperties.ListItemId, args); // execute the CSOM request context.ExecuteQuery(); } }
/// <summary> /// Create a new workflow instance. /// </summary> public static void CreateInstance(ref ClientContext clientConext, ref WorkflowServicesManager wfServicesManager, WorkflowSubscription subscription) { WorkflowInstanceService instService = wfServicesManager.GetWorkflowInstanceService(); Console.WriteLine(); Console.WriteLine("Creating workflow instance..."); Dictionary <string, object> startParameters = new Dictionary <string, object>(); // if there are any values to send to the initiation form, add them here startParameters.Add("Name1", "Value1"); startParameters.Add("Name2", "Value2"); // start an instance instService.StartWorkflowOnListItem(subscription, 1, startParameters); //instService.StartWorkflow(subscription, startParameters); // when starting on a site clientConext.ExecuteQuery(); Console.WriteLine("Workflow started!"); }
/// <summary> /// Start a SharePoint List Workflow /// </summary> /// <param name="siteUrl">URL of the SharePoitn Site</param> /// <param name="workflowName">Name of the SharePoint2013 Workflow</param> /// <param name="itemID">ID of the Item on which to start the workflow List, if item ID is equal to 0 the worlflow to start is considered as site workflow</param> /// <param name="initiationData">Any custom parameters you want to send to the workflow</param> public bool Execute(worflowParameters param) { try { using (ClientContext clientContext = new ClientContext(param.siteUrl)) { string user = ConfigurationManager.AppSettings["USER"]; string domain = ConfigurationManager.AppSettings["DOMAIN"]; string password = ConfigurationManager.AppSettings["PASSWORD"]; System.Net.NetworkCredential cred = new System.Net.NetworkCredential(user, password, domain); clientContext.Credentials = cred; Web web = clientContext.Web; //Workflow Services Manager which will handle all the workflow interaction. WorkflowServicesManager wfServicesManager = new WorkflowServicesManager(clientContext, web); //The Subscription service is used to get all the Associations currently on the SPSite WorkflowSubscriptionService wfSubscriptionService = wfServicesManager.GetWorkflowSubscriptionService(); //All the subscriptions (associations) WorkflowSubscriptionCollection wfSubscriptions = wfSubscriptionService.EnumerateSubscriptions(); //Load only the subscription (association) which we want. You can also get a subscription by definition id. clientContext.Load(wfSubscriptions, wfSubs => wfSubs.Where(wfSub => wfSub.Name == param.workflowName)); clientContext.ExecuteQuery(); //Get the subscription. WorkflowSubscription wfSubscription = wfSubscriptions.First(); //The Instance Service is used to start workflows and create instances. WorkflowInstanceService wfInstanceService = wfServicesManager.GetWorkflowInstanceService(); if (param.initiationData == null) { param.initiationData = new Dictionary <string, object>(); } //validate item id if (param.itemID > 0) { wfInstanceService.StartWorkflowOnListItem(wfSubscription, param.itemID, param.initiationData); } else { wfInstanceService.StartWorkflow(wfSubscription, param.initiationData); } clientContext.ExecuteQuery(); return(true); } } catch (Exception err) { responseMsg = err.Message; return(false); } }