Using the PropertyBag element to assign properties to Items in SharePoint 2010

Posted Thursday, February 11, 2010 3:43 PM by CoreyRoth

When the NDA dropped, I mentioned how you could use the new PropertyBag element in a feature to write values into the property bag of a site.  I already thought that was going to be incredibly useful, but it turns out the power of this new element is even greater than I first realized.  At the time I noticed that the PropertyBag element had a Url attribute but I didn’t understand what it was used for at the time.  Now I do.  As I mentioned yesterday, I have been experimenting with exporting sites as .wsp files and I discovered that you can use this to write to properties of an existing list item.  Take a look at the following example, that I grabbed from the file it generated when exporting my site.  This applies properties to default.master.

<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <PropertyBag Url="_catalogs/masterpage/default.master" ParentType="File" RootWebOnly="FALSE" HyperlinkBaseUrl="http://sp2010/Test" xmlns="http://schemas.microsoft.com/sharepoint/">

    <Property Name="vti_charset" Value="utf-8" />

    <Property Name="vti_candeleteversion" Value="true" />

    <Property Name="vti_modifiedby" Value="SHAREPOINT\system" />

    <Property Name="vti_cachedneedsrewrite" Value="false" />

    <Property Name="vti_level" Value="1" />

    <Property Name="vti_rtag" Value="rt:7CDE768C-FCE1-46E8-9165-FDA564419D9A@00000000002" />

    <Property Name="vti_cachedbodystyle" Value="&lt;body scroll=&quot;yes&quot; onload=&quot;BLOCKED SCRIPTif (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();&quot;&gt;" />

    <Property Name="vti_title" Value="&lt;asp:ContentPlaceHolder id=&quot;PlaceHolderPageTitle&quot; runat=&quot;server&quot;/&gt;" />

    <Property Name="UIVersion" Value="3" />

    <Property Name="vti_parserversion" Value="14.0.0.4536" />

    <Property Name="vti_cachedhastheme" Value="false" />

    <Property Name="vti_foldersubfolderitemcount" Value="0" />

    <Property Name="vti_hasdefaultcontent" Value="true" />

    <Property Name="vti_setuppathversion" Value="4" />

    <Property Name="vti_metainfoversion" Value="2" />

    <Property Name="vti_canmaybeedit" Value="true" />

    <Property Name="vti_generator" Value="Microsoft SharePoint" />

    <Property Name="vti_etag" Value="&quot;{7CDE768C-FCE1-46E8-9165-FDA564419D9A},2&quot;" />

    <Property Name="vti_cachedtitle" Value="&lt;asp:ContentPlaceHolder id=&quot;PlaceHolderPageTitle&quot; runat=&quot;server&quot;/&gt;" />

    <Property Name="vti_contentversion" Value="0" />

    <Property Name="vti_progid" Value="SharePoint.WebPartPage.Document" />

    <Property Name="vti_timelastmodified" Value="2010-02-10T16:12:34Z" />

    <Property Name="vti_sourcecontrolversion" Value="V1.0" />

    <Property Name="vti_timecreated" Value="2010-02-10T16:12:34Z" />

    <Property Name="vti_folderitemcount" Value="0" />

    <Property Name="vti_docstoretype" Value="0" />

    <Property Name="ContentTypeId" Value="0x01010500CE60EDAC92E8C942BFB82AF8AFC4A5F4" />

    <Property Name="vti_filesize" Value="29039" />

    <Property Name="vti_sourcecontrolcookie" Value="fp_internal" />

    <Property Name="vti_author" Value="SHAREPOINT\system" />

    <Property Name="vti_setuppath" Value="global\default.master" />

    <Property Name="vti_metatags" Value="GENERATOR Microsoft\ SharePoint progid SharePoint.WebPartPage.Document HTTP-EQUIV=Content-Type text/html;\ charset=utf-8 HTTP-EQUIV=Expires 0" />

    <Property Name="vti_replid" Value="rid:{7CDE768C-FCE1-46E8-9165-FDA564419D9A}" />

    <Property Name="vti_docstoreversion" Value="2" />

    <Property Name="vti_cachedcustomprops" Value="vti_title" />

  </PropertyBag>

</Elements>

As you can see the relative URL of the file is specified along with a separate attribute indicating the base URL it came from.  You also need to specify something for ParentType.  For now I know that you can specify File or Folder there (although I am guessing it might take Item or something like that).

Now for the most part all of these properties listed above are internal properties that you would never care about setting, but as you guessed you can specify your own properties as well.  The export site as template functionality does its best to export everything it can and that includes internal properties as well.  This is pretty interesting feature and I think it could remove the need to write a lot of code in my future.  @SPKyle might still want to write code to store properties on list items, but I will take a pass. :)

Comments

# Mechanical Properties and Performance of Engineering Ceramics and Composites III | Ceramic Engineering Addict

Pingback from  Mechanical Properties and Performance of Engineering Ceramics and Composites III | Ceramic Engineering Addict

# What's Your &quot;Why?&quot;

Friday, February 12, 2010 7:56 PM by What's Your "Why?"

Pingback from  What's Your &quot;Why?&quot;

# re: Using the PropertyBag element to assign properties to Items in SharePoint 2010

Tuesday, September 28, 2010 9:20 AM by Ahmad

Hey,

Can we retrieve the PropertyBag object of a web via sharepoint web serivces (out of the box).

# re: Using the PropertyBag element to assign properties to Items in SharePoint 2010

Wednesday, December 15, 2010 10:56 PM by SharePointKevin

Corey,

Thanks for the information. Can a property be set and then assigned a value via PowerShell?

What I need to do is create 8000 site collections and need the sites to have custom properties that are pulled and from .csv files and assigned as the site collection top level site is created.

The site creation I can do with PowerShell. But can I assign the property bag property values with PS?

# re: Using the PropertyBag element to assign properties to Items in SharePoint 2010

Thursday, December 16, 2010 10:51 AM by CoreyRoth

@Kevin I am thinking the answer is yes.  When you retrieve the site you effectively get a SPWeb object.  I think once you have that you can set the values you need to set.  I'd have to test it because I'm not quite sure of the syntax it would require.

Leave a Comment

(required)
(required)
(optional)
(required)