in SharePoint 2010

SharePoint 2010: Working with Resource Files

If you are working with multi-lingual SharePoint deployments, then you will end up working with Resource files in your SharePoint projects. Resource files (.resx) helps developers to create and manage localizable content.

Let us see how you can make use of the resource files in your SharePoint projects using Visual Studio 2010.

Add a Resource File (.resx)

First, you need to add a SharePoint mapped folder – Resources – to your project:

image

The Resources folder (C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions14Resources) is where SharePoint manages and loads the resource files.

image

Right click on the Resources folder and add a new item – Resources File

image

This will add the .resx file to Resources folder which means that this will be deployed to the mapped Resources folder

image

Now, you can add strings to this resource file.

Using Resource Strings

Now that you have a resources file, it is very simple to use the resource strings in CAML and code

In CAML:

<Field ID="{1b49fd88-9aac-4b9c-add6-8497cac1fb8a}"
    Name="$Resources:Chaks.SharePoint.ResourceSample,Field_MyTextFieldName"
    DisplayName="$Resources:Chaks.SharePoint.ResourceSample,Field_MyTextFieldDisplayName"
    Type="TEXT"
    Group="Chaks Sample Fields" />

The format being:

$Resources: <resource-file-name-without-extension>, <resource-string-name>

In Code:

We use the SPUtility.GetLocalizedString

SPUtility.GetLocalizedString(
     "$Resources:Chaks.SharePoint.ResourceSample,Field_MyTextFieldDisplayName",
     "Chaks.SharePoint.ResourceSample",
      spWeb.Language)

Using Resource Strings in Features

What about features?

Set the Default Resource File to your resources file (.resx) file:

image

You can also control the visibility of the feature depending on whether resources are available in the language by setting the RequireResources property:

image

Now, you can can reference your resource in the Feature properties:

image

Sample Solution

You can download the sample solution here.

Write a Comment

Comment

Time limit is exhausted. Please reload the CAPTCHA.

  1. Hi Chak, any particular reason why you prefer to specify a global resource file for the feature resources rather than rely on the feature default resource file (feature, right-click, Add Feature Resource)?

    Is it possible to use the feature default resource file (feature, right-click, Add Feature Resource) for resources in the feature event receivers code? AFAIK it is not, you need a global resource file for that. Is that why you prefer the global resource file?

    Thanks