Friday, May 13, 2011

Publishing Pages : check-in file programmatically

One of the common scenarios when working with content migration is to handle the files checked out by other users. Following code is written to check-in all files check out by any users.

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter site url");

using (SPSite site = new SPSite(Console.ReadLine()))
{
using (SPWeb web = site.OpenWeb())
{
PublishingPageCollection publishingPages = PublishingWeb.GetPublishingWeb(web).GetPublishingPages();

foreach (PublishingPage page in publishingPages)
{
SPFile file = page.ListItem.File;
if (file.CheckedOutByUser != null)
{
Console.WriteLine("The page {0} is check out by user {1}. Check-in in to take control.", file.Title, file.CheckedOutByUser);

file.CheckIn("checked in by script", SPCheckinType.MajorCheckIn);
SPModerationInformation moderationInformation = page.ListItem.ModerationInformation;
if (moderationInformation.Status == SPModerationStatusType.Pending)
{
file.Approve("Approved by script");
}
if (moderationInformation.Status == SPModerationStatusType.Draft)
{
file.Publish("Published using script");
file.Approve("Approved by script");
}
}
}
}
}
}
}

No comments:

Post a Comment