Tuesday, February 3, 2009

Writing to Property bag using Feature

Scenario:
Solution:
As SiteCollection ( SPSite object ) does not have a property bag. I created a feature for Scope=Site and updated the property bag for the RootWeb.
Code:

<Feature
Id="{New GUID}"
Title="Updates Web Properties."
Description="This will make necessary changes to the site collection root web properties."
Scope="Site"
ReceiverAssembly="Four-part assembly name"
ReceiverClass="Training.UpdateWebPropertiesReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/" >
<!-- no declaratvie elements required -->
<ElementManifests />
<Properties >
<Property Key ="Key1" Value ="Value1"/>
<Property Key ="Key2" Value ="Value2"/>
</Properties>
</Feature>

Code (UpdateWebPropertiesReceiver.cs) :
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Training
{
public class UpdateWebPropertiesReceiver : SPFeatureReceiver
{
public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }
public override void FeatureActivated(SPFeatureReceiverProperties properties) {

try {
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.RootWeb)
{
// Set FeatureProperties to Web Property Bag
SetFeaturePropertiesToPropertyBag(properties.Feature.Properties, web);
}
}
catch {
throw;
}
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {

try {
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.RootWeb)
{
// Reset FeatureProperties to Web Property Bag
ResetFeaturePropertiesToPropertyBag(properties.Feature.Properties, web);
}
}
catch
{
throw;
}
}

private void SetFeaturePropertiesToPropertyBag(SPFeaturePropertyCollection featurePropertyCollection, SPWeb web)
{
// Loop through each property in the collection
foreach (SPFeatureProperty featureProperty in featurePropertyCollection)
{
// Validate the name and value are populated
if ((!string.IsNullOrEmpty(featureProperty.Name)) && (!string.IsNullOrEmpty(featureProperty.Value)))
{
// Check if the property name already exists
if (web.Properties.ContainsKey(featureProperty.Name))
{
// Update property with new value
web.Properties[featureProperty.Name] = featureProperty.Value;
}
else
{
// Add new property with name and value
web.Properties.Add(featureProperty.Name, featureProperty.Value);
}
}
}

// Update PropertyBag
web.Properties.Update();
}

private void ResetFeaturePropertiesToPropertyBag(SPFeaturePropertyCollection featurePropertyCollection, SPWeb web)
{
// Loop through each property in collection
foreach (SPFeatureProperty featureProperty in featurePropertyCollection)
{
try
{
// If the property Name exists, try to set it's corresponding propertybag value to ""
if (!String.IsNullOrEmpty(featureProperty.Name))
{
web.Properties[featureProperty.Name] = string.Empty;
}
}
catch
{
}
}
web.Properties.Update();
}
}
}

1 comments:

Suresh Pydi May 29, 2013 at 12:47 AM  

Nice post. Here is one more post explaining to get and set property bag values in SharePoint 2013 apps using CSOM

http://sureshpydi.blogspot.in/2013/05/set-and-get-property-bag-values-in.html