Thursday, April 29, 2010

SharePoint tips: Working in localized environment

My SharePoint tips or lessons learned in practice

In this post I would like to share my list of things to keep-in-mind when working on a SharePoint solution for a localized environment. All these are learned the hard way – after hours spent in debugging and troubleshooting. Hope this saves you some time :-)

Lesson 1: Strong internal name

When creating a new SharePoint field use strong internal name. This is well-known practice, but since this is my list of things to keep-in-mind, I will start with it. Here is why:

1. Using a strong internal name, you will have easy way to refer to the field in your code
2. You will have unique name (rather than “Title1”, “Title2” etc)
3. Once you take control and use your internal name code practice, you will have the choice not to work with names like: “Some_%20_Column_%20_Name” (and this might be pretty annoying sometimes), but “Some_Column_Name” or just “SomeColumnName”.

There are many different posts showing how to give specific internal name for your SharePoint field, so I will skip this part.

Lesson 2: Use "SPListTemplateType"

Don’t use this line of code when creating list programmatically:

Guid guid = web.Lists.Add(listName, String.Empty, web.ListTemplates["Document"]);

Simply because “Document” list template might be called “Документ” in a localized environment. Instead, better use this one:

Guid guid = web.Lists.Add(listName, String.Empty, SPListTemplateType.DocumentLibrary);

It even looks better!

Lesson 3: Use "SPBuiltInFieldId" enumerator

If you want to access built-in "Title" field in a localized environment, and you do something like this:

item["Title"] = "Some title";

You may end up with:

System.ArgumentException: Value does not fall within the expected range.
at Microsoft.SharePoint.SPFieldCollection.GetFieldByDisplayName(String strDisplayName, Boolean bThrowException)

This is why SharePoint team gives us "SPBuiltInFieldId" enumerator which can be used to access all built-in fields by GUID. So, the line above will look like this:

item[SPBuiltInFieldId.Title] = "Some title";

Lesson 4: Use “SPBuiltInContentTypeId” enumerator

When working with build-in content types, use "SPBuiltInContentTypeId" enumerator to get them. Don’t rely on display name, in a localized environment this will not work for sure!

Lesson 5: Use “SPRoleType”

Let’s say you have a list, and you need to set programmatically permissions for it. To do this, you have to get “Full Control” role definition. Which line of code is correct?

1. SPRoleDefinition role = web.RoleDefinitions["Full Control"];
2. SPRoleDefinition role = web.RoleDefinitions.GetByType(SPRoleType.Administrator);

And the answer is: both. But if the question is: “Which line of code is better to use?”, then the answer definitely is “2”.

Using "GetByType" method and "SPRoleType" enumerator to access a role definition will guarantee that you won’t deal with exceptions like: “Microsoft.SharePoint.SPException: Permission level cannot be found”.
Bookmark and Share

No comments: