What is a proper way to store site-level global variables in a SharePoint site?
Asked Answered
V

3

6

One thing that has driven me nuts about SharePoint2007 is the apparent inability to have defineable settings that apply specifically to a site or site collection itself, and not the content. I mean, you have some pre-defined settings like the Site Logo, the Site Name, and various other things, but there doesn't appear to be anywhere to add new kinds of settings.

The application I am working on needs to be able to create multiple kinds of "project site collections" that all follow a basic template, but have certain additional settings that apply specifically to that site collection and that one alone. In addition to the standard site name we also need to define the Project Number, the Project Name, and the Client Name. And given the requests of some of our clients, we also reach a point where we have to have configurable settings that alter how some of the workflows work, like whether files are marked with Letters or Numbers.

Our current solution, which I'm hesitant about, has been to store an XML file on the SharePoint server. This file contains one node for each site collection, identified by the URL of the root site. Inside the node are all of the elements that need to be defined for that site collection. When we need them, we have to access the XML file (which will always require SPSecurity.RunWithElevatedPrivileges to access files right on the server) every time to load it and retrieve the data. There are a lot of automated processes which will have to do this, and I'm hesitant about the stability of this method when we reach hundreds of sites with thousands of files running tens of thousands of workflows, all wanting to access this file. Maybe they're unfounded worries, but I'd rather worry than risk everything breaking in a couple years.

I was looking into the SPWeb object and found the AllProperties hashtable. It looks like just the kind of thing which might work, but I don't know how safe it is to be modifying this. I read through both MSDN and the WSS SDK but found nothing that clarified on adding completely new properties into AllProperties. Is it safe to use AllProperties for this kind of thing? Or is there yet another feature that I am missing, which could handle the concept of global variables at the site collection or site scope?

Vicar answered 9/6, 2010 at 17:57 Comment(0)
C
5

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

MSDN - Managing Custom Configuration Options for a SharePoint Application

There is a production ready code available as part of the

MSDN - The SharePoint Guidance Library

See Hierarchical configuration manager

This gives you programmatic access to read/write these values. If you want to do this without using the guidance library then you would use something like the following code.

SPWeb web = SPContext.Current.Web;
if (web.Properties.ContainsKey("MyProperty"))
   string myProperty = web.Properties["MyProperty"];

If you want a UI to allow admins to easily set the values then use either SharePoint Designer (urghhhh!) or something like SharePoint Property Bag Settings

Caseose answered 10/6, 2010 at 12:28 Comment(2)
I wrote a custom application page for setting these values, so thankfully no SharePoint Designer. Thanks for the articles, it not only highlighted the options but also helped clarify the applicability of AllProperties versus Properties. This looks like the path to go!Vicar
More confusion for you - trentacular.com/2009/06/… Not sure I agree with the author though - I think Properties is the way to go - worth noting about the case sensitivity though.Caseose
V
1

The propertybag can be used to store key/value type properties. I think it is meant for what your purposes seem to be.

Personally I have more need for cross-site collection properties (such as db connection strings) and I use this for storing those.

Vestment answered 10/6, 2010 at 5:46 Comment(0)
D
1

I used http://pbs.codeplex.com/

Disreputable answered 10/6, 2010 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.