only My site

Monday, January 21, 2013

Property Bag - Scopes

Property bags are place to store metadata or properties.The property bags are implemented as hash table consisting of property names and values.

Property bags can be defined at following levels:
  • Farm (SPFarm class)
  • Web application (SPWebApplication class)
  • Site collection (SPSite class)
  • Site (SPWeb class)
  • List (SPList class) - List,  List item, File, Folder
Specifies a custom property and its value for a property bag of a List item, File, Folder, or Website.

The following is a list of the elements in the Property Bag schema.
<Elements>
<PropertyBag>  - Must be one of File, Folder, ListItem, or Web. Specifies the kind of object to which the property bag belongs.
<Property> - Types are int, string or DateTime.

The example is available at http://msdn.microsoft.com/en-in/library/gg491705.aspx

The following example shows how to add a Location property and its value to the property bag of a website that represents a field office.
<PropertyBag HyperlinkBaseUrl="http://contoso.com/sites/EastEnglandSalesOffice"" Url="" ParentType="Web" RootWebOnly=TRUE" />
    <Property Name="Location" Value="London" Type="string" />
</PropertyBag>
The following example shows how to add a DaysToLive property and its value to the property bag of a list item that represents a temporary price discount. The property specifies how many days the twenty-third list item should be retained before it is deleted from the list. Note that the value of the Type attribute must be a member of the PropertyBagType enumeration. Hence, in this case it must be "int", not "integer".
<PropertyBag HyperlinkBaseUrl="http://contoso.com/sites/EastEnglandSalesOffice"" Url="Lists/CurrentDiscounts" ParentType="ListItem" ItemIndex="23"/>
    <Property Name="DaysToLive" Value="90" Type="int" />
</PropertyBag>

Property bags can be set using SharePoint designer and programmatically.

Set and Get using SharePoint Designer 2010
  • Open Site in SharePoint Designer 2010
  • Go to Site->Site Options
  • Click on Parameters tab where you can see the list of existing properties, from the same place you can even perform add/modify/remove actions.

Set and Get Programmatically
To Read the Set Key:
SPSite siteCollection = new SPSite("");
SPWeb website = mySite.RootWeb;
string MyValue = website.AllProperties["KeyName"]);

To Set the Value
SPSite siteCollection = new SPSite(" ");
SPWeb website = mySite.RootWeb;
website.Properties.Add(" KeyName ", "KeyValue");
website.Properties.Update

No comments: