Setting site properties in a SharePoint 2007 site definition by using a feature

In SharePoint 2007 ‘features’ are new. They are a great way to add all sorts of things to your SharePoint environment. You can use them for example to create content types or site columns (see my previous post). It is also possible to activate a feature from the site definition. I have used this to create a feature that stores properties for the site in the properties (SPPropertyBag) of the site. We can now set these properties while creating the site, directly from the site definition. And the good news: it is extremely easy!

 

Please note the code below is tested on MOSS 2007 beta2. It is not guaranteed to work on later builds.

 

Step 1 – Create a SPFeatureReceiver

·         Create a class library project in Visual Studio 2005.

·         Add a reference to the assembly Microsoft.SharePoint.

·         Sign the assembly.

·         Add a class to the project and let it inherit from SPFeatureReceiver (in the Microsoft.SharePoint namespace)

·         Implement these methods:

o   FeatureActivated

o   FeatureDeactivating

o   FeatureInstalled

o   FeatureUninstalling

·         In my receiver I have only implemented FeatureActivated. This method gets a parameter of the SPFeatureReceiverProperties  type. This parameter has a reference to the site (SPWeb) that you are creating in the Feature.Parent property (see code sample below). The code then simply walks through the properties of the feature and adds them to the propertybag.

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

    SPWeb web = (SPWeb)properties.Feature.Parent;

    bool updated = false;

    foreach (SPFeatureProperty featureProperty in properties.Feature.Properties)

    {

        if ((!string.IsNullOrEmpty(featureProperty.Name)) && (!string.IsNullOrEmpty(featureProperty.Value)))

        {

            if (web.Properties.ContainsKey(featureProperty.Name))

            {

                web.Properties[featureProperty.Name] = featureProperty.Value;

            }

            else

            {

                web.Properties.Add(featureProperty.Name, featureProperty.Value);

                updated = true;

            }

        }

    }

    if (updated) web.Properties.Update();

}

·         Build the assembly

·         Add the assembly to the GAC

 

Step 2 – Create and install a new feature

·         Create a new folder for your new feature in the folder “C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATEFEATURES”. I called this folder “SiteProperties”.

·         Add a feature.xml file to this folder, and configure your feature:

o   Make sure it has a unique ID (a guid)

o   Set the title and description

o   Make sure the scope is “Web”. This feature can only be used in a web, and not in other scopes.

o   Set the references to your assembly and receiver class from step 1.

o   My xml looks like this:

<Feature

  Id="C15B7790-A6B7-444D-BACF-5C797A4E1F33"

  Title="Site Properties"

  Description="Set per-site properties in the property bag of the site."

  Version="1.0.0.0"

  Scope="Web"

  Hidden="TRUE"

  ReceiverAssembly="Eog.OfficeServer.Features, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fef402cb2da0e9fa"

  ReceiverClass="Eog.OfficeServer.Features.SiteProperties"

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

</Feature>

·         Install your feature using STSADM:

stsadm -o installfeature -filename sitepropertiesfeature.xml

 

Step 3 – Create a Site Definition

To create a new Site Definition, I started with a copy of the “Publising and Team Collaboration Site” template:

·         Copy the folder PUBLISHING in “C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATESiteTemplates”, and give it a new name.

·         Register the new site template in WEBTEMPSPS.XML (in folder “C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATE1033XML”). This is the same process as registering a new site definition in SharePoint 2003.

·         Do an IISRESET and test if you can create a new site based on the new site definition (at the moment it is just a copy, to make sure we registered it properly)

 

Step 4 – Activate your feature from the site definition.

The last step is to activate your feature from the definition:

·         Open the ONET.XML file in your site definition

·         One of the “Configuration” sections contains a XML node called “WebFeatures”.

·         Add a new “Feature” childnode to that node and set the properties. This ID for the feature is the same as the “Id” in the feature.xml file. This is my xml:

<Feature ID="C15B7790-A6B7-444D-BACF-5C797A4E1F33">

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

    <Property Key="NavigationParent" Value="http://tst2007/Reports"/>

    <Property Key="NavigationExpand" Value="3"/>

  </Properties>

</Feature>

·         Save the XML file and do an IISRESET

 

Step 5 – Test

When you create a new site based on this site definition, this site will have 2 extra properties stored in the property bag. I have created a very simple webpart that just reads these 2 properties and writes them in the page. And here’s the result:

 

Leave a Reply