SharePoint Custom webpart with SharePoint resource files

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

Step 4: Add Empty Element named "App_GlobalResources"


Step 5: In Solution Explorer, click on Show All Files icon to view all files. Open SharePointProjectItem.spdata files located under App_GlobalResources node. Add the relavent information to the xml file which includes Source, and Type of the file. The type should be set to AppGlobalResource and source should be a relative path of resx file. Following is the example

<?xml version="1.0" encoding="utf-8"?> 
<ProjectItem Type="Microsoft.VisualStudio.SharePoint.GenericElement" DefaultFile="Elements.xml" SupportedTrustLevels="All" SupportedDeploymentScopes="Web, Site, WebApplication, Farm, Package" xmlns="http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel"> 
 <Files> 
 <ProjectItemFile Source="Elements.xml" Target="App_GlobalResources\" Type="ElementManifest" /> 
 <ProjectItemFile Source="..\Resources\Contoso.resx" Type="AppGlobalResource"/> 
 <ProjectItemFile Source="..\Resources\Contoso.de-DE.resx" Type="AppGlobalResource"/> 
 <ProjectItemFile Source="..\Resources\Contoso.en-GB.resx" Type="AppGlobalResource"/> 
 <ProjectItemFile Source="..\Resources\Contoso.en-US.resx" Type="AppGlobalResource"/> 
 <ProjectItemFile Source="..\Resources\Contoso.es-ES.resx" Type="AppGlobalResource"/> 
 <ProjectItemFile Source="..\Resources\Contoso.it-IT.resx" Type="AppGlobalResource"/> 
 </Files> 
</ProjectItem> 

Helper Code:

<asp:Label ID="lblTitle" runat="server" Text="<%$Resources:[ResourceFileName],[keyName] %>"></asp:Label>



public string getLocalizedValue(string strInput, int intLCID)
{
  // Function to retreive specified Language Variation Value
  // [FolderName] : foldername under mapped "SharePoint Resource folder"
  // vsProject\\vsProject = [FolderName]\\[Resourcefile Name]
  
  string strLocalizedValue = "";

  strLocalizedValue = Microsoft.SharePoint.Utilities.
SPUtility.GetLocalizedString("$Resources: " + strInput, "vsProject\\vsProject", (uint)intLCID);
  return
 strLocalizedValue;
}


Reference Site:
http://blogs.msdn.com/b/yojoshi/archive/2012/04/29/easy-way-to-deploy-resx-resource-files-under-app-globalresources-folder-in-sharepoint-2010.aspx

http://ganpages.wordpress.com/2011/10/22/sharepoint-custom-webpart-with-sharepoint-resource-files/

Comments

Popular posts from this blog

How to remove app from SharePoint 2013/2016

The model backing the context has changed since the database was created. Consider using Code First Migrations to update the database