Friday, July 01, 2011

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

}
}
}
Share:

2 comments:

micalaston said...

hey dude this is really nice blog and its very useful to learn metatag in sharepoint and also useful coding.please continue posted this type of another blog and thanks for sharing information

SaMolPP p said...

hi,
can you pls tell, how to get teh controls within this page?
i would like to change the summarylink field within this page and change its url.
thnx ina dvance

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels