I have a TimerJobWorkflow and I need to start the workflow automatically when an added or changed. This is not working. I am not sure if I can use an EvenReceiver to schedule it to run automatically. Any suggestion will be appreciated. Below is the code .
public sealed partial class TimerJobWorkflow : SequentialWorkflowActivity { public TimerJobWorkflow() { InitializeComponent(); } public Guid workflowId = default(System.Guid); public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties(); private static DependencyProperty _ContextProperty = DependencyProperty.Register("_Context", typeof(WorkflowContext), typeof(TimerJobWorkflow)); public WorkflowContext _workflowNew { get { return (WorkflowContext)base.GetValue(_ContextProperty); } set { base.SetValue(_ContextProperty, value); } } private void onWorkflowActivated(object sender, ExternalDataEventArgs e) { textworkflow(); } public void textworkflow() { if (_workflowNew == null) { _workflowNew = new WorkflowContext(); _workflowNew.Initialize(workflowProperties); } } private void isWorkflowPending(object sender, ConditionalEventArgs e) { textworkflow(); e.Result = true; try { DataContext data = new DataContext(_workflowNew.Web.Url); SyncData(); int NextRun = NextRun(); DateTime time = DateTime.Now.AddMilliseconds(NextRun); System.Threading.Thread.Sleep(NextRun); TimerJobManagerItem item = new TimerJobManagerItem(); item.Title = DateTime.Now.ToString(); item.Description = "Next Run: " + time.ToString(); data.TimerJobManager.InsertOnSubmit(item); data.SubmitChanges(); data.Dispose(); } catch (Exception ex) } private int NextRun() { DateTime nextday = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day); DateTime now = DateTime.Now; tomorrow = nextday.AddHours(2); return (int)nextday.Subtract(now).TotalMilliseconds; } private void onWorkflowItemChanged(object sender, ExternalDataEventArgs e) { Contextdata(); } public void CheckJob() { Stopwatch sw = Stopwatch.StartNew(); TimeSpan elapsedTime; int seconds = 0; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(_Context.Web.Site.ID)) { using (SPWeb Web = site.OpenWeb(_Context.Web.ID)) { Web.AllowUnsafeUpdates = true; DataContext data = new DataContext(Web.Url); var item = data.TimerJob.Where(c => c.Id == int.Parse(workflowProperties.Item["ID"].ToString())).First(); item.Title = "Workflow In Progress: " + DateTime.Now.ToString(); item.Description = "In Progress"; data.SubmitChanges(); MyEventReceiver receiver = new MyEventReceiver(Web, -1); try { receiver.CheckJob(Web); sw.Stop(); elapsedTime = sw.Elapsed; seconds += elapsedTime.Seconds; } catch (Exception ex) { ex.Message.ToString(); } try { sw = Stopwatch.StartNew(); receiver.LateJob(Web); sw.Stop(); elapsedTime = sw.Elapsed; seconds += elapsedTime.Seconds; } catch (Exception ex) { ex.Message.ToString(); } int nextRuntime = NextRun(); DateTime time = DateTime.Now.AddMilliseconds(nextRuntime); item = data.TimerJob.Where(c => c.Id == int.Parse(workflowProperties.Item["ID"].ToString())).First(); item.Title = "Workflow Completed At: " + DateTime.Now.ToString() + ". Next Run - " + time.ToString(); item.Description = log; data.SubmitChanges(); } } });