Wednesday, February 24, 2010

Deploying Custom Resource files into "App_GloablResources"

Normally, when you create new webapplication in sharepoint the system will automatically copies all resources files from \\12\Config\Resources folder to respective webapplication Virtual directory "App_GlobalResources" folder this will work fine in normal scenarios, so in deployment you can just place your custom resources files into 12 hive Resources folder and create webapplication everything will work fine

But some times we may have to install some kind of upgradion packages with out affecting existing system which contains custom resource files? What would be the approach if we want to place all custom resources files into already created webapplication???

Options to deploy resources files in existing webapplication

1. We have to create deployment document and saying that copy and paste all the files into "App_GlobalResurces" folder

2. Create a separate feature and while activating that feature write a code to copy and paste all the files from feature folder to "App_GlobalResources" folder

Personally I prefer second approach

Please check below image to find out folder structre and where all resource files are placed



Just create feature with event receiver class in the FeatureActivated event write below code

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using Microsoft.SharePoint.Utilities;
namespace RSMTechno.DeployGlobalResources
{
class DeployGlobalResources : SPFeatureReceiver
{

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

string sourcePath = string.Empty;
SPSite site = (SPSite)properties.Feature.Parent;
SPWeb web = site.OpenWeb();
//sourcepath is the path where all resource files are stored
sourcePath = string.Format("{0}\\FEATURES\\{1}\\", SPUtility.GetGenericSetupPath("TEMPLATE"), properties.Definition.DisplayName);
try
{
SPWebApplication webApp = web.Site.WebApplication;

foreach (SPUrlZone urlZone in webApp.IisSettings.Keys)
{
SPIisSettings settings = webApp.IisSettings[urlZone];
string destinationPath = Path.Combine(settings.Path.ToString(), "App_GlobalResources");
string[] filePaths = Directory.GetFiles(sourcePath, "*.resx");
foreach (string filePath in filePaths)
{
string fileName = Path.GetFileName(filePath);
File.Copy(filePath, Path.Combine(destinationPath, fileName), true);
}

}
}
catch (Exception)
{
throw new Exception("Failed to copy files");
}
}
}
}

Then build the application and deploy and activate the feature everything will work fine

Click here to download source code RSMTechno.DeployGlobalResourceFilesCode

0 comments:

Post a Comment