SharePoint Custom webpart with SharePoint resource files


In SharePoint custom webpart we may need to display the message or label text from a congfiguration resource files, this article shows the how to read the resource files from the custom webpart.

The article shows the step by step with the help of visual studio 2010.

Step 1: First we need to create the visual webpart project in visual studio 2010.

Step 2: After adding the visual webpart project, in the solution explorer, right click and add new sharepoint mapped folder and refer the folder Resources.

Once the resource folder is mapped, right click and add the folder (since so many resource files are present in the 14\Resource folder used by sharepoint and we create our own folder and use it for the particular webpart /particular web application ). Lets call the folder as CustomResource folder.

Step 3: Now add the resource file by right click on the CustomResource folder and add new item, select c#, and select the resource file, name it as CustomResource.resx

Step4 : Add the key value in the resources file, say for e.g CustomSuccessMsg, and value as “Thanks for submitting the form “.

Step 5: Now we can use the resoure file in our custom webpart, by adding the below code.

uint currentLanguage = currentWeb != null ? currentWeb.Language : 1033;

string stSuccessMsg = SPUtility.GetLocalizedString(“$Resources:CustomSuccessMsg”, “CustomResource\\CustomResource”, currentLanguage );

Step 6: now compile the webpart and deploy using vs 2010 or using powershell.

Once its deployed if the site is running in english language then it will read it from CustomResource.resx file from the CustomResource folder of SharePoint Root folder.

If the language is differnet, then appropriate resource file is required. Some sample resource file name appropriate lanaguage



I hope this will help some one who try to use resource file to store the message or listname or any other configuration parameter according to the lanaguage of the site is going to be used.