Skip to main content

Weblog Ton Stegeman [MVP]

Go Search
Home
  

ODC 2008
If you have a question or suggestion, please contact me through Windows Live Messenger.
My status: .

If I am not online, please send me an e-mail.
Weblog Ton Stegeman [MVP] > Posts > Setting site properties in a SharePoint 2007 site definition by using a feature
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 Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES”. 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 siteproperties\feature.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 Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates”, and give it a new name.

·         Register the new site template in WEBTEMPSPS.XML (in folder “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\XML”). 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:

image001

Comments

There are no comments yet for this post.
Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Your city *


Type the name of the city you live in (making it easier to handle spam...)

CurrentDate *

Select the current date (see if this gives me fewer spam...)
Attachments

 Links

  SharePoint Object on CodePlex
  Screencast introducing SharePoint Objects
  Content by Type and Filter Web Parts on CodePlex
  Archive
  Archive (Calendar)

 My Latest Blog Posts

Scripting SharePoint 2007 setup: choices and conceptsUse SHIFT+ENTER to open the menu (new window).
Adventures in Visual Studio 2010: Migrate the Content By Type web part to SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Register SharePoint themes by using a featureUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 development on Windows 2008 Server R2 – Getting StartedUse SHIFT+ENTER to open the menu (new window).
New release SharePoint Objects: features and groupsUse SHIFT+ENTER to open the menu (new window).
Constructing the url to the SharePoint Edit Permissions pageUse SHIFT+ENTER to open the menu (new window).
Screencast: introduction to SharePoint ObjectsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 and Reporting ServicesUse SHIFT+ENTER to open the menu (new window).
SharePoint Objects – Insight in usage of your SharePoint artifactsUse SHIFT+ENTER to open the menu (new window).
SharePoint 2007 Custom list schema and the Content Query Web PartUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010 Silverlight Client Object Model – ExecuteQuery vs ExecuteQueryAsyncUse SHIFT+ENTER to open the menu (new window).
SharePoint 2010, the Client Object Models and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Having fun with SharePoint 2010, Silverlight 3 and Bing MapsUse SHIFT+ENTER to open the menu (new window).
Connecting TFS 2010 projects to SharePoint sitesUse SHIFT+ENTER to open the menu (new window).
Adding a database to the SharePoint database Server using SPDatabaseUse SHIFT+ENTER to open the menu (new window).