Saturday, July 21, 2012

Use provisioning resources in page layouts in Sharepoint

In Sharepoint provisioning resources are stored in 14/Resources folder. In order to use the same resource literals declaratively e.g. in page layouts (remember that uncustomized page layouts are retrieved in 14/Template/Features subfolder, which is not even virtual folder. Nevertheless it is possible to use resources in them declaratively – see below) you may need to copy them to App_GlobalResources folder of your web application in IIS (e.g. C:\inetpub\wwwroot\wss\VirtualDirectories\MyWebApp80\App_GlobalResources). If you worked with Sharepoint 2007 you probably know that there is special stsadm command which makes similar things: copyappbincontent. In this post I will show how to achieve the result using only Visual Studio features.

In order to do that at first double click on your package in VS solution tree:

image

Then in opened package window click “Manifest” tab. Scroll content and below the window you will find Edit options section:

image

Expand it and write the following in text area:

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <Solution xmlns="http://schemas.microsoft.com/sharepoint/">
   3:   <ApplicationResourceFiles>
   4:     <App_GlobalResourceFile Location="Resources\MyResources.resx" />
   5:     <App_GlobalResourceFile Location="Resources\MyResources.ru-RU.resx" />
   6:     <App_GlobalResourceFile Location="Resources\MyResources.fi-FI.resx" />
   7:   </ApplicationResourceFiles>
   8: </Solution>

In this example I assume that your provisioning resources are stored in Resources mapped folder in Visual Studio (this is common scenario). After that package wsp and deploy it. After that check App_GlobalResources folder of your web application. It should contain Resources subfolder with provisioning resources copied in it. The fact that resx files are located in subfolder should not worry you. ASP.Net allows using resources from subfolders of App_GlobalResources, so you will be able to add literals to your page layouts by standard way:

   1: <asp:Literal runat="server" Text="<%$Resources:MyResources,Foo%>" />

And this is useful, because as I said page layouts are located not in virtual folder. In virtual folders like 14/Template/Layouts or 14/Template/ControlTemplates you may create App_LocalResources using default ASP.Net convention (resource file should be the same as page or user control file name with “.resx” extension, e.g. MyControl.ascx.resx) and use resources even simpler without resx file name prefix:

   1: <asp:Literal runat="server" Text="<%$Resources:Foo%>" />

This technique allows to use standard ASP.Net expressions for resources in page layouts.

2 comments:

  1. Hi, Alexey!
    If it is possible to separate run-time resources from provisioning resources, then you could create 2 mapped folders in VS2010 - first one will be mapped to "14/Resources/" folder and the second one will be mapped to "App_GlobalResources" folder (how to map the last one - http://blog.mastykarz.nl/provisioning-appglobalresources-sharepoint-2010/)

    ReplyDelete
  2. hi Vladimir,
    thanks for the tip. To be true I didn't often use this method. Almost always use the same file both for provisioning and global resources.

    ReplyDelete