Technology Toolbox

Your technology Sherpa for the Microsoft platform

Jeremy Jameson - Founder and Principal

Search

Search

Be Careful Using PublishingWeb.GetPagesListName()

Note
This post originally appeared on my MSDN blog:

Since I no longer work for Microsoft, I have copied it here in case that blog ever goes away.

A couple of years ago when we began evaluating Language Packs for Microsoft Office SharePoint Server (MOSS) 2007, we discovered that after installing certain Language Packs, the "Pages" library may be localized -- including both the list name as well as the URL.

For example, after installing the Spanish Language Pack, if you had any code that referred to something like "/en-US/Products/Pages/default.aspx" then you would soon find that your code could break when referring to the equivalent page on a localized site. For example, on a Spanish site, the equivalent URL would be "/es-ES/Products/Paginas/default.aspx" (assuming you chose not to translate "Products" when creating the Spanish site).

Fortunately, the SharePoint API provides the PublishingWeb.GetPagesListName method. In the following code sample, note how I use this method to avoid hard-coding the name of the Pages library:

        private static string GetDefaultSearchResultsPageUrl(
            SPWeb searchWeb)
        {
            Debug.Assert(searchWeb != null);

            string pageListName = PublishingWeb.GetPagesListName(searchWeb);

            string defaultSearchResultsPageUrl = string.Format(
                CultureInfo.InvariantCulture,
                "{0}/{1}/Results.aspx",
                searchWeb.ServerRelativeUrl,
                pageListName);

            return defaultSearchResultsPageUrl;
        }

Consequently, this code works as expected, even on localized sites created with various Language Packs.

The problem -- as we discovered on our project this week -- is that you have to be careful about how you use the "list name" returned from PublishingWeb.GetPagesListName().

Last week, another developer on our team sent out an email inquiring about how to get the Pages list in a way that worked even with localized sites. I quickly responded pointing out the PublishingWeb.GetPagesListName() method.

Unfortunately, the developer then added the following code to his feature:

SPList pages = web.Lists[PublishingWeb.GetPagesListName(web)];

This seems natural, after all, since web.Lists is simply an SPListCollection and SPListCollection provides the following property:

public SPList this [string strListName] { get; }

This appeared to work during his unit testing and so he checked in his changes. The problem is that this doesn't work in all cases (meaning for all Language Packs).

If you look at the documentation for the PublishingWeb.GetPagesListName method, you will see that it states the method returns the "URL name of the pages list" (emphasis mine) -- which, in most cases, is the same as the "name of the pages list."

Unfortunately, for certain Language Packs the "URL name" is different from the list "name." For example, in Chinese the URL name is "Pages" but the list name is "页面".

The correct way to get the Pages list is to use the PublishingWeb.GetPagesListId method instead:

SPList pages = web.Lists[PublishingWeb.GetPagesListId(web)];

It looks like the PublishingWeb.GetPagesListName() method really should have been called GetPagesUrlName instead.

Tags

Comments

  1. # re: Be Careful Using PublishingWeb.GetPagesListName()

    October 11, 2010 5:22 PM
    Dirk

    Jeremy - that some saved not only my day but the whole week!

    Due to this behaviour, my chinese site definitions did not work either...

    Thanks for sharing this!

  2. # re: Be Careful Using PublishingWeb.GetPagesListName()

    October 11, 2010 5:22 PM
    Dirk

    Jeremy - that saved not only my day but the whole week!

    Due to this behaviour, my chinese site definitions did not work either...

    Thanks for sharing this!

  3. # re: Be Careful Using PublishingWeb.GetPagesListName()

    October 11, 2010 5:22 PM
    Dirk

    Jeremy - that saved not only my day but the whole week!

    Due to this behaviour, my chinese site definitions did not work either...

    Thanks for sharing this!

  4. # re: Be Careful Using PublishingWeb.GetPagesListName()

    November 4, 2010 5:08 PM
    Danny

    Great article. Will ref this on my blog.

  5. # Anith » Be Careful Using PublishingWeb.GetPagesListName()

    May 27, 2009 9:18 PM
    www.anith.com

Add Comment

Optional, but recommended (especially if you have a Gravatar). Note that your email address will not appear with your comment.
If URL is specified, it will be included as a link with your name.

To prevent spam from being submitted, please select the following fruit: Watermelon

Pear
Apple
Watermelon
Cherries
Grapes
Strawberry
 
Please add 3 and 1 and type the answer here: