Resource Files within SharePoint 2010


What are Resource files?

Resource files are XML-based files ending in .resx, they are typically used for localization. To localize a SharePoint solution, you remove the hard-coded strings from the code or front end aspx page and abstract them into resource files. There are 3 types of resource files: Default, language-neutral and language-specific.

A default resource files are the files contain string localized for the default culture, such as English. These are used if no localized resource files for the given language can be found.

A Language-Neutral resource file contains strings localized for a language, but not a specific culture. E.g., “it” for Italian.

A language-specific resource file contains strings localized for a language and culture. E.g., “pt-BR” for Portuguese – Brazil or “pt-PT” for Portuguese-Portugal.

Resource files in SharePoint.

Today, I discovered that the resource file created for the code behind .cs files cannot be used/found when using them in the ASPX pages. This is because in SharePoint 2010, global resource files are stored in 3 locations.

  1. Config/Resources folder in the SharePoint Root: This is located in the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\Resources. Resource files in this area 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 it’s global resource files in new web applications.
  2. Resource folder in the SharePoint Root folder: This is the resource folder located in the 14 hive, c:\Program files\Common Files\Microsoft Shared\Web Server Extensions\14\Resources. Resource files located in this folder are used when referencing resources using the SharePoint object model.
  3. App_GlobalResources: The App_GlobalResources folder is located in C:\inetpub\wwwroot\wss\VirtualDirectories\<WebApp:Port>\App_GlobalResources. Resources files stored here are used when coding in an ASPX page or control.

SharePoint Solution Resource file with Code Behind

The following steps will show you a basic project that will use a Resource folder in the code behind.

  • Create an Empty SharePoint Solution that is a farm solution
  • Right click the project and select Add > SharePoint Mapped Folder.
  • Select Resources folder and click OK.
  • Right click your new Resources folder and click Add > New Item.
  • Under General templates click Resources File and give it a name like MyProjectResource.resx. Click Add
  • The MyProjectResource.resx file is an XML file, but Visual Studio gives us a nice GUI when you double click on it.
  • Note:You can only add string to SharePoint resource files. Resource Editor enables non-string resources, however these do not deploy to SharePoint.
  • I have added 3 strings to my resource file. (Can you guess that I’m creating a Hello World project yet?)
  • Right click the project and click Add > New Item.
  • Under SharePoint 2010 templates select Application Page and give it a name like WelcomePage.aspx.
    Click Add.
  • In the aspx page
    with the PlaceHolderMain put the following code. This will give us our basic Hello World page.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

  <div>

  <div id="label"><SharePoint:EncodedLiteral ID="lblWelcome" runat="server"></SharePoint:EncodedLiteral></div>

  <div id="text"><asp:TextBox ID="txtbox" runat="server"></asp:TextBox></div>

  <div id="button"><asp:Button ID="btnClick" runat="server" OnClick="btnClick_Click" /></div>

  <div id="resultsLabel"><SharePoint:EncodedLiteral ID="lblResult" runat="server"></SharePoint:EncodedLiteral></div>

 </div>

</asp:Content>

  • In the .cs code behind, put the following code.

protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

            //This section will allow you to update the page title. Page.Title doesn't work in SharePoint.

            ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)

            Page.Master.FindControl("PlaceHolderPageTitle");

            // Clear out anything that SharePoint might have put in it already

            contentPlaceHolder.Controls.Clear();

            // Put your content in

            LiteralControl literalControl = new LiteralControl();

            //Get resource Title from my Resource File.

            literalControl.Text = SPUtility.GetLocalizedString("$Resources:Title", "MyProjectResource", (uint)SPContext.Current.Web.Locale.LCID);

            contentPlaceHolder.Controls.Add(literalControl);


            lblWelcome.Text = SPUtility.GetLocalizedString("$Resources:WhoAreYouLabel", "MyProjectResource", (uint)SPContext.Current.Web.Locale.LCID);

            btnClick.Text = SPUtility.GetLocalizedString("$Resources:WhoAreYouButton", "MyProjectResource", (uint)SPContext.Current.Web.Locale.LCID);

        }


        public void btnClick_Click(object sender, EventArgs e)

        {

            lblResult.Text = "Hello " + txtbox.Text;

        }

  • As you can see we are calling the Name within the resource file, the default resource file name, and the LCID language
  • Run the solution and navigate to the page. You will see the labels you put in the resource file will be displayed within your application page. (I’ve added some styles)

SharePoint Solution Resource file in ASPX page.

  • Continuing on from the last solution, delete the OnInit method we created. We are going to add this directly to the ASPX page instead.
  • In the ASPX file replace PlaceHolderMain, PlaceHolderPageTitle and PlaceHolderPageTitleInTitleArea with the following

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

  <div>

  <div id="label"><SharePoint:EncodedLiteral ID="lblWelcome" runat="server" Text="<%$Resources:MyProjectResource, WhoAreYouLabel  %>"></SharePoint:EncodedLiteral></div>

  <div id="text"><asp:TextBox ID="txtbox" runat="server"></asp:TextBox></div>

  <div id="button"><asp:Button ID="btnClick" runat="server" OnClick="btnClick_Click" Text="<%$Resources:MyProjectResource, WhoAreYouButton %>"/></div>

  <div id="resultsLabel"><SharePoint:EncodedLiteral ID="lblResult" runat="server"></SharePoint:EncodedLiteral></div>

 </div>

</asp:Content>


<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">

 <SharePoint:EncodedLiteral ID="litPageTitle" runat="server" Text="<%$Resources:MyProjectResource,Title %>"></SharePoint:EncodedLiteral>

</asp:Content>


<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >

<SharePoint:EncodedLiteral ID="litPageInTitleArea" runat="server" Text="<%$Resources:MyProjectResource,Title %>"></SharePoint:EncodedLiteral>

</asp:Content>

  • The way to add a Resource to an ASPX page is <%$Resouces:<DefaultResourceFileName>, <ResourceName>%>
  • If you deploy now you will encounter an error message saying that the resource object was not found. That is because it is looking for the resource file in the App_GlobalResources folder.

    The way to fix this would be to create a Module, delete both the Elements.xml and sample.txt file from the module, and place the Resource.resx file directly into the module. Then change the Deployment Type on the resource.resx file to AppGlobalResource. Add the Module to the new Feature that has been created in your project. Then run your project.


    You will find that now your SharePoint page at last works again. However, if you try to use any code behind now with the aspx resource you will find it doesn’t work either.


    So do you have to create a Resource file for ASPX pages and one for code behind? With multiple languages that could spawn many files that would need to be maintained.

Hybrid solution

  • Remove the text for the lblWelcome in the ASPX page.
  • In the .cs file re-add OnInit method and just add the lblWelcome.Text line.

protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

            lblWelcome.Text = SPUtility.GetLocalizedString("$Resources:WhoAreYouLabel", "MyProjectResource", (uint)SPContext.Current.Web.Locale.LCID);

        }

  • Move the MyProjectResource.resx file back from the Module to the Resources folder, and delete the feature. Your solution should look similar to below

  • In the menu bar of the Solution Explorer, click the Show All Files icon.
  • In the Resources folder you should now see another file called SharePointProjectItem.spdata
    • Double click this file and open it. You will find 4 lines of XML within this file.
    • We need to remove the ProjectItemFolder and replace it with ProjectItemFiles. Two lines for each resource file. One to copy the resource file to the Resource folder in the SharePoint Root folder, and one to copy into the App_GlobalResources. Replace line 3 with the following.

  <Files>

    <ProjectItemFile Source="MyProjectResource.resx" Target="Resources\" Type="RootFile"/>

    <ProjectItemFile Source="MyProjectResource.resx" Type="AppGlobalResource"/>

  </Files>

  • Save this file and deploy your solution. You will now find all labels are working, allowing you to have 1 resource file for both aspx pages and code behind pages.
Advertisements

One thought on “Resource Files within SharePoint 2010

Comments are closed.