0

I want to create timer job which should work as below. I have a list with field 'comments(multiple line of text)' , this timer job need to run after 1 week and check if field is empty or not.If field is empty it should send mail to 'assigned to' else will not send mail. Please help me on this

0

You must create a timer job like that:

class SyncListsJob:SPJobDefinition
{
  public SyncListsJob ():base()
  {
  }

  public SyncListsJob (string jobName, SPService service, SPServerserver, SPJobLockType targetType)
:base (jobName, service, server, targetType)
  {
  }

  public SyncListsJob (string jobName, SPWebApplication webApplication):base(jobName, webApplication, 
null, SPJobLockType.Job)
  {
    this.Title = "Job Name"
  }

  public override void Execute(Guid targetInstanceId)
  {
      SPWebApplication webapplication = (SPWebApplication)this.Parent;

                foreach (SPSite site in webapplication.Sites)
            {
                try
                {
                    foreach (SPWeb subWeb in site.AllWebs)
                    {
                        try
                        {
                            SPList list = subWeb.Lists.TryGetList("Your list Name");
                            SPQuery query = new SPQuery()
                                                {
                                                    Query = "Your query that check if field empty",
                                                    ViewAttributes = "Scope=\"Recursive\""
                                                };
                              var items =  list.GetItems(query);
                              foreach (SPListItem item in items)
                              {
                                  string messagebody = "your message body";
                                  //
                                  StringDictionary headers = new StringDictionary();
                                  headers.Add("to", subWeb.Web.CurrentUser.Email);
                                  headers.Add("cc", "xyz@abc.com");
                                  headers.Add("bcc", "");
                                  headers.Add("from", "email@add.com");
                                  headers.Add("subject", "Email Subject");
                                  headers.Add("content-type", "text/html");
                                  string bodyText = "Hello how are you?";
                                  SPUtility.SendEmail(subWeb, headers, messagebody);
                            }
                        }
                        finally
                        {
                            subWeb.Dispose();
                        }
                    }
                }
                finally
                {
                    site.Dispose();
                }
            }
  }
}

Then add a timer job in feature activated event:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
  {
    try
    {
      SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;
      //Good Practice to delete the older version of Job if it exists
      foreach (SPJobDefinition job in webApp.JobDefinitions)
      {
        if (job.Name.ToLower() == myJobName.ToLower())
        {
          job.Delete();
        }
      }


//If you want to run on Monday every week between 2:01:00 am to 2:01:05 am, use SPWeeklySchedule
SPWeeklySchedule schedule = new SPWeeklySchedule();
schedule.BeginDayOfWeek = DayOfWeek.Monday;
schedule.BeginHour = 2;
schedule.BeginMinute = 1;
schedule.BeginSecond = 0;
schedule.EndSecond = 5;
schedule.EndMinute = 1;
schedule.EndHour = 2;
schedule.EndDayOfWeek = DayOfWeek.Monday;
tempJob.Schedule = schedule;
tempJob.Update();
}
}
  • Thanks Andrey, if you can also help on below : syntax to send mail to "assign to" and what i need to change in feature if mail need to send after 7 days not monday to monday. Like if item is added on 1st and column is blank for 7 days then mail need to trigger on 8th and if another item is added on 3rd then for that mail need to trigger on 10th. – Brishal Dec 3 '13 at 8:39
  • You can easily do this with this post "SharePoint Timer Job to get weekly updates"blogs.msdn.com/b/sharepoint__cloud/archive/2013/01/13/…. And if this help please mark it as correct answer – Andrey Ungur Dec 3 '13 at 9:18
  • Thanks Andrey, also please let me know how to change syntax headers.Add("to", subWeb.Web.CurrentUser.Email); so that mail should send to 'assigned to' . 'assigned to' is field in your group. How to mark answer as correct? I am new to this forum – Brishal Dec 3 '13 at 13:05
  • item["Assigned To"]; User has the option to "accept" an answer and you can click the arrow up opposite my answer. – Andrey Ungur Dec 3 '13 at 13:13
  • Hi Andrey, item["assigned to"] syntax is not working. Can you give me full syntax which i should use instead headers.Add("to", subWeb.Web.CurrentUser.Email); to send mail to assigned to user – Brishal Dec 5 '13 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.