Pages

Search This Blog

Friday, February 11, 2011

Publishing Sharepoint 2007 Publishing Pages,Master pages using Object Model

Requirement : We are having 150+ pages. So every time we have to demo when we have to go to pages library and master page library to check the publishing status. It was tedious task to make sure that all the pages, master pages and css file in style library are check in and published.
Solution : We have create a console application so every time when we needs all the pages to be check in and approve all we have to do is to run the console applicaton .
Code:


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
namespace Publishing_Sharepoint_Pages
{
class Program
{
static void Main(string[] args)
{
//Get the reference of site object
using (SPSite site = new SPSite("http://test"))
{
//Get the spweb object
using (SPWeb webs = site.OpenWeb())
{
// To publish all the page in pages library
//Get the publishing instance of the web
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(webs);
//Get the pages library
PublishingPageCollection pages = pubWeb.GetPublishingPages();
foreach (PublishingPage page in pages)
{
SPModerationInformation moderationStatus = page.ListItem.ModerationInformation;
//Check if the page is not approved
if (moderationStatus.Status != SPModerationStatusType.Approved)
{
// the page page status approved and update the page status
page.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
page.Update();
}
}
// Code to publish the master page
//Get the master page libaray reference
SPList masterPageGallery = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
//Navigate the all item in master page
// This will also include all the page layouts
foreach (SPListItem galleryPage in masterPageGallery.Items)
{
//if the page is status in not published
if (!galleryPage.HasPublishedVersion)
{
//Check in the page with automated comments and set the status to checkin
galleryPage.File.CheckIn("Automatically approved", SPCheckinType.MajorCheckIn);
galleryPage.File.Update();
// approve the page with comments
galleryPage.File.Approve("Automatically approved");
galleryPage.File.Update();
}
}
// Update the style library items
SPList styleLibrary = webs.Lists["Style Library"];
//Get all the css from the style library
SPFolder editingMenuFolder = styleLibrary.RootFolder;
foreach (SPFile cssFile in editingMenuFolder.Files)
{
// if the file is not checkin then check in the file with comments.
if (cssFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
cssFile.CheckIn("Automatically approved");
cssFile.Update();
}
}
}
}
}
}
}

No comments:

Post a Comment