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 > Adding and using site columns and content types from code in Office SharePoint Server 2007
Adding and using site columns and content types from code in Office SharePoint Server 2007

As described in the previous post, sitecolumns are an interesting new feature of Office SharePoint Server 2007. There are 3 ways you can create these sitecolumns:

1.      By using the user interface (see previous item)

2.      By using the object model.

3.      By using SharePoint Features.

This item is about the second option. 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.

Add a new site column

Sitecolumns can be added to a SPWeb object, which is exactly the same as adding fields to a list. Both SPList and SPWeb now have a “Fields” property. Adding fields these SPFieldCollection object is the same as in SharePoint 2003. The sample below adds a new choicefield to the “Topics” site in my testportal.

    SPSite site = new SPSite("http://tonportal");
    SPWeb web = site.OpenWeb("/Topics");
    string newFieldName = web.Fields.Add("RegionFromCode", SPFieldType.Choice, true);
    SPFieldChoice regionField = (SPFieldChoice)web.Fields[newFieldName];
    regionField.Choices.Add("Global");
    regionField.Choices.Add("Europe");
    regionField.DefaultValue = "Europe";
    regionField.Update();
 

This nice thing about a sitecolumn is that when you make changes to them , these changes are pushed to all instances that use the site column. From code you can do this by using the “pushChangesToLists” parameter of the Update method of the SPField:

    regionField.Update();

 

Add site column to a list

When you want to add a site column to a list from code, you can do that by using the “AvailableFields” property of the SPWeb. By using AvailableFields you do not have to worry about the scope of your sitecolumn. Of course it must be available in your site, but you don’t have to figure out if the field is in the current web, or one if it’s parent webs. In the sample below, the site column we create above will be used in one of the subsites of the Topics site. It is added to the “Shared Documents” document library.

    SPSite site = new SPSite("http://tonportal");
    SPWeb web = site.OpenWeb("/Topics/MOSS2007");
    SPList list = web.Lists["Shared Documents"];
    SPField regionField = web.AvailableFields["RegionFromCode"];
    list.Fields.Add(regionField); 
 

Content Types

As described in this post by Martin Kearn, content types are another important new feature of MOSS. It is very easy to add site columns to a contenttype. You simply add a new SPFieldLink object to the FieldLinks collection of the contenttype. The code snippet below adds a new content type to the Topics site, and adds the region column we just created to that content type. The content type will inherit from the out of the box SharePoint ‘Document’ content type.

    SPSite site = new SPSite("http://tonportal");
    SPWeb web = site.OpenWeb("/Topics");
 
    // Create new content type 
    SPContentType documentContentType = web.AvailableContentTypes["Document"];
    SPContentType newContentType = new SPContentType(documentContentType, web.ContentTypes, "Ton Document");
    web.ContentTypes.Add(newContentType);
    newContentType = web.ContentTypes[newContentType.Id];
 
    // Add FieldLink to content type 
    SPField regionField = web.AvailableFields["RegionFromCode"];
    SPFieldLink fieldLink = new SPFieldLink(regionField);
    newContentType.FieldLinks.Add(fieldLink);
    newContentType.Update(false);

The Update method of a content type has a parameter “UpdateChildren”. If this is true, it will update all content types that inherit from this content type.

Use content type

The last step in this post is to add the new content type to a document library. Please not that these steps do not only apply to document libraries, but to all SharePoint lists and content types. To add a contenttype to a list, the list must support contenttypes. You can switch this on by using the ContentTypesEnabled property.

    SPSite site = new SPSite("http://tonportal");
    SPWeb web = site.OpenWeb("/Topics");
    SPList list = web.Lists["Document Library"];
    SPContentType documentContentType = web.AvailableContentTypes["Ton Document"];
    list.ContentTypesEnabled = true;
    list.ContentTypes.Add(documentContentType);

 

To summarize the steps we have done in this post:

·        Create a new site column

·        Use the site column to a list

·        Create a new content type

·        Add our new site column to the new contenttype

Use the new contenttype in a list

Comments

Re: Adding and using site columns and content types from code in Office SharePoint Server 2007

good articles.... really helpful...
at 11/11/2009 9:19 AM

Add Comment

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).