PowerShell Set Property Bag to SharePoint

Sometimes we want to set some configuration or wants to hard code :) some values in a solution. For the same we have some several ways and Property Bag is one of them. 

Below is the PowerShell script to set property bag

clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue 
Add-PSSnapin Microsoft.SharePoint.Powershell
$farm=Get-SPFarm
$propKey = "KeyName"
$propValue = "Value"
if($farm.Properties.Containskey($propKey))
{
    $farm.Properties.Remove($propKey)
}

$farm.Properties[$propKey] = $propValue
$farm.Update()


We have set property bag value. But now how we can access the same from our solution. So below is the code to get property bag values by key.

               SPFarm farm = SPFarm.Local;
                string propertyBagKey= "KeyName";
                string propertyBagValue= string.Empty;

                if (farm != null
)
                {
                    if (farm.Properties[
propertyBagKey] != null)
                    {
                        
propertyBagValue= Convert.ToString(farm.Properties[propertyBagKey]);
                    }
                }

                return 
propertyBagValue;




Comments

Popular posts from this blog

C# Copy files from one server to another

Suppress StyleCop SA1600

Telerik Rad Grid Sorting