Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 10 down vote accepted

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);
share|improve this answer
    
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 '12 at 14:28
    
You meant adding a menu item? –  Falak Mahmood Feb 1 '12 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 '12 at 15:36
    
Alex, can you see the update code that you can use to save Global variable? –  Falak Mahmood Feb 1 '12 at 16:02
    
Thanks, Free Styler. Your help is much appreciated :) –  Alex C Feb 2 '12 at 6:07

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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