Using resource files in your SharePoint customisations

Have you ever been putting a piece of code together and thought to yourself "I know I shouldn’t be hard coding that string" but you don’t think that its something that would belong in a configuration file? Well then this is for you. For those who don’t know about resource files, they are basically an XML document that is a collection of strings and references to other files that your code accesses. The beauty of the resource files is that they are very easy to integrate into every part of your code, and you can extend the use of these resource files to globalise your application.

So first of all, I have to credit Mikhail Dikov and Tom for helping get this all figured out. You should really go check out their blog posts there, as I am really just summarising and combining the info from both of these guys.

To add a resource file to your SharePoint solution, I think the best place to do it is with a feature (for a change!). You can use a web application scoped feature with a custom receiver to deploy your resource to the web application. (All resources for an application reside in the App_GlobalResources folder for the application under the inetpub/wwwroot/wss/virtualdirectories/[port] folder). Here is the code I put into the receiver of my web application feature:

   1: public override void FeatureActivated(SPFeatureReceiverProperties properties)
   2: {
   3:     SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
   4: foreach (SPUrlZone zone in webApp.IisSettings.Keys)
   5:     {
   6: // The settings of the IIS application to update     
   7:         SPIisSettings oSettings = webApp.IisSettings[zone];
   8:  
   9: // Determine the source and destination path     
  10:         String sourcePath = string.Format("{0}FEATURES{1}", SPUtility.GetGenericSetupPath("Template"), properties.Feature.Definition.DisplayName);
  11:         string destPath = Path.Combine(oSettings.Path.ToString(), "App_GlobalResources");
  12:         string[] filePaths = Directory.GetFiles(sourcePath, "*.resx");
  13:  
  14: // Copy the files      
  15: foreach (string filePath in filePaths)
  16:         {
  17: string fileName = Path.GetFileName(filePath);
  18:             File.Copy(filePath, Path.Combine(destPath, fileName), true);
  19:         }
  20:     }
  21: }

This takes every resx file from the feature folder and deploys it to the web application (this is a slight modification of the code from Mikhail’s blog).

Once you have your resource file deployed, you need to start using the strings in it within your code. Luckily this is usually a single line of code (depending on how you want to do it. Here are some examples taken from Tom’s blog:

In any C# code:

   1: HttpContext.GetGlobalResourceObject("MyResource", "MyName").ToString();

As a property of a control in an ASPX page:


   1: <%$Resources:MyResource, MyName%>

As text in an ASPX page:

   1: <asp:literal runat="server" Text="<%$Resources:MyResource, MyName%>" />

In XML:

   1: $Resources:MyResource, MyName

So there it is, how to use resource files in your SharePoint customisations. Once again, seriously go and have a read of both of the blog posts I mention above (by Mikhail Dikov and Tom) because they go into a lot more detail about how MOSS handles resource files, which is some stuff you are going to want to know to really get your head around it. And seriously, when resource files are this easy to you, you don’t really have much of an excuse to be hard coding all of those strings into your application now, do you?

  1. No trackbacks yet.