SharePoint: Using Provisioning and Application Resource (.resx) Files


Using resources in SharePoint can initially be a bit confusing, in this post we will cover;

  1. The 2 different types of resources
  2. Provisioning resources into your SharePoint farm
  3. Using resources from both your deployment and runtime assets.

Types of Resources.

Provisioning Resources

These are resources (resource strings) which are used from provisioning assets such as features and site definition files (ONET.xml). A resource  file containing localised strings may reside either in the global resources folder in the 12 hive (../12/Resources) or in a Resources sub-folder of the feature.

Note, that for Administration or Application pages the global resource files location is ../12/Config/AdminResources.

The default name of a feature resource file is ..12/Template/Features/FeatureName/Resources/Resources.resx, or if a culture specific resources file is required ..12/Template/Features/FeatureName/Resources/Resources.{culture}.resx.

To reference a resource string in a feature, use the following resource string format

$Resources:{resource string key}

or

$Resources:{resource file prefix},{resource string key}

The 1st form will attempt to locate the resource string in the default resource file for the feature.

The 2nd form will attempt to locate the resource string from the resource file specified by {resource file prefix} (without the .resx extension) in the global resources folder. Alternatively, if the feature has specified it’s default resource file using the DefaultResourceFile attribute,  the {resource file prefix} part can be omitted.

E.g.

<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
		Id="985e543f-181b-4217-9bcf-3c8268bab617"
		Title="$Resources:my_feature_title"
		Description="$Resources:my_feature_description"
		Version="12.0.0.0"
		Scope="WebApplication"
		ImageUrl="GenericFeature.gif"
		Hidden="FALSE"
		AlwaysForceInstall="TRUE"
		ActivateOnDefault="TRUE">
  <ElementManifests>
	<ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>
...
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
		Id="985e543f-181b-4217-9bcf-3c8268bab617"
		Title="$Resources:myresources,my_feature_title"
		Description="$Resources:myresources,my_feature_description"
		Version="12.0.0.0"
		Scope="WebApplication"
		ImageUrl="GenericFeature.gif"
		Hidden="FALSE"
		AlwaysForceInstall="TRUE"
		ActivateOnDefault="TRUE">
  <ElementManifests>
	<ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>

As with other feature assets you would want to add the resource file(s) to the feature manifest;

  <ElementManifests>
	<ElementFile Location="Resources.resx"/>
	<ElementFile Location="Resources.en-US.resx"/>
  </ElementManifests>

Deploying Resources to the Global Resources Folder

Resource files are provisioned as root files in the solution manifest file.

  <RootFiles>
    <!-- resorce files for the solution -->
    <RootFile Location="Resources\myresources.resx"/>
  </RootFiles>

Application (Runtime)  Resources

These are resources (resource strings) used at runtime by (ASPX) page markup and code. Such resource files are located in the App_GlobalResources folder under the folder for the web application, i.e ../VirtualDirectories/{port}/App_GlobalResources, and are standard .NET strongly typed resources compiled by the .NET runtime.

Resource files in this folder are initially copied there (from the global resources folder ../12/Resources) when a web application is first created, and ordinarily at no other time. Which is a problem if you are deploying features and resources post web application creation.

You develop and deploy a feature, site definition etc, where some of the deployed assets want to use localised resources at runtime, but because the web application has already been created, your resource file will not be in the App_GlobalResources folder.

A solution to this is that you can simply copy whatever appropriate resources you need into the App_GlobalResources folder from your feature folder or from the global resources folder, during feature activation.

Provisioning Resources into Web Applications

The following code snippet can be called from a web application feature receiver and copies resource files named my*.resx from the global resources folder (../12/Resources) into the App_GlobalResources folder of the web application the feature is being activated against.

public static void ProvisionResourcesToApplicationResourcesForWebApplication(SPWebApplication webApp, bool removeProvisionedFiles)
{
	if (webApp == null) throw new ArgumentNullException("webApp");
	try
	{
		foreach (var zone in webApp.IisSettings.Keys)
		{
			var zoneSettings = webApp.IisSettings[zone];
			var sourcePath = string.Format("{0}\\Resources\\", SPUtility.GetGenericSetupPath(""));
			var destPath = Path.Combine(zoneSettings.Path.ToString(), "App_GlobalResources");
			var filePaths = !removeProvisionedFiles
										? Directory.GetFiles(sourcePath, "my*.resx")
										: Directory.GetFiles(destPath, "my*.resx");
			foreach (var filePath in filePaths)
			{
				var fileName = Path.GetFileName(filePath);
				if (!removeProvisionedFiles)
					File.Copy(filePath, Path.Combine(destPath, fileName), true);
				else
					File.Delete(Path.Combine(destPath, fileName));
			}
		}
	}
	catch
	{ }
}

Optionally during feature deactivation, the resources can be removed from the App_GlobalResources folder.

The code for this article can be found on my projects page.

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

One thought on “SharePoint: Using Provisioning and Application Resource (.resx) Files

  1. Hello,

    Its Excellent…..
    Can you tell what would happen to the extended site gobal resouces. Will this copy resource files to the extended one?

    Thank you.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.