Dedicated to development, configuration, and tips and tricks for both WSS 3.0 and MOSS

Creating Site Columns using Features

Posted by Brian LaSitis on Thursday, 26 Feb 2009 02:07

Nearly every SharePoint project that involves some type of content management, whether it be using document libraries, custom lists, or published web content, we generally find ourselves needing to create one or more content types to deliver the designed solution to a client.  Up until this point, most often we have taken the most basic approach to creating content types in the past – performing the task through the built-in SharePoint UI.  While this methodology works, it can almost be thought of as the brute-force approach.  I make this comparison because while the built-in UI offers a working method for creating content types, and the underlying site columns that they are made up of, it lacks in providing any means to take those site columns and content types and move them elsewhere, or apply them to multiple site collections.

Another approach to building content types and site columns to leverage the built-in Features framework within SharePoint 2007.  While this method isn’t as glitzy, as it involves creating XML files instead of just pointing & clicking, it offers a much more portable, extensible deliverable.  Instead of going to a client and just manually building out the same site columns and content types in each & every site collection they require, we can create a single SharePoint feature that contains these definitions, and then activate it on each site collection after it is created, or take it one step further, and create a custom site definition based upon the site template the client wants to use for their sites, and configure the feature within it to be activated automatically upon site creation, or leverage the feature stapling framework as another approach to automatically activating this feature for new sites – what could be more streamlined, and error-proof than that?

My intent is for this posting to cover simply the process of creating a feature for defining site columns, since these are the fundamental building blocks of content types, then in forthcoming posts, discuss the process of bringing these site columns together into one or more content types, and finally how to take the built-in Team site definition, create a duplicate of it, and add our custom feature to it, to illustrate automatic feature activation. 

A SharePoint feature, is a module of functionality that can be enabled at specific scopes within a SharePoint farm, namely the farm level, web application level, site collection level, and site level.  SharePoint itself uses features for nearly everything it provides out of the box – the standard list definitions, the built-in site columns & content types, built-in web parts, and they are even used to define what is displayed on the Site Settings page and the Site Actions menu.  Each of the features, both built-in and custom ones, live within the “12” hive at 12\TEMPLATE\FEATURES, and each is contained within its own unique folder, whose name reflects the feature’s purpose.  Within this folder there is one and only one required file, whose name must be feature.xml.  This file defines the basic characteristics of the feature including its ID, name, description, activation scope, and visibility.  Additionally this XML file can define additional information that is specific to the feature itself, such as a receiving assembly, and potentially one or more element manifests.  Each element manifest that is defined is represented by an additional XML file that is also contained within the feature’s folder, and within these files is where the uniqueness of the feature comes out, as they can be used to define the set of files that the feature is going to inject into a given site, or the custom actions the feature will add to the Site Actions menu, or the site columns and/or content types that the feature will add to its targeted site, just to name a few.  Finally, in addition to the element manifest XML files, the feature’s folder can contain any number of other files and folders that are needed for the feature itself, based upon its intended purposes.

For our example feature, we will start with just two XML files in our feature, namely the required feature.xml file, and a single element manifest named AcmeSiteColumns.xml.  Let us begin by reviewing the contents of our feature.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<Feature  Id="A5DA673C-FF79-427c-BBB4-422937F82FCB"
          Title="Acme Content Types"
          Description=""
          Version="12.0.0.0"
          Scope="Site"
          Hidden="FALSE"
          xmlns="http://schemas.microsoft.com/sharepoint/">
    <ElementManifests>
        <ElementManifest Location="AcmeSiteColumns.xml" />
    </ElementManifests>
</Feature>

A couple of the more important points about this to highlight are:

  • Id – this must be a unique GUID for your feature.  New GUIDs  can be generated within Visual Studio using the Tools, Create GUID menu option.
  • Title – this is the display name of the feature, as it will show up within SharePoint.
  • Description – this is the descriptive text that will show up within SharePoint for the feature, on the listing page.
  • Scope – this can be set to 1 of 4 possible values – Farm, WebApplication, Site (site collection), Web (site).
  • We have one element manifest defined, using a filename of AcmeSiteColumns.xml

And the accompanying AcmeSiteColumns.xml file contains the following:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- Acme Site Columns -->
    <Field ID="{9A77BCEB-5230-48f2-93ED-D87DAAEC6998}" 
           Name="ArticleHeadline" 
           StaticName="ArticleHeadline" 
           SourceID="http://schemas.microsoft.com/sharepoint/v3" 
           Group="Acme Site Columns" 
           DisplayName="Article Headline" 
           Type="Text" 
           Required="FALSE" 
           Sealed="FALSE">        
    </Field>

    <Field ID="{CB2E757E-3876-40c8-A720-C98063DBCC3D}"
           Name="ArticleAbstract"
           StaticName="ArticleAbstract"
           SourceID="http://schemas.microsoft.com/sharepoint/v3"
           Group="Acme Site Columns"
           DisplayName="Article Abstract"
           Type="Note"
           NumLines="6"
           Required="FALSE"
           Sealed="FALSE">
    </Field>

    <Field ID="{13480703-B260-4354-83DC-AA7AF22A629E}"
           Name="Classification"
           StaticName="Classification"
           SourceID="http://schemas.microsoft.com/sharepoint/v3"
           Group="Acme Site Columns"
           DisplayName="Classification"
           Type="Choice"
           Required="FALSE"
           Sealed="FALSE">
            <CHOICES>
                <CHOICE></CHOICE>
                <CHOICE>Public</CHOICE>
                <CHOICE>Internal</CHOICE>
                <CHOICE>Restricted</CHOICE>
                <CHOICE>Confidential</CHOICE>
                <CHOICE>Secret</CHOICE>
            </CHOICES>
            <Default></Default>
    </Field>

    <Field ID="{B022435C-D299-40fd-B071-5C9DF95F6F4F}"
           Name="DisclosureLevels"
           StaticName="DisclosureLevels"
           SourceID="http://schemas.microsoft.com/sharepoint/v3"
           Group="Acme Site Columns"
           DisplayName="Disclosure Levels"
           Type="MultiChoice"
           Required="FALSE"
           Sealed="FALSE">
            <CHOICES>
                <CHOICE>A1</CHOICE>
                <CHOICE>A2</CHOICE>
                <CHOICE>A3</CHOICE>
                <CHOICE>A4</CHOICE>
                <CHOICE>A5</CHOICE>
            </CHOICES>
    </Field>

    <Field ID="{DBBD4D5F-A390-414b-9BB2-1160C1B2E35E}"
           Name="Lifetime"
           StaticName="Lifetime"
           SourceID="http://schemas.microsoft.com/sharepoint/v3"
           Group="Acme Site Columns"
           DisplayName="Lifetime (yrs.)"
           Type="Number"
           Required="FALSE"           
           Min="0"
           Max="100"
           Decimals="0"
           Sealed="FALSE">
    </Field>

    <Field ID="{7B6905FB-6619-4276-AB3D-9A04A04D8806}"
            Name="ExpectedCost"
            StaticName="ExpectedCost"
            SourceID="http://schemas.microsoft.com/sharepoint/v3"
            Group="Acme Site Columns"
            DisplayName="Expected Cost"
            Type="Currency"
            Required="FALSE"
            Sealed="FALSE">
    </Field>

    <Field ID="{6AAD913B-D930-49b5-8BCD-99FAB8857257}"
            Name="ExpiresOn"
            StaticName="ExpiresOn"
            SourceID="http://schemas.microsoft.com/sharepoint/v3"
            Group="Acme Site Columns"
            DisplayName="Expires On"
            Type="DateTime"
            Required="FALSE"
            Sealed="FALSE">
    </Field>

    <Field ID="{D38E97DA-CD82-4a77-9086-3B97A6D61A92}"
           Name="Activated"
           StaticName="Activated"
           SourceID="http://schemas.microsoft.com/sharepoint/v3"
           Group="Acme Site Columns"        
           DisplayName="Activated"
           Type="Boolean"        
           Required="FALSE"
           Sealed="FALSE">
        <Default>0</Default>
    </Field>
</Elements>

As should be evident, we have defined eight (8) site columns, each of which is represented by a Field XML element in the file shown above.  In this set of site columns, I’ve attempted to illustrate the following basic built-in field types: Single line of text, Multiple lines of text, Choice (single value), Choice (multiple value), Number, Currency, Yes/No, and Date/Time.  Obviously I was not able to exhaust each potential option that is available within these field types, but I’ve attempted to provide a sampling of what is possible, to wet your appetite a bit.  Furthermore notice that each site column defines its own ID value which must be a unique GUID for each column you define, as well as a consistent, meaningful grouping name. 

Also, note the use of the Required and Sealed attributes.  The Required attribute is used to indicate if a given site column, when it is used, must require a value to be specified – each of my examples set this attribute to FALSE.  The Sealed attribute is a bit more interesting – this defines if the given site column is able to be modified within the SharePoint UI after it is created, such as to allow its choice values or other customizable options to be changed.  For some columns, restricting this might make sense, but in this example I’ve again set each one to a value of FALSE for this attribute.

To see the fruits of our work, we can browse to a SharePoint site where this custom feature has been activated within, and we should see the following on the Site Columns page:

image

As stated earlier, both of these files will live within a new folder that defines the feature.  I find it most beneficial to create an Empty Visual Studio project when creating features and other custom SharePoint artifacts, and within this project, create a folder hierarchy that mimics the “12”-hive structure, for the portions of it that are applicable to your solution.  In the case of this custom feature, this means creating folders for 12, TEMPLATE, and FEATURES, nesting them accordingly, to result in the following project structure:

image

In addition to the folder structure, you will also see a BAT file script named wsp.bat. This particular BAT file script contains the necessary commands to invoke WSPBuilder to bundle this feature into a solution package, and then activate the feature within a SharePoint site.  Based upon  your deployment needs, you may find it beneficial to modify the logic within this script to meet your own needs.  The contents of my deployment BAT file is as follows:

@ECHO OFF
@SET WSPPBUILDER="C:\Tools\WspBuilder\WspBuilder.exe"
@SET STSADM="C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\STSADM.EXE"
@SET SOLUTIONNAME=ContentTypeTest.wsp
@SET URL=http://sp2007dev
@SET FEATURE=AcmeContentTypes
  
ECHO Building Solution Package
  %WSPPBUILDER% -Excludepaths bin
  
ECHO Deactivate feature
 %STSADM% -o deactivatefeature -name %FEATURE% -url %URL%/sites/blank
  
ECHO Removing Existing Solution
 %STSADM% -o retractsolution -name %SOLUTIONNAME% -immediate -url %URL%
 %STSADM% -o execadmsvcjobs
 %STSADM% -o deletesolution -name %SOLUTIONNAME%

ECHO Installing Solution
 %STSADM% -o addsolution -filename %SOLUTIONNAME%
 %STSADM% -o deploysolution -name %SOLUTIONNAME% -immediate -url %URL% -allowGacDeployment -allowCasPolicies -force
 %STSADM% -o execadmsvcjobs

ECHO Recycling App Pool
 iisapp /a "SharePoint - 80" /r

ECHO Activate feature
 %STSADM% -o activatefeature -name %FEATURE% -url %URL%/sites/blank

I find it easiest to simply set up this script to be executed as a post-build event, this way once you have made changes to your XML files within the Visual Studio project, you can just build the project, and that will kick off this process, from which you can proceed to your site, and review the results.  Below identifies how my post-build event is configured:

image

Using this approach I’ve outlined for packaging up SharePoint artifacts is well worth it, as it provides a nice end-to-end means for taking a Visual Studio project and turning it into something that can be deployed to any number of SharePoint farms without any additional work.  My forthcoming posts on creating additional features and site definitions will employ a similar pattern, though I will refrain from explaining the details of it in those posts.

So with that said, you can see what I mean by saying that leveraging SharePoint Features to deploy custom site columns adds tremendous value to a project.  Not only does it allow development efforts for creating site columns to produce something that is re-usable, but it also formalizes the process, and makes it agnostic of the SharePoint farm that it is being deployed to.  There is absolutely no reason why a set of custom features that define an *entire* SharePoint build-out couldn’t be created, pulled together into a WSP solution package, and then at the client site just install the solution package, deploy it, and proceed to activate its contents as necessary.  In the next posting I’m planning on this topic, we will take the set of site columns we’ve defined here and create a content types that links them all together, again leveraging the feature framework for the entire process.

Posted by Brian LaSitis | 1 Comments | Trackback Url | Bookmark with:        
Categories: Content Types, Taxonomy

Links to this post

Trackback from wss.made4the.net: by Jeremy Thake on 20 Feb 2009 02:43

Trackback from Casino 1242955280 on 21 May 2009 08:35

Trackback from sdiywhir on 22 May 2009 08:15

Trackback from Casino 1243045918 on 22 May 2009 10:14

Trackback from Casino 1243052402 on 22 May 2009 11:52

Trackback from Casino 1243056365 on 23 May 2009 12:47

Trackback from Casino 1243115379 on 24 May 2009 01:12

Trackback from Casino 1243365450 on 29 May 2009 10:49

Trackback from txjrzlwq on 18 Jul 2009 10:18

Trackback from qkktivql on 20 Jul 2009 12:34

Trackback from vlydbsur on 23 Jul 2009 08:52

Trackback from fwiskcjg on 23 Jul 2009 09:15

Trackback from jedoifyi on 23 Jul 2009 10:23

Trackback from nnpjqhzb on 23 Jul 2009 10:58

Trackback from urpxurhq on 23 Jul 2009 11:54

Trackback from ijsewbcq on 24 Jul 2009 01:11

Trackback from mexxkijt on 31 Jul 2009 07:15

Trackback from qigyihzt on 06 Aug 2009 01:37

Trackback from Casino 1249772306 on 09 Aug 2009 12:05

Trackback from Casino 1249883930 on 12 Aug 2009 11:16

Trackback from Casino 1250191482 on 14 Aug 2009 06:00

Trackback from Casino 1250533814 on 17 Aug 2009 02:26

Trackback from Casino 1250538984 on 17 Aug 2009 02:53

Trackback from Casino 1250624386 on 18 Aug 2009 02:25

Trackback from Casino 1250625839 on 18 Aug 2009 02:47

Trackback from Casino 1250627697 on 18 Aug 2009 04:38

Trackback from Casino 1250633977 on 18 Aug 2009 05:24

Trackback from Casino 1250699013 on 19 Aug 2009 12:21

Trackback from Casino 1250722054 on 19 Aug 2009 09:04

Trackback from jwgvubpq on 29 Aug 2009 04:07

Trackback from jwgvubpq on 29 Aug 2009 04:07

Trackback from chagfpfa on 12 Feb 2010 10:15

Trackback from download skins for windows media player on 03 Mar 2010 04:33

Trackback from cialis discount on 15 Mar 2010 08:08

Trackback from health benefits of low dose cymbalta on 16 Mar 2010 05:34

Trackback from buying cialis online uk on 16 Mar 2010 05:41

Trackback from cheap cialis on 16 Mar 2010 06:25

Trackback from viagra bestellen online on 16 Mar 2010 06:47

Trackback from cheapest generic cialis in Surry VA 23883 Virginia on 16 Mar 2010 06:52

Trackback from viagra billig bestellen on 16 Mar 2010 07:07

Trackback from cialis online pharmacy 100/100mg pills $1.35 each on 16 Mar 2010 07:33

Trackback from best time to take lexapro 50mg on 16 Mar 2010 07:47

Trackback from swedish to english translation on 19 Mar 2010 07:26

Trackback from plzqfooh on 24 Mar 2010 06:56

Trackback from oajvoyqs on 24 Mar 2010 09:14

Trackback from apcidswg on 24 Mar 2010 09:31

Trackback from qqxseqzr on 26 Mar 2010 09:42

Trackback from kuzyapyp on 27 Mar 2010 12:17

Trackback from wulhzgam on 27 Mar 2010 12:54

Trackback from asnzcawn on 27 Mar 2010 03:07

Trackback from phawtaao on 27 Mar 2010 03:58

Trackback from krlxhmmd on 27 Mar 2010 07:42

Trackback from Casino 1269728909 on 27 Mar 2010 06:55

Trackback from Casino 1269728908 on 27 Mar 2010 06:55

Trackback from Casino 1269729422 on 27 Mar 2010 06:55

Trackback from likxjneq on 27 Mar 2010 08:53

Trackback from Casino 1269771703 on 28 Mar 2010 11:30

Trackback from Casino 1269771815 on 28 Mar 2010 11:30

Trackback from Casino 1269772318 on 28 Mar 2010 11:52

Trackback from wuohmzno on 31 Mar 2010 11:12

Trackback from Casino 1270230446 on 02 Apr 2010 03:51

Trackback from Casino 1270231741 on 02 Apr 2010 03:51

Trackback from Casino 1270232424 on 02 Apr 2010 03:51

Trackback from Casino 1270494559 on 05 Apr 2010 06:20

Trackback from Casino 1270493914 on 05 Apr 2010 08:57

Trackback from aofgypti on 06 Apr 2010 11:20

Trackback from cheap cialis on 16 Apr 2010 03:20

Trackback from faxless easy online payday loans on 16 Apr 2010 03:20

Trackback from buy cipro 250mg in Valley springs SD 57068 South Dakota on 05 May 2010 08:04

Trackback from Casino 1273771865 on 13 May 2010 04:51

Trackback from Casino 1273771396 on 13 May 2010 06:34

Trackback from Casino 1273771956 on 13 May 2010 06:34

Trackback from Casino 1273772417 on 13 May 2010 06:34

Trackback from Casino 1273772500 on 13 May 2010 11:24

Trackback from Casino 1273971839 on 16 May 2010 01:25

Trackback from Casino 1274018354 on 16 May 2010 10:30

Trackback from Casino 1274180235 on 18 May 2010 07:03

Trackback from Casino 1274178521 on 18 May 2010 07:35

Trackback from Casino 1274179362 on 18 May 2010 08:31

Trackback from Casino 1274311448 on 19 May 2010 10:05

Trackback from Casino 1274433794 on 21 May 2010 06:43

Trackback from Casino 1274566454 on 23 May 2010 04:49

Trackback from Casino 1276648322 on 15 Jun 2010 11:23

Trackback from Casino 1276643979 on 15 Jun 2010 11:23

Trackback from Casino 1276644814 on 15 Jun 2010 11:23

Trackback from Casino 1276715057 on 16 Jun 2010 06:08

Trackback from Casino 1276717002 on 17 Jun 2010 12:48

Trackback from Casino 1276953490 on 19 Jun 2010 11:22

Trackback from Casino 1277139884 on 21 Jun 2010 01:07

Trackback from Casino 1277214763 on 22 Jun 2010 10:57

Trackback from Casino 1277306709 on 23 Jun 2010 01:08

Trackback from Casino 1277733784 on 28 Jun 2010 01:43

Trackback from Casino 1280331167 on 28 Jul 2010 02:00

Trackback from Casino 1280345989 on 28 Jul 2010 06:13

Trackback from Casino 1284491528 on 14 Sep 2010 05:15

Trackback from Casino 1287050304 on 14 Oct 2010 07:36

Trackback from Casino 1290150433 on 19 Nov 2010 04:17

Trackback from Casino 1290852380 on 27 Nov 2010 07:30

Trackback from Casino 1292243887 on 13 Dec 2010 10:10

Trackback from Casino 1297161962 on 08 Feb 2011 08:11

Trackback from Casino 1299680089 on 09 Mar 2011 01:23

Trackback from commander viagra on 29 Jan 2012 04:24

Trackback from nymphomax on 04 Feb 2012 04:54

Comments

Friday, 27 Feb 2009 12:21 by Mark Klinchin
Thank you for the interesting and concise article. I think that it is worth to mention that we have graphical tool (like Entity Relationship Diagram for RDBMS) that lets to create columns, content types, lists and copy them between sites - remotely, without login to SP box. It serves the same purpose but with slightly different intent. Free beta program is currently open for this tool, check it out here: http://www.metavistech.com We would appreciate your comments.


Name

Url

Email

Comments

CAPTCHA Image Validation