Thursday, August 27, 2015

SHAREPOINT PROPERTY BAGS

Property bag is a feature available in Windows SharePoint Services 3.0. Its nothing but a hash table of Key-Value pairs. It allows to add properties to objects in a SharePoint site.

image
SharePoint Property Bag allows to store configurations settings at different levels of the SharePoint hierarchy outside of the application itself. Property bag is basically a hash table of key-value pair options. Property bag feature is available in Windows SharePoint services 3.0, SharePoint 2010 and SharePoint 2013. Property bag helps you to store meta data as key-value pairs example Connection Strings, file paths, server names and other settings in SharePoint application. This is something like app.config information in classic ASP.NET. Property bags can be created / Modified / Deleted from Sharepoint designer, using Object model or using http://pbs2010.codeplex.com/ access Property bags from Central Administration.
Advantages of using Property bag element
1. The SharePoint Property Bag Settings includes a hierarchical configuration manager that can safely store and retrieve configuration settings at the following levels:
*Farm (SPFarm class)
*Web application (SPWebApplication class)
*Site collection (SPSite class)
*Site (SPWeb class)
*List (SPList class)
2. Import and Export Property bags
3. Access Property bags from Central Administration or Site Settings
4. Encrypting property bag value
Using SharePoint designer :
You can use SharePoint designer for getting the Property bag settings.
1. Open a site in SharePoint Designer
2. Go to Site menu click on Site options
3. Site Settings dialog box opens
4. Click on Parameters tab where you can see the list of existing properties
from the same place you can even perform add/modify/delete operations.
Using Central Admin :
There is no out of the box user interface available for setting and reading the property bag values, however you can download this Property Bag util from  http://pbs2010.codeplex.com/releases/view/46201 to have the interface in central admin for setting the property bag values. This SharePoint Property Bag Settings is a reusable component that you can include in your own SharePoint applications. It can store simple types, such as integers or strings, as well as any type that can be serialized to XML.
Using SharePoint object model :
using (SPSite RootSite = new SPSite(URL))
            {
                using (SPWeb web= RootSite.OpenWeb())
                {                   
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                       // Get Property bag
                        if (web.AllProperties.ContainsKey("SiteID"))
                        {
                            var data = web.AllProperties["SiteID"].ToString();
                        }                       
                        // Set Property bag
                        web.Properties["SiteID"] = "GUID";
                        web.Properties.Update();
                        web.AllowUnsafeUpdates = false;                       
                    }
                    catch (Exception ex)
                    {
                      //Throw Exception 
                    }          
                }
            }


Why to Use SharePoint Property Bag

The Property Bag hash table for a site can store any metadata as Key-Value pairs such as connection strings, server names, file paths, and other settings needed by your SharePoint application. Most of the time we will store the above settings in configuration file, which is common to the entire web application. If there is any setting specific each and individual sites in the web application, then we have maintain that many entries in the config file. To over come the above scenario we can use the SharePoint Property Bag.
There is no specific out of box user interface available to set or to read the property bag settings. In WSS 3.0 property bag values has to set/get using the object model. There is an option available in SharePoint designer to set/get the property bag settings. Go to Site -> Site Settings. click on the Parameters tab.  On this tab, you will be able to manipulate of all of your custom property bag values.
image
image
How to Use
SPSecurity.RunWithElevatedPrivileges(delegate()
        {
        try
        {
            using (SPSite RootSite = new SPSite(URL))
            {
                using (SPWeb SiteCollection = RootSite.OpenWeb())
                {                    
                    try
                    {
                        SiteCollection.AllowUnsafeUpdates = true;
                       // Get connection string from Property bag
                        if (SiteCollection.AllProperties.ContainsKey("ConnectionString"))
                        {
                            ConnectionString = SiteCollection.AllProperties["ConnectionString"].ToString();
                        }                        
                        // Set siteID in the Property bag
                        SiteCollection.Properties["siteID"] = siteID;
                        SiteCollection.Properties.Update();
                        SiteCollection.AllowUnsafeUpdates = false;                        
                    }
                    catch (Exception ex) 
                    { 
                      //Handle Exception  
                    }           
                }
            }
        }
        catch(Exception ex)        
        {            
        }
        });
Examples


The following example is a console application that accesses the Properties property, iterates through the collection, and prints each key/value pair to the console.
using System;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPPropertyBag props = web.Properties;
                    foreach (DictionaryEntry de in props)
                    {
                        Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
                    }
                }
            }
            Console.ReadLine();
        }
    }
}
The output that this application prints to the console varies with the website, but it might look like the following:
Key = vti_associatemembergroup, Value = 5
Key = vti_extenderversion, Value = 14.0.0.4016
Key = vti_associatevisitorgroup, Value = 4
Key = vti_associategroups, Value = 5;4;3
Key = vti_createdassociategroups, Value = 3;4;5
Key = vti_siteusagetotalbandwidth, Value = 547
Key = vti_siteusagetotalvisits, Value = 9
Key = vti_associateownergroup, Value = 3
Key = vti_defaultlanguage, Value = en-us

No comments:

View Tenant (ULS) Logs in SharePoint Online using CSOM

Even though the classes exist in the CSOM, the Microsoft Office 365 Engineering team has confirmed that this is something which is not poss...