0

I have a requirement of fetching the SPWorkflow object on a SPListItem object. The assocaiated workflow is a sharepoint 2013 visual studio workflow.

I have tried the following approaches, but none of them are working:

using (SPSite site = new SPSite(url))
                {
                    using (SPWeb oWeb = site.OpenWeb())
                    {
                        var workflowServiceManager = new WorkflowServicesManager(oWeb);
                        var workflowSubscriptionService = workflowServiceManager.GetWorkflowSubscriptionService();

                        //get all workflows associated with the list
                        var subscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(list.ID);

                        foreach (var workflowSubscription in subscriptions)
                        {

                            var inputParameters = new Dictionary<string, object>();


                            WorkflowInstanceService instanceService = workflowServiceManager.GetWorkflowInstanceService();

                            if (instanceService != null)
                            {
                                foreach (WorkflowInstance wfInstance in instanceService.EnumerateInstancesForListItem(list.ID, currentItem.ID))
                                {
                                    SPWorkflow wf = new SPWorkflow(currentItem, wfInstance.Id);
                                }
                            }
                        }

                        SPWorkflowManager spWorkflowManager = site.WorkflowManager;
                        SPWorkflowFilter filter = new SPWorkflowFilter();

                        filter.InclusiveFilterStates = SPWorkflowState.Running;

                        SPWorkflowTaskCollection workTaskColl = oWeb.Site.WorkflowManager.GetItemTasks(currentItem, filter);

                        foreach (SPWorkflowTask task in workTaskColl)
                        {
                        }

                        foreach (SPWorkflow workflow in spWorkflowManager.GetItemActiveWorkflows(currentItem))
                        {
                            foreach (SPWorkflowTask t in workflow.Tasks)
                            {
                            }
                        }

                        foreach (SPWorkflow instance in spWorkflowManager.GetItemWorkflows(currentItem))
                        {


                        }
                    }
                }

Using workflow subscription method I am getting the correct workflow instance object, but unable to create the workflow object out of it. other approaches like GetItemTasks, GetItemActiveWorkflows, and GetItemWorkflows are always returning 0, although there are values present.

Are there any other approach by which I can get the workflow object programmatically.

0

My requirement was to get all the tasks associated with the workflow, I managed to achieve the same by querying the task list and filtering the tasks on the basis of new field added to sharepoint 2013 "related items".

I am not sure how efficient this approach is but nevertheless worth mentioning

    using (SPSite mySite = new SPSite(SPContext.Current.Web.Url))
                        {
                            using (SPWeb myWeb = mySite.OpenWeb())
                            {
                                var filteredtaskItem = (from SPListItem itm in myWeb.Lists.TryGetList("TaskListName").Items
                                                        where GetRelatedItem2(myWeb, itm["Related Items"].ToString() + "") == itemID.ToString()
                                                        select itm).ToList();


                                foreach (SPListItem item in filteredtaskItem)
                                {
                                }
                            }
                        }


public string GetRelatedItem2(SPWeb web, string relatedItems)
        {
            string itemID = string.Empty;
            relatedItems = relatedItems.Trim(new char[] { '[', '{', '}', ']' });
            string[] relatedItemsVals = relatedItems.Split(',');
            Dictionary<string, string> items = new Dictionary<string, string>();
            foreach (var itemsVal in relatedItemsVals)
            {
                string[] keyval = itemsVal.Split(':');
                items[keyval[0].Trim('"')] = keyval[1].Trim('"');
            }

            string itemIDStr = items["ItemId"];
            string listIDStr = items["ListId"];
            Guid listID = new Guid(listIDStr);
            SPList itemList = web.Lists[listID];
            SPItem item = null;
            try
            {
                item = itemList.GetItemById(int.Parse(itemIDStr));
                if (item != null)
                {
                    itemID = item["ID"].ToString();
                }
            }
            catch (Exception ex)
            {

                itemID = string.Empty;
            }
            //SPItem item = itemList.GetItemById(int.Parse(itemIDStr));
            return itemID;
        }

itemID is the id of the list item on which the workflow is running. As I am querying the task list directly which increases day by day hence I am not sure about the performance.

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.