Inconvenient Pages List Name


While working on Microsoft Office SharePoint Server (MOSS) 2007 Web Content Management (WCM) Solutions you might have relied on the fact that the name of the Pages Library was always Pages. Well almost always, because in some languages, like German, it was translated along with the Title. Given that fact, changed the way we had to deal with the Pages Library in code. Instead of hard coding the URL part of the Pages Library, all of a sudden we had to retrieve it dynamically, just because someone accidentally translated the URL of the Pages Library. But looking at SharePoint Server 2010 tells us otherwise. Now the URL parts of Pages Libraries in all languages are translated. So was it a mistake in MOSS 2007 that in German the Pages Library was called Seiten or was it a mistake that the Dutch one was called Pages instead of Paginas?

No matter the answer to the above question, there is a lesson learned for the future: to never hard code the name of the Pages Library and always retrieve it when referring to the Pages Library. And it’s a shame, because it adds both extra complexity and performance penalty to your existing code.

Retrieving the name of the Pages Library in code is not difficult and can be done using the PublishingWeb.PagesListName property. However in order to do that you have to have the instance of the PublishingWeb object first:

SPWeb web = SPContext.Current.Web;
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
string pagesListName = publishingWeb.PagesListName;

Then you can use the value of the pagesListName variable to refer to the Pages Library in code.

Next to the complexity and performance penalty the above approach has one more flaw. Remember which namespaces are supported in Sandbox? Microsoft.SharePoint.Publishing is not among them. Unfortunately, if you want to retrieve the name of the Pages Library from within a Sandbox Solution you have to use a rather quirky approach of retrieving the value of the __PagesListId property stored in the SPWeb.AllProperties Property Bag.

string pagesListIdPropertyName = "__PagesListId";
if (SPContext.Current.Web.AllProperties.ContainsKey(pagesListIdPropertyName))
{
    Guid pagesListId = new Guid(SPContext.Current.Web.AllProperties[pagesListIdPropertyName] as string);
}

And while the above does the job, and it works in the Sandbox and with Anonymous users without elevating the privileges, it somehow doesn’t seem very reliable to me…

Having that all said makes me wonder one thing: how many MOSS 2007 WCM Solutions will break after upgrading to SharePoint Server 2010 because we used Pages instead of PublishingWeb.PagesListName?

Technorati Tags: SharePoint 2010,WCM

Others found also helpful: