Getting Values from SharePoint 2013 Property Bags

SharePoint 2013 has a hierarchical set of property bags. At each of the following levels in a SharePoint farm, one can store properties and their corresponding values:

  • Farm
  • Web Application
  • Site
  • Web
  • List

This can be extremely handy when writing code. For example, if you have four different environments (Development, QA, Staging, Production), you could store properties in each environment's farm property bag that specifies things like connection strings, debugging constants, or host URLs.

It's even handier when you have a simple means to extract these properties from the property bag in your code. I generally make a class (FarmProperty in the example below) with a "Get" method that extracts the string value for the given property.

    public class FarmProperty
    {
        public static string Get(string name)
        {
            string value = null;
            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                var farm = SPFarm.Local;
                foreach (var prop in farm.Properties.Cast().
Where(prop =>; prop.Key.Equals(name)))
                {
                    value = prop.Value.ToString();
                }
            });
            return value;
        }
    }


To use this code, just call the Get method on the FarmProperty class. If the property doesn't exist, it will return null.

    var reportServerHost = FarmProperty.Get("ReportServerHost");

Yorum Gönder

0 Yorumlar

Ad Code

Responsive Advertisement