Search This Blog

Tuesday, March 17, 2015

Save the value in sharepoint property bags


In this article we will see the sharepoint property bag CRUD operation at different access level( farm level , Web application level , Site Collection level and web level )

Add the property farm level
---------------------------------
SPFarm farmObject = SPFarm.Local;
farmObject .Properties.Add("PropertyKey", "PropertyValue");
farmObject .Update();

Get the property farm level
------------------------------------
SSPFarm farmObject = SPFarm.Local;
if (farmObject.Properties != null && farmObject.Properties.Count > 0)
{
if (farmObject.Properties.ContainsKey("PropertyKey"))
{
String propertyValue = myWebApplication.Properties["PropertyKey"].ToString();
}
}

Change the property farm level
------------------------------------
SPFarm farmObject = SPFarm.Local;
if (farmObject.Properties != null && farmObject.Properties.Count > 0)
{
if (farmObject.Properties.ContainsKey("PropertyKey"))
{
farmObject.Properties["PropertyKey"] = "New Property Value";
        farmObject.Update();
}
}

Remove the property farm level
-------------------------------
SPFarm farmObject = SPFarm.Local;
if (farmObject.Properties != null && farmObject.Properties.Count > 0)
{
if (farmObject.Properties.ContainsKey("PropertyKey"))
{
farmObject.Properties["PropertyKey"] = null;
        farmObject.Properties.Remove("PropertyKey");
        farmObject.Update();
}
}

Add the property web application level
--------------------------------------
SPWebApplication webApplicationObject = SPWebApplication.Lookup(new Uri("http://YourWebApplicationURL"));
webApplicationObject.Properties.Add("PropertyKey", "PropertyValue");
webApplicationObject.Update();

Get the property web application level
--------------------------------------
SPWebApplication webApplicationObject = SPWebApplication.Lookup(new Uri("http://YourWebApplicationURL"));
if (webApplicationObject.Properties != null && webApplicationObject.Properties.Count > 0)
{
if (webApplicationObject.Properties.ContainsKey("PropertyKey"))
{
String propertyValue = webApplicationObject.Properties["PropertyKey"].ToString();
}
}

Change the property web application level
-------------------------------------------
SPWebApplication webApplicationObject = SPWebApplication.Lookup(new Uri("http://YourWebApplicationURL"));
if (webApplicationObject.Properties != null && webApplicationObject.Properties.Count > 0)
{
if (webApplicationObject.Properties.ContainsKey("PropertyKey"))
{
webApplicationObject.Properties["PropertyKey"] = "New Property Value";
webApplicationObject.Update();
}
}

Remove the property web application level
-----------------------------------------
SPWebApplication webApplicationObject = SPWebApplication.Lookup(new Uri("http://YourWebApplicationURL"));
if (webApplicationObject.Properties != null && webApplicationObject.Properties.Count > 0)
{
if (webApplicationObject.Properties.ContainsKey("PropertyKey"))
{
webApplicationObject.Properties["PropertyKey"] = null;
webApplicationObject.Properties.Remove("PropertyKey");          
webApplicationObject.Update();
}
}

Add the property site level
----------------------------
using(SPSite site = new SPSite("http://server:port"))
{
    SPWeb rootWeb = site.RootWeb;  
    rootWeb.Properties.Add("PropertyKey", "PropertyValue");//In sharepoint there is no any provision to store the property value in SPSite object so we can use the RootWeb object
    rootWeb.Properties.Update();        
}

Get the property site level
--------------------------------------
using(SPSite site = new SPSite("http://server:port"))
{
    SPWeb rootWeb = site.RootWeb;  
if (rootWeb.Properties != null && rootWeb.Properties.Count > 0)
{
if (rootWeb.Properties.ContainsKey("PropertyKey"))
{
String propertyValue = rootWeb.Properties["PropertyKey"].ToString();    
}
}
}

Change the property site level
--------------------------------------
using(SPSite site = new SPSite("http://server:port"))
{
    SPWeb rootWeb = site.RootWeb;  
if (rootWeb.Properties != null && rootWeb.Properties.Count > 0)
{
if (rootWeb.Properties.ContainsKey("PropertyKey"))
{
rootWeb.Properties["PropertyKey"] = "New Property Value";
rootWeb.Update();  
}
}
}

Remove the property site level
-----------------------------------------
using(SPSite site = new SPSite("http://server:port"))
{
    SPWeb rootWeb = site.RootWeb;  
if (rootWeb.Properties != null && rootWeb.Properties.Count > 0)
{
if (rootWeb.Properties.ContainsKey("PropertyKey"))
{
rootWeb.Properties["PropertyKey"] = null;
rootWeb.Properties.Remove("PropertyKey");            
rootWeb.Update();  
}
}
}

Add the property web level
----------------------------
using(SPSite site = new SPSite("http://server:port"))
{
    using(SPWeb web = site.OpenWeb())
{   
web.Properties.Add("PropertyKey", "PropertyValue");
web.Properties.Update();        
}
}

Get the property web level
--------------------------------------
using(SPSite site = new SPSite("http://server:port"))
{
    using(SPWeb web = site.OpenWeb())
{
if (web.Properties != null && web.Properties.Count > 0)
{
if (web.Properties.ContainsKey("PropertyKey"))
{
String propertyValue = web.Properties["PropertyKey"].ToString();    
}
}
}
}

Change the property web level
--------------------------------------
using(SPSite site = new SPSite("http://server:port"))
{
    using(SPWeb web = site.OpenWeb())
{
if (web.Properties != null && web.Properties.Count > 0)
{
if (web.Properties.ContainsKey("PropertyKey"))
{
web.Properties["PropertyKey"] = "New Property Value";
web.Update();  
}
}
}
}

Remove the property web level
-----------------------------------------
using(SPSite site = new SPSite("http://server:port"))
{
    using(SPWeb web = site.OpenWeb())
{
if (web.Properties != null && web.Properties.Count > 0)
{
if (web.Properties.ContainsKey("PropertyKey"))
{
web.Properties["PropertyKey"] = null;
web.Properties.Remove("PropertyKey");            
web.Update();  
}
}
}
}

No comments:

Post a Comment