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