Working with resource files in SharePoint 2010

Thought I´d share a little light on my experiences around working with resource file (.resx) in SharePoint 2010.

Background

First of all, we have three “types” of resource file in SharePoint. I´m not going to go into detail about that but you can read more about it here if you are interested (it´s the same lineup in 2010 as 2007).

My “mission” is as always to make SharePoint “behave” like an asp.net application (where we have got the trusted App_GlobalResources folder). In SharePoint there is also SharePoint resources (SharePointRoot\Resources) which we can use in features and such. I would like to use the same .resx file for both of these types of resources and not maintain two of them.

In SharePoint 2007 (with Visual Studio 2008) I usually had the resources in a folder at the root of my project, called Resources, and then I XCopied (or RoboCopied) the resource files to 12/Resources and 12/CONFIG/Resources. Why these two locations?
Well, in 2007 we needed to run stsadm –o copyappbincontent to copy stuff from 12/CONFIG/Resources to the IIS virtual directory/App_GlobalResources because that was the only way to get the files over there in safe manner.

In 2010 we have some new options. Let me walk you through those:

The mapped folder approach

Add a new mapped folder which maps to the resources folder in the SharePoint root.
This adds a new folder to the root of your project called” Resources” (resembles the way I did it in 2007) and all the files you put there will end up in SharePointRoot/Resources (or 14/Resources). Great!
What about making those files also appear in App_GlobalResources. No problem, open up the packaging explorer and go into edit mode of the manifest.xml file:

Click that link and we now have the ability to add App_Global Resource files like this:

A little note here though: If you want to add files from that mapped folder (called Resources) you will have to specify them like above (Resources\[your resx file])
Also, you can´t rename that mapped folder into something more relevant (like MyResources). Visual Studio will keep the path as Resources (no mather what name you´ll give it) in the WSP as you can see below:

When the deployment takes place the files are also copied to App_Global resources like you can see below:

But wait…they are copied into a folder named Resources! And as I stated above, you can´t change that name…damn it!

The App_GlobalResoures SPI approach

Another way to do it, even though I don´t like maintaining two separate files, is to add a new Empty Element SPI (Project Item):

I name mine App_GlobalResources because that will give me a folder with that name inside my project and that will look like it would in an asp.net project…I like that!

Change the deployment type to AppGlobalResource and also change the deployment location like:

Remove that Path stuff (the arrow is showing that in the before and after picture above). Why?
Otherwise you will get a folder named [Project Name]\App_GlobalResources inside your App_GlobalResource folder…not OK!

I like this approach better than the first one because it gets rid of that Resources folder name inside App_GlobalResources (even though it works with more than one WSP writing into that folder…looks weird but works).

You can of course keep that other mapped folder for your SharePoint specific resources (don´t forget to remove the stuff from the manifest though) and it will look like this:

The conclusion will be that it now works much better….not perfect but a hell lot more flexible now that in 2007!

About these ads

, , , ,

  1. #1 by Amol Bhosale on March 11, 2011 - 09:56

    How to add a file in WebApplication root folder?I want to add Global.asax file.

    • #2 by Johan Leino on March 11, 2011 - 10:33

      Do you mean that you want to alter the global.asax file that is included in SharePoint to use your implementation of it?

  2. #3 by Christophe on July 27, 2011 - 17:12

    In your second solution “App_GlobalResoures SPI approach”, how do you do to have on ly one resx file and put it either in the App_Global folder and in the 14/Resources ?

    When you write “You can of course keep that other mapped folder for your SharePoint specific resources”, do you mean you have 2 different resx file in your development ?

    Best regards,

    • #4 by Johan Leino on August 1, 2011 - 16:07

      Sorry for the late reply, I’ve been on vacation…
      That’s the problem, you can’t OOB. I guess you could write some code that takes the file in App_Global and copies it into 14/Resources.
      For your second question, yes I have 2 different resx files since the stuff that goes into App_Global tend to be UI centric (used by asp.net pages and such) and things in 14/Resources tend to be used by SP features and such.

  3. #5 by jared on September 15, 2011 - 15:31

    I am unclear as to why you would put something in the app_global resources location.

    No one has examples of displaying values from these files in some sort of control or page. How do you call it? What if you have a copy in the 14 hive and in app_global? The reason that I ask is because I was copying to both, then tried updating the app_global manually and realized when it didn’t work that it wasn’t being used at all. I could delete it and nothign happened. Infact, in all of the blog posts that I read, people mention both but never say how to access the values. It seems like app_global is a theory that one one uses at all. Am I missing something?

    • #6 by Johan Leino on September 16, 2011 - 07:45

      the app_global is the resource folder that the asp.net runtime uses, hence you would use it HTML markup (masterpages, pagelayouts, usercontrols)…like:

  4. #7 by Larkin Young on October 10, 2011 - 08:01

    Johan,

    Is this approach required if you want to utilize resource files from your custom SharePoint application pages, usercontrols, etc? What would be the syntax for using a resource value for, say, a Label control?

    • #8 by Johan Leino on October 12, 2011 - 16:47

      Have a look at this link at it will give you the details.

  5. #9 by Larkin Young on October 10, 2011 - 08:15

    Also, what should the feature be scoped to that deploys the App_GlobalResources? WebApplication?

    Thanks

    • #10 by Johan Leino on October 12, 2011 - 16:48

      Yes, it think so. The SPI will not be applicable to scopes that are not valid I think in the VS2010 SP template.

  6. #11 by rohan on March 19, 2012 - 12:31

    Good Article. Helped solve my problem of referencing resource files the asp.net way!!!

  7. #12 by Søren on February 21, 2013 - 07:26

    Great walk-through – helped me determine how to best package and provision resources in our project. Thanks, Johan!

  8. #14 by Nazir Ahmed on March 26, 2013 - 09:26

    Thanks a lot, it was very helpful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 305 other followers

%d bloggers like this: