Advertisement
Guest User

Start Workflow CSOM C#

a guest
Nov 19th, 2015
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Configuration;
  7. using Microsoft.SharePoint.Client;
  8. using System.Security;
  9. using Microsoft.SharePoint.Client.WorkflowServices;
  10. using Microsoft.SharePoint.Client.Workflow;
  11.  
  12. namespace WorkflowScheduler
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             string siteUrl = ConfigurationManager.AppSettings["siteUrl"];
  19.             string userName = ConfigurationManager.AppSettings["userName"];
  20.             string password = ConfigurationManager.AppSettings["password"];
  21.             string lists = ConfigurationManager.AppSettings["lists"];
  22.  
  23.                 using (ClientContext clientContext = new ClientContext(siteUrl))
  24.                 {
  25.                     SecureString passWord = new SecureString();
  26.  
  27.                     foreach (char c in password.ToCharArray())
  28.                     {
  29.                         passWord.AppendChar(c);
  30.                     }
  31.  
  32.                     clientContext.Credentials = new SharePointOnlineCredentials(userName, passWord);
  33.  
  34.                     var workordersQuery = @"<View><Query><OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy><Where><And><Leq><FieldRef Name='Assets_x003a_Maintenace_x0020_Da' /><Value Type='DateTime'><Today /></Value></Leq><Eq><FieldRef Name='Issue_x0020_Status' /><Value Type='Text'>Assigned</Value></Eq></And></Where></Query></View>";
  35.                     var assetsQuery = @"<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>";
  36.  
  37.                     Web web = clientContext.Web;
  38.  
  39.                     List workOrderList = web.Lists.GetByTitle("Workorders");
  40.  
  41.                     var workOrderCaml = new CamlQuery
  42.                     {
  43.                         ViewXml = workordersQuery
  44.                     };
  45.                     ListItemCollection workOrderListItemCollection = workOrderList.GetItems(workOrderCaml);
  46.  
  47.                     List assetsList = web.Lists.GetByTitle("Assets");
  48.  
  49.                     var assetsCaml = new CamlQuery
  50.                     {
  51.                         ViewXml = assetsQuery
  52.                     };
  53.                     ListItemCollection assetsListItemCollection = assetsList.GetItems(assetsCaml);
  54.                     clientContext.Load(workOrderList);
  55.                     clientContext.Load(workOrderListItemCollection);
  56.                     clientContext.Load(assetsList);
  57.                     clientContext.Load(assetsListItemCollection);
  58.                     clientContext.ExecuteQuery();
  59.  
  60.                     WorkflowServicesManager wfServicesManager = new WorkflowServicesManager(clientContext, web);
  61.                     InteropService workflowInteropService = wfServicesManager.GetWorkflowInteropService();
  62.  
  63.                     WorkflowAssociationCollection workordersWfAssociations = web.Lists.GetByTitle("Workorders").WorkflowAssociations;
  64.                     WorkflowAssociation workordersWfAssociation = workordersWfAssociations.GetByName("Overdue Emails");
  65.                     clientContext.Load(workordersWfAssociation);
  66.                     clientContext.ExecuteQuery();
  67.  
  68.                     Console.WriteLine("Workflow Timer Job Started");
  69.  
  70.                     Console.WriteLine("Running on Workorders");
  71.  
  72.                     foreach(ListItem item in workOrderListItemCollection){
  73.                         var itemGuid = item["GUID"].ToString();
  74.                         var itemId = new Guid(itemGuid);
  75.                         Guid correlationId = Guid.NewGuid();
  76.                         Console.WriteLine("running on item: ..." + itemGuid);
  77.                         var resultGuid = workflowInteropService.StartWorkflow(workordersWfAssociation.Name, correlationId, workOrderList.Id, itemId, null);
  78.                         clientContext.ExecuteQuery();
  79.                     }
  80.  
  81.                     WorkflowAssociationCollection assetsWfAssociations = web.Lists.GetByTitle("Assets").WorkflowAssociations;
  82.                     WorkflowAssociation assetsWfAssociation = assetsWfAssociations.GetByName("Maintenance Date Set Up");
  83.                     clientContext.Load(assetsWfAssociation);
  84.                     clientContext.ExecuteQuery();
  85.                     Console.WriteLine("Running on Assets");
  86.                     foreach (ListItem item in assetsListItemCollection)
  87.                     {
  88.                         var itemGuid = item["GUID"].ToString();
  89.                         var itemId = new Guid(itemGuid);
  90.                         Guid correlationId = Guid.NewGuid();
  91.                         var resultGuid = workflowInteropService.StartWorkflow(assetsWfAssociation.Name, correlationId, assetsList.Id, itemId, null);
  92.                         Console.WriteLine("running on item: ..." + itemGuid);
  93.                         clientContext.ExecuteQuery();
  94.                     }
  95.  
  96.                     Console.WriteLine("Workflow Timer Job Done");
  97.                     Console.ReadKey();
  98.                 }
  99.             }
  100.         }
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement