Friday, January 20, 2012

Working with Resource file in SharePoint 2010

Here we will discuss how to work with resource file in SharePoint 2010. Also we will see how we can add our own custom resource file to SharePoint and how we can retrieve the resource file contents.
Resource files are really helpful if you are on multi-lingual SharePoint sites, where your site supports multiple languages. SharePoint resource files are stored normally at this location C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources .

Its better to follow SharePoint resource file naming conventions like core.en-US.resx (Default file provided by SharePoint.

So we will discuss, how we will create a custom resource file by using Visual Studio 2010 and how to deploy the resource also by using Visual Studio 2010.
First open your visual studio 2010 then click on File -> New Project and then choose Empty SharePoint Project template from New project dialog box, give a name and click on OK as shown in the figure below.
After that in the SharePoint Customization Wizard, Specify your local site for debugging and Choose Deploy as farm solution as Shown in the figure below and click on Finish.
Now if you will open your solution explorer, then your empty SharePoint 2010 project is ready. Now since the resource files needs to be deployed in the Resources folder in the 14 hive directory, lets take the help of SharePoint Mapped folder for the same.

To add this Right click on the project -> Add -> SharePointMappedFolder as shown in the figure below:
Ok in the next step, from the Add SharePoint Mapped Folder dialog select the Resource folder and click on OK as shown in the figure below.
Now our Mapped folder Resource is ready, So in the next step we need to add our resource file. To add a resource file into the Resource folder .

To do this Right click on the Resource Mapped folder from the project then click on Add ->Add New Item  as shown in the figure below:
And then from the Add New Item dialog box Select the Resource File template and click on ADD as shown in the figure below.
The above step will add our resource file and then our task is to add the content for the resource file. Add your content as shown in the figure below. We have added 2 Name and their corresponding Value like below:
Now to deploy the project then Right click on the Solution explorer and then click on Deploy Solution. This will deploy our resource file to the corresponding mapped folder. If you want you can check in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources location, our resource file should be there.

Now since our web part is deployed to the site collection, now we will see how we can use the contents of the resource file.

For this write the below code:
var lang = SPContext.Current.Web != null ? SPContext.Current.Web.Language : 1033;
string firstName = SPUtility.GetLocalizedString("$Resources:FirstName", "English-us", lang);
string lastName = SPUtility.GetLocalizedString("$Resources:LastName", "English-us", lang);

Remember to use the using statement: using Microsoft.SharePoint.Utilities;

No comments:

Post a Comment