Chaks' Corner

SharePoint and other stuffs

18. October 2010 21:47
by Chaks
0 Comments

SharePoint 2010: Working with Resource Files

18. October 2010 21:47 by Chaks | 0 Comments

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 Files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources) 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.

Comments are closed