Friday, February 26, 2016

Timer Job to execute a Workflow Instance

Hello SharePointers,

Here is a little guide to create a timer job and inside its code, you could execute a wf instance.

namespace SP.Sample.OnlineForms{    using System;
    using System.Collections.Generic;
    using System.Web;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Workflow;
    using Microsoft.SharePoint.WorkflowUtil;
    using Microsoft.SharePoint.Utilities;
    using System.Security.Permissions;
    using System.Text;
    using System.Collections.Specialized;
    public class SampleExpireAlertsTimerJob : SPJobDefinition    {        //SPWeb mySiteWeb;
        public SampleExpireAlertsTimerJob(SPWebApplication webApp)
            : base("Sample Expire Alerts", webApp, nullSPJobLockType.Job)
        {            this.Title = "Sample Expire Alerts";
        }        public SampleExpireAlertsTimerJob()
            : base()
        {        }        public override void Execute(Guid targetInstanceId)
        {            string key = "TeamsRootSiteUrl";
            string TeamsRootSiteUrl = "";
            if (this.Properties.Contains(key))
            {                TeamsRootSiteUrl = this.Properties[key].ToString();
                           }
            if (!string.IsNullOrEmpty(TeamsRootSiteUrl))
            {
               
                using (SPSite site = new SPSite(TeamsRootSiteUrl))
                {                    using (SPWeb web = site.RootWeb)
                    {                        SPList list = web.Lists.TryGetList("SampleExpireAlerts");
                        SPQuery oQuery = new SPQuery();
                        oQuery.Query = string.Format(@"                                                "                                              );                        SPListItemCollection collListItems = list.GetItems(oQuery);
                        foreach (SPListItem item in collListItems)
                        {                            string weburl = item["WebURL"as string;
                            string listName = item["ListName"as string;
                            string dateFieldtoFilter = item["DateFieldtoFilter"asstring;
                            int daysToFilter = int.Parse(item["DaysToFilter"asstring);
                            string workflowName = item["WorkflowName"as string;
                            StartAlertWorkFlow(weburl, listName, dateFieldtoFilter, daysToFilter, workflowName);
                        }                    }                }            }        }
        /// 
        /// Send remainder and warning emails to the employees        /// 

        /// Name of the employee ex: Sutha Thavaratnarajah
        /// Name of the module ex: conduct
        ///  Url retrived from the timer job property.
        /// email template ID.
        public void StartAlertWorkFlow(string weburl, string listName, stringdateFieldtoFilter, int daysToFilter, string workflowName)
        {            using (SPSite SampleSite = new SPSite(weburl))
            {                using (SPWeb SampleWeb = SampleSite.OpenWeb())
                {
                    SPList list = SampleWeb.Lists.TryGetList(listName);
                    SPQuery oQuery = new SPQuery();
                    //string userName = "Sutha Thavaratnarajah";                    string datetofilter = System.DateTime.Today.AddDays(daysToFilter).ToString("yyyy-MM-dd");
                    oQuery.Query = string.Format(@"                                                   
                                                     
                                                        
                                                         {1}                                                     
                                                   ", dateFieldtoFilter, datetofilter);
                    SPListItemCollection collListItems = list.GetItems(oQuery);
                    foreach (SPListItem item in collListItems)
                    {
                        item.Web.AllowUnsafeUpdates = true;
                        SPWorkflowManager workflowManager = item.Web.Site.WorkflowManager;
                        SPWorkflowAssociationCollection workflowAssociation = item.ContentType.WorkflowAssociations;
                        foreach (SPWorkflowAssociation Association inworkflowAssociation)
                        {                            if (Association.Name == workflowName)
                            {                                workflowManager.StartWorkflow(item, Association, Association.AssociationData, true);
                                break;
                            }                        }
                   
                    }                                  }            }        }    }}
 Source could be founded here.

Please let me know your experience if you have used this code :)

Until next Post!!!