简体   繁体   中英

TimerJob sharepoint server 2013

In sharepoint all list having Status column(Datatype is Dropdown) & Duedate Column. Duedate column exceed the todays date Status Column should be changed Closed automatically using Timerjob.I used this code but is not working please someone modify this code and how to achieve this solution.

class.cs

namespace TimerJobNew
{
    class StatusCompleted : SPJobDefinition

    {
         public const string jobName = "CompletedProjectsJob";
        public StatusCompleted() : base() { }
        public StatusCompleted(SPWebApplication webApplication)
            : base(jobName, webApplication, null,SPJobLockType.Job)
        {
            Title = "Completed Projects Job";
        }
        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication webApp = this.Parent as SPWebApplication;
            SPSite site = new SPSite(SPContext.Current.Web.Url);
            SPWeb web = site.RootWeb;
           // SPWeb web = webApp.Sites["/sites/test"].RootWeb;
            SPList list = web.Lists.TryGetList("CommonList");
            SPListItem items;
            bool flag =true;
            SPListItemCollection itemColl = list.Items;
            var query =new SPSiteDataQuery();
            query.Lists = "<Lists BaseType='0' />";
            query.ViewFields = "<FieldRef Name='Title' Nullable='TRUE' />" +
                               "<FieldRef Name='Status' Nullable='TRUE' />" +
                               "<FieldRef Name='CommonlistID' Nullable='TRUE' />";

            query.Query = "<Where>" +
                                "<Eq>" +
                                    "<FieldRef Name='Status' />" +
                                    "<Value Type='Choice'>Closed</Value>" +
                                "</Eq>" +
                           "</Where>";
            query.Webs = "<Webs Scope='SiteCollection' />";
            DataTable dt = web.GetSiteData(query);
            foreach (DataRow row in dt.Rows)
            {
                items = list.Items.Add();
                if (itemColl.Count != 0)
                {
                    foreach (SPListItem item in itemColl)
                    {
                        if (item["CommonlistID"].ToString() == row["CommonlistID"].ToString())
                        {
                            flag = false;
                            break;
                        }
                        else
                        {
                            flag = true;
                        }
                    }
                    if (flag ==true)
                    {
                        items["Title"] = row["Title"].ToString();
                        items["Status"] = row["Status"].ToString();
                        items["CommonlistID"] = row["CommonlistID"];
                        items.Update();
                        list.Update();
                    }
                }
                else
                {
                    items["Title"] = row["Title"].ToString();
                    items["Status"] = row["Status"].ToString();
                    items["CommonlistID"] = row["CommonlistID"];
                    items.Update();
                    list.Update();
                }
            }
        }
    }
}


     [Guid("95a2f297-4d46-45b2-b792-a7874f11ce08")]
    public class StatusFeatureEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            DeleteJob(webApp.JobDefinitions);
            StatusCompleted tasksTimerJob = new StatusCompleted(webApp);
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            tasksTimerJob.Schedule = schedule;
            tasksTimerJob.Update();
        }       
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            DeleteJob(webApp.JobDefinitions);
        }
        private void DeleteJob(SPJobDefinitionCollection jobs)
        {
            foreach (SPJobDefinition job in jobs)
            {
                if (job.Name.Equals(StatusCompleted.jobName,
                StringComparison.OrdinalIgnoreCase))
                {
                    job.Delete();
                }
            }
        }
    }
}

As said in comment, your code'll never work since SPContext is null when using SPTimerJob. (Note this is the same for EventReceiver)

You can enhanced the constructor of your job to retrieve more information. So, in your case you could do :

public StatusCompleted(SPWebApplication webApplication, string TargetSite)
        : base(jobName, webApplication, null,SPJobLockType.Job)
{
    Title = "Completed Projects Job";
    Properties.Add("TargetSite", TargetSite);
}

Then, when you call your Execute() method, you can retrieve the site :

string targetList = (this.Properties["TargetList"] ?? string.Empty).ToString();
using (SPSite site = new SPSite(targetSite))
{
   using (SPWeb web = site.OpenWeb())
   {
   //TO DO
   }
}

Hope this helps

And last thought, don't forget to debug your TimerJob. It'll be very useful to find the error. You just have to attach your Visual Studio to the process OWSTIMER.exe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM