How to get Publishing Page Meta Data (Column Value) in Sharepoint

Requirement:

The requirement is to identify status (Draft/Approved/Checkout/Pending/Published) of page in Pages library and also fetch the metadata of page using SharePoint Ojbect Model in Publishing Web.

Resolution:

With the help of PublishingWeb class (Microsoft.SharePoint.Publishing), we can access different components of Published Web site like Pages document library, Published site navigation bar and etc.

The PublishingWeb class provides publishing behavior for an SPWeb instance that supports publishing. This class cannot be inherited.

Namespace: Microsoft.SharePoint.Publishing
Assembly: Microsoft.SharePoint.Publishing (in microsoft.sharepoint.publishing.dll)


In this example we will identify status (Draft/Approved/Checkout/Pending/Published) of page and access metadata of Pages from Pages Library in Publishing Web:



using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Publishing;


namespace GetPublishedData
{
class Program
{
static void Main(string[] args)
{
try
{

using (SPSite site = new SPSite("http://amitkumarmca04.blogspot.com"))
{
using (SPWeb web = site.OpenWeb())
{
// Check web is PublishingWeb or not
if (PublishingWeb.IsPublishingWeb(web))
{
// Get the PublishingWeb
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
// Get the Page list from PublishingWeb
SPList pagesList = publishingWeb.PagesList;
if (pagesList != null && pagesList.Items.Count > 0)
{
// Get a reference to a publishing page

foreach (SPListItem page in pagesList.Items)
{
//Find status (Draft/Approved/Checkout/Pending/Published) of page
if (page.ModerationInformation.Status == SPModerationStatusType.Draft)
{
Console.WriteLine("Page in Draft mode.");
}
else if (page.ModerationInformation.Status == SPModerationStatusType.Approved)
{
Console.WriteLine("Page in Approved mode.");
}
else if (page.ModerationInformation.Status == SPModerationStatusType.Pending)
{
Console.WriteLine("Page in Pendig mode.");
}

if (page.Level == SPFileLevel.Checkout)
{
Console.WriteLine("Page in Checkout mode.");
}
else if (page.Level == SPFileLevel.Published)
{
Console.WriteLine("Page in Published mode.");
}
else if (page.Level == SPFileLevel.Draft)
{
Console.WriteLine("Page in Draft mode.");
}

//Find metadata of page
if (string.Equals(page.Name, "AmitKumar.aspx", StringComparison.InvariantCultureIgnoreCase))
{
//Access the page metadata
Console.WriteLine(Convert.ToString(page["Title"]));
}
}
}
}
else
{
// If the SPWeb is not a PublishingWeb,
// then GetPagesListName returns the URL
// that would be used by the Pages list
// if the Publishing feature were to be
// activated.
string pagesListName = PublishingWeb.GetPagesListName(web);
//After getting the url we can access Pages list
}
}
}

}
catch (Exception ex)
{
throw;
}

}
}
}

2 Comments

Previous Post Next Post