Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Configuration;
- using Microsoft.SharePoint.Client;
- using System.Security;
- using Microsoft.SharePoint.Client.WorkflowServices;
- using Microsoft.SharePoint.Client.Workflow;
- namespace WorkflowScheduler
- {
- class Program
- {
- static void Main(string[] args)
- {
- string siteUrl = ConfigurationManager.AppSettings["siteUrl"];
- string userName = ConfigurationManager.AppSettings["userName"];
- string password = ConfigurationManager.AppSettings["password"];
- string lists = ConfigurationManager.AppSettings["lists"];
- using (ClientContext clientContext = new ClientContext(siteUrl))
- {
- SecureString passWord = new SecureString();
- foreach (char c in password.ToCharArray())
- {
- passWord.AppendChar(c);
- }
- clientContext.Credentials = new SharePointOnlineCredentials(userName, passWord);
- 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>";
- var assetsQuery = @"<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>";
- Web web = clientContext.Web;
- List workOrderList = web.Lists.GetByTitle("Workorders");
- var workOrderCaml = new CamlQuery
- {
- ViewXml = workordersQuery
- };
- ListItemCollection workOrderListItemCollection = workOrderList.GetItems(workOrderCaml);
- List assetsList = web.Lists.GetByTitle("Assets");
- var assetsCaml = new CamlQuery
- {
- ViewXml = assetsQuery
- };
- ListItemCollection assetsListItemCollection = assetsList.GetItems(assetsCaml);
- clientContext.Load(workOrderList);
- clientContext.Load(workOrderListItemCollection);
- clientContext.Load(assetsList);
- clientContext.Load(assetsListItemCollection);
- clientContext.ExecuteQuery();
- WorkflowServicesManager wfServicesManager = new WorkflowServicesManager(clientContext, web);
- InteropService workflowInteropService = wfServicesManager.GetWorkflowInteropService();
- WorkflowAssociationCollection workordersWfAssociations = web.Lists.GetByTitle("Workorders").WorkflowAssociations;
- WorkflowAssociation workordersWfAssociation = workordersWfAssociations.GetByName("Overdue Emails");
- clientContext.Load(workordersWfAssociation);
- clientContext.ExecuteQuery();
- Console.WriteLine("Workflow Timer Job Started");
- Console.WriteLine("Running on Workorders");
- foreach(ListItem item in workOrderListItemCollection){
- var itemGuid = item["GUID"].ToString();
- var itemId = new Guid(itemGuid);
- Guid correlationId = Guid.NewGuid();
- Console.WriteLine("running on item: ..." + itemGuid);
- var resultGuid = workflowInteropService.StartWorkflow(workordersWfAssociation.Name, correlationId, workOrderList.Id, itemId, null);
- clientContext.ExecuteQuery();
- }
- WorkflowAssociationCollection assetsWfAssociations = web.Lists.GetByTitle("Assets").WorkflowAssociations;
- WorkflowAssociation assetsWfAssociation = assetsWfAssociations.GetByName("Maintenance Date Set Up");
- clientContext.Load(assetsWfAssociation);
- clientContext.ExecuteQuery();
- Console.WriteLine("Running on Assets");
- foreach (ListItem item in assetsListItemCollection)
- {
- var itemGuid = item["GUID"].ToString();
- var itemId = new Guid(itemGuid);
- Guid correlationId = Guid.NewGuid();
- var resultGuid = workflowInteropService.StartWorkflow(assetsWfAssociation.Name, correlationId, assetsList.Id, itemId, null);
- Console.WriteLine("running on item: ..." + itemGuid);
- clientContext.ExecuteQuery();
- }
- Console.WriteLine("Workflow Timer Job Done");
- Console.ReadKey();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement