Anonymous Access and Publishing Web Pages List

December 22 2011 4 comments

As You know, using PublishingWeb.GetPublishingWeb(web) method causes Access Denied exception for anonymous users. You will also get the same exception if you try to access the PublishingWeb.GetPagesListId(web) or PublishingWeb.GetPagesListName(web) methods.
There are solutions here and here and in many other blogs. These solutions use SPSecurity.RunWithElevatedPrivileges, but that’s not ideal solution for anonymous sites.
So how to get pages list from a web, if you don’t even know if the web is publishing site and you are not able to use PublishingWeb.IsPublishingWeb(web)?
Heres yet another solution. Open the web you want to check if it is a publishing site and try to get the pages list using TryGetList method with the right localized pages list name. You will get the localized name from cmscore.resx file using SPUtility.GetLocalizedString method with resource key “List_Pages_UrlName”.

var pages = web.Lists.TryGetList(SPUtility.GetLocalizedString(
                 string.Format("$Resources:{0}", "List_Pages_UrlName"),
                 "cmscore", web.Language));

if (pages != null)
{
// do the do
}

It all gets interesting, if the site is not publishing web, but still contain a list called “Pages”. Then You might want to check another things from returned list, not just if it’s null like in the snippet above.

***********************************************
Jerry Seinfeld: Why not? We’re neighbors. What’s mine is yours.
Cosmo Kramer: [leaning against the door-frame and looking around in wonder] Really?
***********************************************

Popularity: 1% [?]

4 comments to “Anonymous Access and Publishing Web Pages List”

  1. Juha Alhojoki says:

    Thanks, I needed this today and knew where to look for a solution.

  2. This is definitely amazing, You will be a extremely specialized digg. I’ve got become a member of the rss and search in advance for you to trying to find a greater portion of a person’s superb write-up. Also, We’ve provided your web blog during my internet sites

  3. hey says:

    This article offers clear idea in favor of the new users of blogging, that truly how to do blogging.|

Leave a Reply