次の方法で共有


Be Careful Using PublishingWeb.GetPagesListName()

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.

Comments

  • Anonymous
    May 28, 2009
    PingBack from http://www.anith.com/?p=42531

  • Anonymous
    October 11, 2010
    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!

  • Anonymous
    October 11, 2010
    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!

  • Anonymous
    October 11, 2010
    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!

  • Anonymous
    November 04, 2010
    Great article. Will ref this on my blog.