Inconvenient SharePoint 2007 localization

SharePoint 2007 has built-in support for Resource files helping you achieve localization. Using these files you are able to set localized labels for the resources being provisioned. Furthermore you are able to use Resource files within your assemblies to support localization on run time. Yet there is something wrong with localization in SharePoint 2007...

One of the requirements for the project I'm currently working on is that the solution must be built on top of Dutch version of SharePoint 2007. Just to spare myself some unpleasant surprises I have decided to use that particular locale from the very beginning of the project. I got really surprised when I got a vague exception in Dutch while activating one of the Features we deliver standard with our projects. After a little research I have found out the reason of the exception: I have tried to access the Master Page Gallery using SPWeb.Lists["Master Page Gallery"]. However, since I was working with the Dutch locale it didn't exist. There was that strange thing called "Galerie met basispagina's" instead.

Looking for a solution I took a look at the SharePoint object model. SPListCollection gives you three ways to obtain a list: you can use either the ID (GUID), list index (???) and the list name. It's not really convenient as all these methods use parameters which refer to variable values. Of course you could try loading the Dutch resource file shipped with the Dutch language pack for SharePoint and then retrieving the "Galerie met basispagina's" string - it just seems a little too much to me. Why you can't get a list using the only constant information like... it's URL?!

If you take a look at the SPList class you will find that there is no Url property. Instead there is this thing called RootFolder (SPFolder) which does contain a Url property which contains the site relative url of the particular list - _catalogs/masterpage in case of "Galerie met basispagina's".

Recently I have started working with Extension Methods. Briefly: they allow you to add a custom method to an existing type, for example: SPListCollection.GetListByRootFolderUrl(string rootFolderUrl). Personally I think it's a great possibility to extend existing object models allowing you to work with the code in a more natural way.

The solution

public static SPList GetListByRootFolderUrl(  
  this SPListCollection lists, string rootFolderUrl)
  SPList list = null;

  foreach (SPList l in lists)
    if (String.Compare(rootFolderUrl,
        == 0)
      list = l;

  return list;

The solution is really straight-forward: just iterate through all available lists in the SPListCollection and return the one which has the RootFolder Url which corresponds to the rootFolderUrl parameter. If you implement the above method in a public static class, you will be able to use it like SPWeb.Lists.GetListByRootFolderUrl("_catalogs/masterpage") what will solve your SharePoint 2007 localization problem... this time.

Technorati Tags: SharePoint 2007, SharePoint, MOSS, WCM, Development


comments powered by Disqus