Easy way to deploy .resx (resource) files under App_GlobalResources folder in SharePoint 2010

Resource files in SharePoint 2010

Resource files are mainly used for localization of the custom solution.

While working with SharePoint 2010, many times the customizations need to be localized and hence, it means that developers have to play around the language specific resx files. Deployment of resx files through the SharePoint solution can be achieved using SharePoint mapped folder named "Resources" . However the files those goes under this path can be accessed using SPUtility.GetLocalizedString function. Majority of the times, you may have customizations related to the user interface which are declarative. SharePoint 2010 is based on ASP.NET technology so for, any tags related to resources the resx files will be searched in App_GlobalResources folder located under intepub\wwroot\wss\<<application>> folder hierarchy.

This puts additional overhead of deployment to the developer who want to use the traditional ASP.NET way of localizing UI resources. As, we need to ensure that the resx files should be deployed under both 14 hive and App_GlobalResources folders.

There are multiple ways to deploy the resource files. Following are few of them

  • Write an event handler to copy resx files
  • Custom timer job
  • PowerShell Script as a part of deployment
  • Manual copy and so on....

But what about the solution uninstallations, upgrades and moreover if you have multi server environments, manual deplooyment of resx files is more tedious and erroneous.

SharePoint 2010 Projects supports feature called as "Elements" . Using Elements we can deploy number of files at specific locations. Following solution makes use of Elements to deploy resx files to App_GlobalResources folder. Few advantages of this are as below

  • No need to write custom code for deployment
  • Resx files are packaged as a part of WSP
  • No need to worry about upgrades and uninistallations its done automatically in SharePoint way
  • Works perfectly on multi server enviornment
  • No manual steps

 How to do it?

  • Create new Empty SharePoint 2010 Project name it as ResxDeployment
  • Choose a local site for debugging and, choose deployment type Deploy as farm solution

 

  • In Solution Explorer, right click on ResxDeployment project and Add SharePoint Mapped Folder
  • In Add SharePoint Mapped Folder dialog box, choose Resources and click OK

  •  
  • In Solution Explorer, right click on ResxDeployment project and Add New Item
  • Add few sample resx files to the Resources folder

 

  • In Solution Explorer, right click on ResxDeployment project and Add New Item. Add Empty Element and name it as required for e.g. App_GlobalResources

 

  • 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="https://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> 
  • In Solution Explorer, right click on ResxDeployment project and choose Deploy
  • If everything goes well the solution will be deployed on given web application. Also, you can verify that the resx files are deployed under App_GlobalResources and 14\Resources folders

Points to be noted :

  1. Resource files are deployed at two diffrent locations. You can directly add resx files to only elemets folder if needed.
  2. There is no need to copy the files from Resources mapped folder to App_GlobalResources element folder in Visual studio. There is only one copy of resx file hence, no need to worry about multiple location changes
  3. For every additional resx file, SharePointProjectItem.spdata file should be updated otherwise the file will not be copied to App_GlobalResources folder of web application
  4. Please set the Scope of the Feature that includes this module as relavent. It is preferrable to set, Application Level Scoped

In this way, by using OOTB SharePoint features we can deploy resx files to App_GlobalResources folder.

Comments

  • Anonymous
    May 01, 2012
    Great post, thanks Yogesh.