0

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();
                }
            }
        }
    }
}

FeatureEventreceiver.cs`

 [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();
            }
        }
    }
}

}

  • Just a note: instead of items = list.Items.Add(); use items = list.AddItem(); and there is no need to update list every time you update an item. – Paul Strupeikis Jan 18 '16 at 10:40
0

Did you tried to debug your code. Your code has SPSite site = new SPSite(SPContext.Current.Web.Url);

This line should not work in Timer Job, Since the Timer Jobs runs as a background process it won't take any SharePoint Context from SPContext.Current.Web.Url;

Please create Property Bags in your Web Application or you can add App Settings in your web.config. Add your Web Application Url and your Site Collection url in web.config or Property Bag. Which one you prefer. Pull the Values from there and then initialize you WebApplication and Site Colection objects using those Urls. That should resolve your issue.

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.