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.

May 2006 - Posts

  • 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(true);

     

    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

  • Sitecolumns in Office SharePoint Server 2007

    One of the many new features in Office SharePoint Server 2007 is the availability of sitecolumns. Sitecolumns are SharePoint list fields, that are defined at the site level. They are a general definition of a column, that can be re-used throughout the site, and it’s subsites. This is an important improvement, because you probably all remembered adding a choicefield “Region” to your lists/libraries in SPS2003. Just after you finished adding this choicefield to all lists, you found out there was a new region that had to be added to all the dropdowns…..
    This is where sitecolumns in MOSS help you. Another great thing is that you can now create a “Customer” lookup column, and re-use that throughout all your sites. This way all “Customer” lookups use the same list that holds the customer data. Nice!! These lookups also have a nice new feature: “Allow multiple values”. 

    The following is a brief description of how to create site columns. The first step is to create the site column. In any site, go to Site Settings in any site and click “Site columns” in the Galleries section, and click “Create”.

     O_Sitecolumns_1(Click for a larger version)

    An important thing to remember is that site columns have a “scope”. The availability of your sitecolumns in the portal/sitecollection depends on this scope. Site columns (this also applies to content types) are available in the site where you create them, and all it’s subsites. Site columns that should be available in every site, should be created at the root level (Home). After you create the site column, you can use that column in all your lists and document libraries (or even better, in content types).

    The next step is to add the site columns to a list or document library. Go to the settings menu of your list and click the “Add from existing site columns” link:

    O_sitecolumns_3   

    When you make an update to the select list, the changes are automatically pushed to all instances, when you select “Update all list columns based on this site column?”  

          O_sitecolumns_2

    For each instance of a site column in a list, you can override settings (like “required” or the default value). Please note that when you make changes to the site columns itself, and update all the lists, these changes at the list level will be overridden.


Need SharePoint Training? Attend a SharePoint Bootcamp!

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