Пример #1
0
        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();
        }
Пример #2
0
        /// <summary>
        /// Show all running workflow instances
        /// </summary>
        public static void ListAllInstances(ref ClientContext clientConext,
                                            ref WorkflowServicesManager wfServicesManager,
                                            Guid listId)
        {
            WorkflowInstanceService instService = wfServicesManager.GetWorkflowInstanceService();

            Console.WriteLine();
            Console.WriteLine("Show all running workflow instances...");

            int listItemId = 1;
            WorkflowInstanceCollection wfInstances = instService.EnumerateInstancesForListItem(listId, listItemId);

            // WorkflowInstanceCollection wfInstances = instService.EnumerateInstancesForSite(); // get instances running on the current site

            clientConext.Load(wfInstances);
            clientConext.ExecuteQuery();
            foreach (var wfInstance in wfInstances)
            {
                Console.WriteLine("{0} - {1} - {2}|{3}",
                                  wfInstance.Id,
                                  wfInstance.LastUpdated,
                                  wfInstance.Status,
                                  wfInstance.UserStatus);
            }
        }
        public void ClassLevelSetUp()
        {
            var jurisdiction = new Jurisdiction("ori", "agencyCode");

            _agency   = new AgencyDerived(jurisdiction, "name");
            _workflow = _agency.CreateWorkflow("Workflow Title", null);
            _workflowInstanceService = new WorkflowInstanceService();
            SetUpReport();
        }
Пример #4
0
        /// <summary>
        /// Publish message to existing instance.
        /// </summary>
        public static void PublishMessageToWorkflowInstance(ref ClientContext clientConext,
                                                            ref WorkflowServicesManager wfServicesManager,
                                                            WorkflowInstance instance)
        {
            WorkflowInstanceService instService = wfServicesManager.GetWorkflowInstanceService();

            Console.WriteLine();
            Console.WriteLine("Publishing event to running workflow instance...");

            // publish event
            instService.PublishCustomEvent(instance, "CustomEventName", "CustomEventMessage");
            clientConext.ExecuteQuery();
        }
Пример #5
0
        /// <summary>
        /// Return a single workflow instance.
        /// </summary>
        public static WorkflowInstance GetOneRunningInstance(ref ClientContext clientConext,
                                                             ref WorkflowServicesManager wfServicesManager,
                                                             Guid listId)
        {
            WorkflowInstanceService instService = wfServicesManager.GetWorkflowInstanceService();

            int listItemId = 1;
            WorkflowInstanceCollection wfInstances = instService.EnumerateInstancesForListItem(listId, listItemId);

            clientConext.Load(wfInstances);
            clientConext.ExecuteQuery();

            return(wfInstances.FirstOrDefault());
        }
Пример #6
0
        protected override void ExecuteCmdlet()
        {
            WorkflowServicesManager workflowServicesManager = new WorkflowServicesManager(ClientContext, SelectedWeb);
            InteropService          interopService          = workflowServicesManager.GetWorkflowInteropService();
            WorkflowInstanceService instanceService         = workflowServicesManager.GetWorkflowInstanceService();

            if (Identity.Instance != null)
            {
                WriteVerbose("Instance object set");
                WriteVerbose("Cancelling workflow with ID: " + Identity.Instance.Id);
                if (Force)
                {
                    instanceService.TerminateWorkflow(Identity.Instance);
                }
                else
                {
                    Identity.Instance.CancelWorkFlow();
                }
                ClientContext.ExecuteQuery();
            }
            else if (Identity.Id != Guid.Empty)
            {
                WriteVerbose("Instance object not set. Looking up site workflows by GUID: " + Identity.Id);
                var allinstances = SelectedWeb.GetWorkflowInstances();
                foreach (var instance in allinstances.Where(instance => instance.Id == Identity.Id))
                {
                    WriteVerbose("Cancelling workflow with ID: " + Identity.Instance.Id);
                    if (Force)
                    {
                        instanceService.TerminateWorkflow(instance);
                    }
                    else
                    {
                        instance.CancelWorkFlow();
                    }
                    ClientContext.ExecuteQuery();
                    break;
                }
            }
        }
        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();
            }
        }
Пример #8
0
        /// <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);
            }
        }
        private void WriteSubscriptionInstances(List list, WorkflowInstanceService deploymentService, WorkflowSubscription workflowSubscription)
        {
            Instances = new List <SPWorkflowInstance>();

            if (workflowSubscription != null && !workflowSubscription.ServerObjectIsNull())
            {
                var countTerminated   = deploymentService.CountInstancesWithStatus(workflowSubscription, WorkflowStatus.Terminated);
                var countSuspended    = deploymentService.CountInstancesWithStatus(workflowSubscription, WorkflowStatus.Suspended);
                var countInvalid      = deploymentService.CountInstancesWithStatus(workflowSubscription, WorkflowStatus.Invalid);
                var countCancelled    = deploymentService.CountInstancesWithStatus(workflowSubscription, WorkflowStatus.Canceled);
                var countCanceling    = deploymentService.CountInstancesWithStatus(workflowSubscription, WorkflowStatus.Canceling);
                var countStarted      = deploymentService.CountInstancesWithStatus(workflowSubscription, WorkflowStatus.Started);
                var countNotStarted   = deploymentService.CountInstancesWithStatus(workflowSubscription, WorkflowStatus.NotStarted);
                var countNotSpecified = deploymentService.CountInstancesWithStatus(workflowSubscription, WorkflowStatus.NotSpecified);


                ClientContext.ExecuteQueryRetry();

                LogVerbose("Terminated => {0}", countTerminated.Value);
                LogVerbose("Suspended => {0}", countSuspended.Value);
                LogVerbose("Invalid => {0}", countInvalid.Value);
                LogVerbose("Canceled => {0}", countCancelled.Value);
                LogVerbose("Canceling => {0}", countCanceling.Value);
                LogVerbose("Started => {0}", countStarted.Value);
                LogVerbose("NotStarted => {0}", countNotStarted.Value);
                LogVerbose("NotSpecified => {0}", countNotSpecified.Value);


                if (!DeepScan)
                {
                    var instances = deploymentService.Enumerate(workflowSubscription);
                    ClientContext.Load(instances);
                    ClientContext.ExecuteQueryRetry();

                    LogVerbose($"Instance {instances.Count}...");

                    foreach (var instance in instances)
                    {
                        Instances.Add(new SPWorkflowInstance(instance));
                    }
                }
                else
                {
                    var idx      = 1;
                    var viewCaml = new CamlQuery()
                    {
                        ViewXml = CAML.ViewQuery(string.Empty, string.Empty, 100),
                        ListItemCollectionPosition = null
                    };

                    do
                    {
                        LogVerbose($"Deep search itr=>{idx++} paging => {viewCaml.ListItemCollectionPosition?.PagingInfo}");
                        var items = list.GetItems(viewCaml);
                        this.ClientContext.Load(items, ftx => ftx.ListItemCollectionPosition, ftx => ftx.Include(ftcx => ftcx.Id, ftcx => ftcx.ParentList.Id));
                        this.ClientContext.ExecuteQueryRetry();
                        viewCaml.ListItemCollectionPosition = items.ListItemCollectionPosition;

                        foreach (var item in items)
                        {
                            // Load ParentList ID to Pull Workflow Instances
                            var allinstances = ClientContext.Web.GetWorkflowInstances(item);
                            if (allinstances.Any())
                            {
                                foreach (var instance in allinstances)
                                {
                                    Instances.Add(new SPWorkflowInstance(instance, item.Id));
                                }
                            }
                        }
                    }while (viewCaml.ListItemCollectionPosition != null);
                }
            }
        }