Friday, September 21, 2007

SharePoint Resources, Types, Use and Deployment (Update)

For sometime I wanted to make an update to the deployment method for RESX files I proposed in a previous post. Even though there is nothing wrong with using custom jobs to deploy any file from a WSP solution to a SharePoint farm, I found about another option, which is a bit easier to implement. The code used to copy the resource files from the feature directory to the App_GlobalResources is pretty much the same, but this time not in a custom job, but directly in the feature event receiver.

SPWebApplication webApp =

this.Parent as SPWebApplication;

foreach (SPUrlZone zone in webApp.IisSettings.Keys)

{

// The settings of the IIS application to update

SPIisSettings oSettings = webApp.IisSettings[zone];

// Determine the source and destination path

sourcePath = string

.Format("{0}\FEATURES\{1}\",

SPUtility.GetGenericSetupPath(

"Template"),

featureName);

string destPath = Path.Combine(oSettings.Path.ToString(), "App_GlobalResources");

string[] filePaths = Directory.GetFiles(sourcePath, "*.resx");

// Copy the files

foreach (string filePath in filePaths)

{

string fileName = Path.GetFileName(filePath);

File.Copy(filePath, Path.Combine(destPath, fileName), true);

}

}

Then use feature stapling to activate the feature every time a new site from a given configuration is created. Feature stapling allows us to create associations between a feature and specific site configuration. To find out more about feature stapling have a look at this article: http://sharepointnutsandbolts.blogspot.com/2007/05/feature-stapling.html

Dovizhdane!

8 comments:

AAron nAAs said...

Does that work across all servers in a farm?

I was copying files during FeatureActivation, and found that it only did the activation copy on the server the feature was installed on.

Roel Hans said...

i had the same experience. I had to deactivate / activate on each server to copy the resources over to each node.

AAron nAAs said...

Interesting that it worked for you.
In one of my experiments, it didn't matter which server the code was activated on, it still only copied my solution's ascx files (it was similar to this resource issue) to the webroot of the server the solution was INSTALLED/DEPLOYED on. I remember how was strange it was to see an activation on server B cause files to appear on server A.

Mikhail Dikov said...

The key to make this work is to use Feature Stapling! Otherwise you'll get exactly the results you are observing.

In this post I give an alternative solution, which many people found better:
http://www.mikhaildikov.com/2007/03/sharepoint-resources-types-use-and_2163.html

Mikhail

Anonymous said...

Hi Mikhail,
it's me again, Matthias.
We observed that the solution you proposed using Timer job definitions works well under MOSS RTM, but doesn't work under MOSS SP1 anymore. We are not sure what the cause is. Have you tested the Timer Job Definition Solution on SP1 yet?
On the other hand, you say that when you use a web or site scoped feature that is stapled to a site definition, the feature event handler is executed on every web frontend. Without having tested it myself, why should this work? I mean, a new site is only created once on the farm, not for every web frontend. So there isn't really a good reason for the feature event handler to get fired on every frontend. Question again: Did you verify this?
Thanks for your valuable time!
Matthias

Anonymous said...

Dear Mikhail

I am trying to follow your example using feature stapling.

My stapling feature is scoped as farm, and my feature for copying the runtime resources is scoped webapplication.

However the feature for copying the runtime resources does not get activated, when i create a new the site associated with it in my stapling feature.

Can you give me a hint?

Kind reagrds

Morten Andreasen

Papa said...

I have created an stsadm extensions to deploy the resource across all servers in a farm.
http://blog.libinuko.com/2009/09/06/stsadm-o-copyfarmappbincontent-is-available-in-codeplex/

Unknown said...

Dear Mikhail,
please is there any way, how to use local resx files from App_LocalResources? I mean scenario where you embed ascx in webpart. It's easy to generate local resx file but I have a hard time to persuade sharepoint to use it. Thanks a lot.
Regards Pavel K.