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

大憨熊 提交于 2019-11-28 12:22:00

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!