Tuesday, 19 November 2013

Property bag

It's basically a hashtable in SharePoint, it stores key - value pair that you can access in your code behind; you can install the following wsp from codeplex to view it from your site settings.

Property bag

once downloaded run this powershell to install it

Add-SPSolution -LiteralPath C:\Users\YOURUSER\Downloads\PropertyBagSettings2010.wsp

Install-SPSolution -Identity PropertyBagSettings2010.wsp -GACDeployment

with your property bag installed navigate to your site setting and notice that under Site Administration there's a new link Property bag settings

when clicked on, this will let you view, add, edit, and remove different entries, try not to wreck anything.

now this is where we like to connection strings, generally we add them on feature activating
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPWeb currentWeb = ((SPSite)properties.Feature.Parent).RootWeb)
    {
        if (!currentWeb.AllProperties.ContainsKey("spm_ConnectionString"))
        {
            currentWeb.AllProperties["spm_ConnectionString"] = SPM_CONNECTIONSTRING;
            currentWeb.Update();
            currentWeb.Properties.Update();
        }
    }

}
and remove them on feature deactivating
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    using (SPWeb currentWeb = ((SPSite)properties.Feature.Parent).RootWeb)
    {
        if (currentWeb.AllProperties.ContainsKey("spm_ConnectionString"))
        {
            currentWeb.AllProperties.Remove("spm_ConnectionString");
            currentWeb.Update();
            currentWeb.Properties.Update();
        }
    }

}

now keep in mind that SPM_CONNECTIONSTRING is just a constant string that as the name insinuates is a connection string.