Archive

Posts Tagged ‘Localized’

Getting localized pages listname & url (PublishingWeb.GetPagesListName(web))

November 5th, 2010 No comments

In a piece of code I wrote I had to access the pages list. I had to do this on English and Dutch site. So to get the nameĀ  for the pages list I used the function

PublishingWeb.GetPagesListName(web) On my English site this worked fine. On my Dutch site it didn’t work. So I started to trace what was happening. This is a short example

of the code in a console application

try
{
string SiteUrl = ConfigurationManager.AppSettings["SiteUrl"];
using (SPSite oSPSite = new SPSite(SiteUrl))
{

using (SPWeb web = oSPSite.OpenWeb())
{

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

SPListItemCollection allItems = pagesList.Items;

Console.WriteLine(allItems.Count.ToString());

}
}

Console.ReadLine();

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);

}

On my English site the name of the pages list = “pages” and url = “pages.” The result off the function PublishingWeb.GetPagesListName(web) gives back “pages” so no problem.

On myDutch site the name of the pages list = “Pagina’s” and the url = “paginas”. When executing the fuinction on my Dutch site I get the result “paginas”. So the function PublishingWeb.GetPagesListName gives back url and not the name. When I try to access the list

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

I am trying to get this

SPList pagesList = web.Lists["paginas"];

witch gives an exception because this list doesn’t exist. What I need to do is:

SPList pagesList = web.Lists["pagina's"];

So I found a trick to make this work There is also a function called PublishingWeb.GetPagesListId This gives back the guid Id of the list. So this works fine

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

With thanks to Jeremy Jameson