Getting a SPList object no matter what language of Sharepoint 2010
Today I was deploying a Sharepoint feature with a feature receiver that has a couple of commands that add pages to the pages list and added some reusable content to the reusable content list. When testing on my delopment environment everything worked fine. But when I started deploying on the testing environment the feature wouldn’t activate. I found the following problem:
I was creating a SPList object in this way:
using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Utilities;
namespace Branding
{
[Guid("6473e847-e8d5-4adf-b904-63029f32dceb")]
public class BrandingEventReceiver : SPFeatureReceiver
{
private const string SiteTitle = "Custom site tiel";
private const string MasterPageName = "/_catalogs/masterpage/Custom.master";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
{
using (SPWeb web = site.RootWeb)
{
// set site Title
site.RootWeb.Properties["Title"] = site.RootWeb.Title;
site.RootWeb.Title = String.Format("{0}", SiteTitle);
site.RootWeb.Properties["CustomMasterUrl"] = site.RootWeb.CustomMasterUrl;
site.RootWeb.CustomMasterUrl = MasterPageName;
site.RootWeb.Update();
// disable show pages in navigation
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(site.RootWeb);
publishingWeb.Navigation.CurrentIncludePages = false;
// add reusable content
SPList list = site.RootWeb.Lists[“Reusable Content”];
SPListItem item = list.Items.Add();
item["Title"] = "Information divider";
item["AutomaticUpdate"] = "false";
item["ReusableHtml"] = @"<h2 class=divider>Information</h2>";
item.Update();
// add custom content pages
string pagesListName = SPUtility.GetLocalizedString("$Resources:cmscore,PagesListDisplayName", "cmscore", web.Language);
}
}
}
}
This is the line of code that causes the problem:
SPList list = site.RootWeb.Lists[“Reusable Content”];
The problem is that the string value “Reusable Content” doesn’t work on a Dutch (or any language) server. In this case it is called “Herbruikbare inhoud” To make this code
work for all languages you can get the name of the list from the cmscore resource file. So you can do this:
string listName = SPUtility.GetLocalizedString("$Resources:cmscore,ReusableTextListName", "cmscore", web.Language);
SPList list = site.RootWeb.Lists[listName];
We make use of a Sharepoint Utility to get a string from the resource file So our FeatureReceiver now looks like this:
using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Utilities;
namespace Branding
{
[Guid("6473e847-e8d5-4adf-b904-63029f32dceb")]
public class BrandingEventReceiver : SPFeatureReceiver
{
private const string SiteTitle = "Custom site tiel";
private const string MasterPageName = "/_catalogs/masterpage/Custom.master";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
{
using (SPWeb web = site.RootWeb)
{
// set site Title
site.RootWeb.Properties["Title"] = site.RootWeb.Title;
site.RootWeb.Title = String.Format("{0}", SiteTitle);
site.RootWeb.Properties["CustomMasterUrl"] = site.RootWeb.CustomMasterUrl;
site.RootWeb.CustomMasterUrl = MasterPageName;
site.RootWeb.Update();
// disable show pages in navigation
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(site.RootWeb);
publishingWeb.Navigation.CurrentIncludePages = false;
// add reusable content
string listName = SPUtility.GetLocalizedString("$Resources:cmscore,ReusableTextListName", "cmscore", web.Language);
SPList list = site.RootWeb.Lists[listName];
SPListItem item = list.Items.Add();
item["Title"] = "Information divider";
item["AutomaticUpdate"] = "false";
item["ReusableHtml"] = @"<h2 class=divider>Information</h2>";
item.Update();
// add custom content pages
string pagesListName = SPUtility.GetLocalizedString("$Resources:cmscore,PagesListDisplayName", "cmscore", web.Language);
}
}
}
}
In this example I also added the pages list. I don’t use this right now.
string pagesListName = SPUtility.GetLocalizedString("$Resources:cmscore,PagesListDisplayName", "cmscore", web.Language);
With thanks to my Ordina colleague Octavie van Haaften

