Programmatically schedule an item in SharePoint 2010


In this article we will be seeing how to schedule an item in SharePoint 2010. An item can be scheduled so that item is visible on the site only between the scheduled start date and end date. An item will be scheduled to be automatically approved (Status-Approved) and to be unpublished (Status-Draft) on specified dates.

Before scheduling the item go to the Library Settings => Add from existing site columns => add the following columns.

ScheduShare1.gif

Item Scheduling:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;

namespace ItemScheduling
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://serverName:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list=web.Lists["Doc Library"];
                    SPListItem listItem = list.GetItemById(1);              
                    ScheduledItem scheduledItem = null;
                    if (ScheduledItem.IsScheduledItem(listItem))
                    {
                        scheduledItem = ScheduledItem.GetScheduledItem(listItem);
                    }
                    else
                    {
                        throw new System.ArgumentException
                          ("SPListItem must support scheduling",
                          "listItem");
                    }

                    DateTime startDate = new DateTime(2011, 4, 6, 22, 50, 00);
                    DateTime endDate = new DateTime(2011, 4, 6, 22, 51, 00);
                    scheduledItem.StartDate = startDate;
                    scheduledItem.EndDate = endDate;
                    scheduledItem.ListItem.Update();
                    scheduledItem.Schedule();
                }
            }
        }
    }
}


Once the item is scheduled you could see the approval Status as "Scheduled".

ScheduShare2.gif

If the start date is earlier than the current date, the item is published immediately and Approval Status will become "Approved". If not, it is scheduled to publish at a future date.

ScheduShare3.gif

If the end date is specified, the item is scheduled to unpublished and the Approval Status will become "Draft "at a future date.

ScheduShare4.gif
 

Up Next
    Ebook Download
    View all
    Learn
    View all