Monday, April 23, 2012

SharePoint Global Resource File Locations


Resource files are used to localize a solution by removing hard-coded strings from the code. SharePoint only supports string resources. Although the Resource Editor enables you to add non-string resources, non-string resources do not deploy to the SharePoint.

In SharePoint 2010, global resource files (.resx) are stored in 3 locations;

1.    App_GlobalResources: The App_GlobalResources folder is located in C:\inetpub\wwwroot\wss\VirtualDirectories\<port number>\App_GlobalResources.

2.    Resources folder in the SharePoint root folder: This is the Resources folder in the 14 hive located in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources.

3.    Config/Resources folder in the 14 hive: This is located in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\Resources.


Resources files in App_GlobalResources are used when code in an ASPX page or control refers to a resource. The syntax is “<%$Resources:<Resource File Name>, <String ID>%>”

Ex: <asp:Label ID="lblName" runat="server" Text="<%$Resources:MyResources, lblNameText%>" />


Resources files in the Resources folder in the 14 hive are used when referencing resources using the SharePoint object model

Ex: lblName.Text = SPUtility.GetLocalizedString("$Resources:lblNameText", "MyResources", (uint)SPContext.Current.Web.Locale.LCID);


Resources files in the Config/Resources are copied into the App_GlobalResources folder whenever a new web application is created. By adding the .resx files here you will ensure your application will be able to access its global resource files in new web applications.


How to deploy to App_GlobalResources and 14 hive Resources
Usually, a single .resx file in our solution should be copied into each of the above locations during the installation. We can omit the 3rd location if we are going to deal with only one web application.













Here I’m having three resource files added into a SharePoint Element. Now get the “SharePointProjectItem.spdata” file of the SP Element by selecting “Show All Files” button at the top of the solution explorer.  Then change the file content as below. Please note that there are two entries per each resource file specifying different Type and Target. That’s it. Deploy this element using a SP Feature.

<?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="GlobalResources\" Type="ElementManifest" />
    <ProjectItemFile Source="MyResources.resx" Type="AppGlobalResource" />
    <ProjectItemFile Source="MyResources.resx" Target="Resources\" Type="RootFile" />
    <ProjectItemFile Source="MyResources.en-CA.resx" Type="AppGlobalResource" />
    <ProjectItemFile Source="MyResources.en-CA.resx" Target="Resources\" Type="RootFile" />
    <ProjectItemFile Source="MyResources.fr-CA.resx" Type="AppGlobalResource" />
    <ProjectItemFile Source="MyResources.fr-CA.resx" Target="Resources\" Type="RootFile" />
  </Files>
</ProjectItem>