Skip to content
Loading
  • 0
    Thanks for the answer. Unfortunatelly the itemCollection is always empty. I do have a workflow working in one list everytime a new item is added. Any idea why I cant go in the foreach-loop?
    0
  • Rajkiran Swain
    0
    http://www.enjoysharepoint.com/Articles/Details/sharepoint-online-site-workflow-and-initiation-form-parameters-example-using-21914.aspx
     
    https://www.schaeflein.net/setting-workflow-status-in-visual-studio-and-sharepoint-designer/ 
    0
  • 0
    Here is the answer for what I was looking for. Maybe not the best way to do it but now I have a start.
    1. //I was logged as admin but still got an error with the credentials. This solved it.   
    2.   
    3. string siteUrl = "myWebsite";  
    4.             NetworkCredential credentials = new NetworkCredential("Username", "Password", "Domain");  
    5.             ClientContext clientContext = new ClientContext(siteUrl);  
    6.             clientContext.Credentials = credentials;  
    7.   
    8. //Getting the Lists  
    9.  Web web = clientContext.Web;  
    10.             ListCollection lists = web.Lists;  
    11.             clientContext.Load(lists);  
    12.             clientContext.ExecuteQuery();  
    13.   
    14. //Setting the workflowmanager  
    15.  var manager = new Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager(clientContext,web);  
    16.             var subscription = manager.GetWorkflowSubscriptionService();  
    17.             var instance = manager.GetWorkflowInstanceService();  
    18.   
    19.  foreach(List l in lists)  
    20.             {  
    21.                 var wflowsubscription = subscription.EnumerateSubscriptionsByList(l.Id);  
    22.                 clientContext.Load(wflowsubscription);  
    23.                 clientContext.ExecuteQuery();  
    24.                 foreach(var wfsub in wflowsubscription)  
    25.                 {  
    26. //show is a Listbox to visualize it in the Webpart  
    27.                     show.Items.Add(wfsub.Name);  
    28.                     var q = new Microsoft.SharePoint.Client.CamlQuery();  
    29. //Only the Title  
    30.                     q.ViewXml = " ";  
    31.                     var listItems = l.GetItems(q);  
    32.                     clientContext.Load(listItems);  
    33.                     clientContext.ExecuteQuery();  
    34.   
    35.  foreach(var listItem in listItems)  
    36.                     {  
    37.                         var wflowInstanceCollection = instance.EnumerateInstancesForListItem(l.Id, listItem.Id);  
    38.                         clientContext.Load(wflowInstanceCollection);  
    39.                         clientContext.ExecuteQuery();  
    40.                         foreach(var wflowInstance in wflowInstanceCollection)  
    41.                         {  
    42.                             string wflowStatus = wflowInstance.Status.ToString();  
    43.   
    44.                        //Adding to the listbox too  
    45.                             show.Items.Add(listItem["Title"].ToString());  
    46.                             show.Items.Add("Workflowstatus " + wflowStatus);  
    47.                             show.Items.Add("Last update " + wflowInstance.LastUpdated);  
    48.                              
    49.   
    50.                             //Since I didnt find a correct status list I just wrote all types I found... must be corrected.   
    51.                             switch(wflowStatus)  
    52.                             {  
    53.                                 case "None": //TODO  
    54.                                     break;  
    55.                                 case "Locked": //TODO  
    56.                                     break;  
    57.                                 case "Running": //TODO  
    58.                                     break;  
    59.                                 case "Expiring": //TODO  
    60.                                     break;  
    61.                                 case "Expired": //TODO  
    62.                                     break;  
    63.                                 case "Faulting": //TODO  
    64.                                     break;  
    65.                                 case "Suspended": //TODO  
    66.                                     break;  
    67.                                 case "Orphaned": //TODO  
    68.                                     break;  
    69.                                 case "HasNewEvents": //TODO  
    70.                                     break;  
    71.                                 case "NotStarted": //TODO  
    72.                                     break;  
    73.                                 case "Completed": //TODO  
    74.                                     break;  
    75.                                 case "Started": //TODO  
    76.                                     break;  
    77.                                 case "Terminated": warningMail();  
    78.                                     break;  
    79.                                 case "Cancelled": //TODO  
    80.                                     break;  
    81.                                 case "Failed on Start": //TODO  
    82.                                     break;  
    83.                                 case "Error Ocurred": //TODO  
    84.                                     break;  
    85.                                 case "Approved": //TODO  
    86.                                     break;  
    87.                                 case "Rejected": //TODO  
    88.                                     break;  
    89.                                 case null: //TODO  
    90.                                     break;  
    91.   
    92.                             }  
    93.                                  
    94.                              
    95.                         }  
    96.                     }  
    97.                 }  
    98.             }  
    99.   
    100.         } 
     I uploaded this answer also in the technet forum: https://social.technet.microsoft.com/Forums/en-US/954492fd-d402-4256-9df3-3cab5ab915b1/get-internal-status-from-workflows-in-c?forum=sharepointdevelopment
     
     
    Thanks in advance.
    0