in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Ton Stegeman [MVP] weblog

I have moved my blog to http://www.tonstegeman.com/blog. If you have a blogpost at sharepointblogs that does not display all the content on the right hand side, please go to the new blog. It has all the posts that are on this blog as well. I you have any feedback, please send me a message through the contact form.

June 2006 - Posts

  • Adding site columns to Office SharePoint Server by using Features

    As described in the previous item, one of the ways to deploy a site columns is by using Features. Features are another important new feature J in MOSS 2007. They are an easy way to deploy changes to a SharePoint environment. You can deploy site columns, content types, assemblies, webpart, menu items and a lot more. This article will not talk about features, but is using features to deploy a site column.

     

    Please note that the code samples you will see are based on a beta version of SharePoint 2007. The actual syntax can be different in later builds of the product.

     

    Features are deployed as files on the server. The can be found in the “FEATURES” folder, which is a subfolder of the “TEMPLATE” folder. In this FEATURES folder, I have created a new subfolder called “fieldstst”. In this folder are a number of files.

     

    Feature.xml

    The feature.xml file is a general description of the feature and looks like this:

     

    <Feature  Id="CA7BD552-10B1-4563-85B9-5ED1D39C073B"
      Title="TSTFields"
      Description="Fields added through features by Ton Stegeman"
      Version="12.0.0.0"
      Scope="Site"
      xmlns=http://schemas.microsoft.com/sharepoint/>
      <ElementManifests>
        <ElementManifest Location="fieldstst.xml"/>
      ElementManifests>
    Feature>

     

    The most important setting here are the Scope, that sets the scope for the feature. This Scope determines the availability of the feature. I have set it to “Site”, which makes my sitecolumns available at the site level. The Location attribute of the ElementManifest element is a reference to the file that has the actual definition of my site column.

     

    Site column feature xml

    The file fieldtst.xml looks like this:

     

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Field ID="{1DAB9B48-2D1A-47b3-878C-8E84F0D322CB}"
        Name="RegionFromFeature"
        Group="TST_Columns"
        Type="Choice"
        DisplayName="Region"
        SourceID=http://schemas.microsoft.com/sharepoint/v3/fields
        StaticName="RegionFromFeature"
        FillInChoice="FALSE">
        <CHOICES>
          <CHOICE>GlobalCHOICE>
          <CHOICE>EuropeCHOICE>
        CHOICES>
        <Default>GlobalDefault>
      Field>
    Elements>

     

    I think this XML does not need a very detailed explanation. If you are used to working with site definitions and list templates in SharePoint/WSS 2003, you will recognize the syntax.

     

    Installation

    The actually use the site column, the feature must be installed and activated. This is done in 2 steps by using STSADM:

     

    The first step is to install the feature. In my case this will be like this:

     

    stsadm -o installfeature -filename fieldstst\feature.xml

     

    The second step is to activate the feature:

     

    stsadm -o activatefeature -filename fieldstst\feature.xml -url http://tonportal

     

    After activating, you need to do an IISRESET, and the site column should be available. In my case it is only available in the “tonportal” portal.

     

  • CMS and SharePoint together in MOSS 2007: using SharePoint metadata in webcontent.

    In Microsoft Office SharePoint Server 2007, CMS is now integrated into SharePoint. I really like the way that has been done. Webpages are basically documents in a document library. This document library is called “Pages” and is available in every Office Server (not WSS) site definition. This document library contains a number of different contenttypes. They add something like 20 columns to the document library. All content in the webpages is stored in these columns. You can easily see this by creating a new site and add some content to the page. Then click on the “Pages” link in the breadcrumb. This takes you to the document library that contains an item “default.aspx” (see screenshot below).

    This is just a normal SharePoint item that has the normal dropdown to take you to “View Properties”. This page shows you all the metadata of the default.aspx document, including the content you entered. Nice!!!

    What I also like is that it is now very easy to mix “CMS”  placeholders with “SharePoint” webpartzones in the same page. You do this in the PageLayout.

    Because the “Pages” library is just a normal SharePoint list, you can do easily add your own metadata to that list. In the steps below, I will show how you then can add webcontrols to the PageLayout. By doing this your content editors can edit the metadata for the page, while they are editing the content for the page.

    Step 1 – Add metadata
    I have created 2 sitecolumns at the Homepage of the portal. One of them (industry) is a multivalued lookup to a list called “Industries”. The other is a choicefield. I have created them as sitecolumns, but you can also use contenttypes, or add them directly to the “Pages” library. The image below shows the columns on my Pages library, including my custom fields.

    Step 2 – Create a new Page Layout

    • Start SharePoint Designer and open your site.
    • Navigate to “_catalogs” and then to “masterpage”. This is the Master Page Gallery that has the master pages and all Page Layouts.
    • Copy one of the exiting Page Layouts. (I used ArticleLeft.aspx)
    • Rename the new PageLayout and give it a proper description
    • Open the page layout in SharePoint Designer
      The placeholder named “PlaceHolderMain” in my case already had 2 webcontrols for adding metadata to the page, ArticleStartDate and ArticleByLine. I added 2 extra webcontrols just below these controls, to show the Industry and Region fields. The Industry field (which is a multi value lookup) is added like this:

      <SharePointWebControls:MultipleLookupField runat="server" id="IndustriesField" FieldName="Industry"/>

      And the region field is added using this line:

       <SharePointWebControls:DropDownChoiceField runat="server" id="RegionField" FieldName="Region"/>

      In the WSS3 SDK you can find more information about these  WebControls and all the others that are available.You can put them in any placeholder in the page layout. I put them here, because readers who access the page then see the values we have selected at the top of the page. The Title field is an example of a control that is placed in a different placeholder in the same page.
    • Save the Page Layout (and don’t forget to publish and approve it if you want to make it available to all content editors.)

    Step 3 – Create a new page

    • Navigate to a Site (not based on any WSS site template)
    • Select Site Actions and Create Page
    • Select the page layout we just created.
    • Your page now should look like this:
    • After editing content and saving the page, it should look like this:

    As we have seen it is very easy to use custom metadata in your webpages. I think this is very powerful. It makes it very easy for content editors to maintain metadata on their webpages. And if you run a keyword search for content about a specific industry, the search will now return both documents and webpages. Combined with SiteColumns and ContentTypes, this make content management a lot easier.

  • 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:

     


Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts