Is it possible to define a global variable in SharePoint 2010 and then reference it in menus and server-side pages? We currently have a menu item on all 30+ web sites that points to the existing intranet home but will need to change it to point to the new one. Is it possible to set this to a variable defined in global.asax? Is there another way to change the menu item globally?

Thank you.

2 Answers 2


The recommended way to do this is to use PropertyBag (key/value pairs) through the Properties of SPFarm, SPSite.RootWeb (for root site of site collections), SPWeb, SPList etc (depending upon the scope that you need).

Managing Custom Configuration Options for a SharePoint Application

Update code example:

// ...inside web app feature activation code
SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

string existingValue = ... //get existing value from menu
webApp.Properties.Add("settingkey", existingValue);

// reference property later
string savedProperty = webApp.Properties["settingkey"];

Accessing SPWeb.Properties was just as easy except that I referenced the web application differently. Since the feature is scoped to the web, the Feature.Parent will be an SPWeb instance instead of SPWebApplication. Follow this pattern instead.

SPWeb web = (SPWeb)properties.Feature.Parent;
web.Properties.Add("settingkey", existingValue);
  • Thanks, Free Styler. Can you then use the variable to set a menu item? Or does it have to be done via code? Thanks.
    – Alex C
    Feb 1, 2012 at 14:28
  • You meant adding a menu item? Feb 1, 2012 at 14:52
  • Yes. We have an existing menu item called "Intranet Home" that points to our existing page but will need to be changed to another address. If we could set this to a variable and then just change that in one place, that would be our lives so much easier.
    – Alex C
    Feb 1, 2012 at 15:36
  • Alex, can you see the update code that you can use to save Global variable? Feb 1, 2012 at 16:02
  • 1
    Note that writing in Farm or WebApp scoped property bags require special Farm admin privileges.
    – Louis
    Jun 23, 2012 at 2:38

Another option is to use Chris O'Brien Config Store (in CodePlex). Essentially it is a SharePoint list that contains your variables and values that can be accessed programatically. It also deals with caching so it doesn't have to retrieve the value every time. There's some installation and configuration hassles to be aware of (like modifying web.config ) but generally it works perfectly well. The clear advantage here is the user experience with access to the SharePoint list to perform CRUD operations on the variables.

Check it out in Code Plex here. And you know it is quality as Chris O'Brien is a legend in the SP space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.