Andrew Connell [MVP SharePoint]
1741 Posts |  46 Articles |  5310 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
Andrew Connell's RSS Feed    
AC's Blog Quick Links
SharePoint Quick Links
Article Categories
Archives
May, 2012 (3)
April, 2012 (4)
March, 2012 (4)
February, 2012 (4)
January, 2012 (12)
December, 2011 (6)
November, 2011 (3)
October, 2011 (17)
September, 2011 (8)
August, 2011 (6)
July, 2011 (7)
June, 2011 (13)
May, 2011 (9)
April, 2011 (15)
March, 2011 (1)
February, 2011 (6)
January, 2011 (5)
December, 2010 (11)
November, 2010 (6)
October, 2010 (12)
September, 2010 (5)
August, 2010 (4)
July, 2010 (5)
June, 2010 (6)
May, 2010 (11)
April, 2010 (11)
March, 2010 (9)
February, 2010 (9)
January, 2010 (3)
December, 2009 (10)
November, 2009 (15)
October, 2009 (15)
September, 2009 (7)
August, 2009 (4)
July, 2009 (10)
June, 2009 (8)
May, 2009 (2)
April, 2009 (9)
March, 2009 (6)
February, 2009 (16)
January, 2009 (6)
December, 2008 (12)
November, 2008 (12)
October, 2008 (27)
September, 2008 (13)
August, 2008 (14)
July, 2008 (14)
June, 2008 (12)
May, 2008 (23)
April, 2008 (12)
March, 2008 (15)
February, 2008 (13)
January, 2008 (12)
December, 2007 (10)
November, 2007 (8)
October, 2007 (15)
September, 2007 (20)
August, 2007 (21)
July, 2007 (16)
June, 2007 (8)
May, 2007 (25)
April, 2007 (16)
March, 2007 (18)
February, 2007 (18)
January, 2007 (12)
December, 2006 (16)
November, 2006 (13)
October, 2006 (18)
September, 2006 (22)
August, 2006 (27)
July, 2006 (23)
June, 2006 (23)
May, 2006 (23)
April, 2006 (9)
March, 2006 (17)
February, 2006 (15)
January, 2006 (23)
December, 2005 (31)
November, 2005 (32)
October, 2005 (38)
September, 2005 (53)
August, 2005 (30)
July, 2005 (63)
June, 2005 (30)
May, 2005 (59)
April, 2005 (29)
March, 2005 (74)
February, 2005 (27)
January, 2005 (22)
December, 2004 (32)
November, 2004 (42)
October, 2004 (39)
September, 2004 (20)
August, 2004 (14)
July, 2004 (27)
June, 2004 (40)
May, 2004 (5)
April, 2004 (6)
March, 2004 (16)
February, 2004 (26)
January, 2004 (23)
December, 2003 (7)
November, 2003 (14)
October, 2003 (20)
September, 2003 (4)
Post Categories



Managed Windows Shared Hosting

In this post I want to cover how to create managed metadata columns. The other posts in this series are as follows (I'll update this list as they are published):

Creating columns on a SharePoint list is fairly straightforward for most folks as the browser is such an easy option. Another creation option is SharePoint Designer 2010. However in both of these scenarios the creation of the columns is not very portable. These columns are only going to live within the confines of the list you created them within. Be forewarned!

What about the situations where you want to have a more repeatable scenario in creating the columns of type Managed Metadata? In these cases you want to get everything boxed up in a package (*.wsp). The only way to do this is to either create the column declaratively or programmatically. However there can be issues with this approach.

First, look at your two options. You can create site / list columns in a repeatable fashion using the object model typically either in a Feature receiver or maybe as a PowerShell Script. The other option is to create the column using the Feature schema (XML). Both options have their advantages and disadvantages. I've had some great conversations watching folks go back and forth on the merits of a declarative vs. programmatic approach.

Wictor Wilen (here & here) and Ari Bakker (here & here) have written a good bit about this subject. Now I'd like to give you my opinion. Generally speaking I prefer to create columns declaratively. However I found quickly that you couldn't do a 100% pure declarative approach when creating Managed Metadata columns. You always had some extra work to do to clean up links to the term store and term set the column was attached to. Sure, in many instances you could define these values in XML, but it always felt like an exercise that was "do it only in XML for the sake of doing it" not because that is what made sense. Most of the time I found I needed to add some logic around getting the term store or term set and not just specifying some ID. This is demonstrated in both Wictor's and Ari's posts… they both show how to create the columns declaratively and then have some code do the extra work.

However I’d like to offer another approach. Instead of doing it declaratively, why not do the whole column programmatically? When you create the column programmatically you don't have any extra work to do like creating the note field or the other hidden fields. When you create the Managed Metadata column programmatically, SharePoint handles all the other creation of the hidden fields and wiring up tasks. There's no extra work that you have to do as is shown in both Wictor's and Ari's posts.

For instance, consider this code creating a new column on a list:


click image for larger view

Now using the Server Explorer tool window in Visual Studio 2010 notice the other hidden columns were created for us. Here are the two event receivers automatically added to a list:


click image for larger view

And here are all the fields (#1 is what I created in code, #2 & #3 are the new hidden fields):


click image for larger view


click image for larger view

If you need some more info on these hidden fields, check out my other post on the subject.

Perfect… exactly what we wanted! Now I too wish there was a declarative only way to do this but fact is there just isn't. So for now, this method works just fine for me and doesn't require any extra work to do what SharePoint should already be doing for us.

posted on Monday, July 25, 2011 7:19 AM

Feedback

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 7/25/2011 9:22 AM Ari Bakker
Gravatar The main reason I'd go for a declarative approach is so that you can use the metadata columns in declarative content types and (particularly) list definitions. If you create the metadata column programmatically as you do above the unique ID for the field is automatically generated at activation time so you prevent yourself from extending your solution to include these types of elements. For one off columns on a specific list then a programmatic approach is fine but if you are creating reusable site columns you need to be aware that the the programmatic approach isn't as extensible as it prevents you from creating list definitions that include these columns.

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 7/26/2011 5:59 AM AC [MVP SharePoint]
Gravatar @Ari - That is a valid reason... needing the GUIDs ahead of time.

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 8/3/2011 9:28 PM michael
Gravatar Is there any way to get the GUID when taking a declarative approach? I've tried all the other combination of xml and declarative but cannot get it to link up correctly at all. I've gotten it all the way to the point of creating my custom content type based on an article page, creating the managed metadata site column with the hidden note field and the other two required ones. I've tried wiring it up both in the code as well as in the xml. I've gotten it to the point were i can add the metedata column to my page layout that is based on my custom content type, i can then edit that page and set my metadata field correctly but when i do a save and close it errors saying it cannot find that required hidden note field. If i do it all declaratively then i don't know the guid to supply to the taxonmyfield that is in my page layout.

Any help would be great.

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 8/4/2011 7:20 AM AC [MVP SharePoint]
Gravatar @michael - Not declaratively, only programatically.

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 8/4/2011 8:22 AM Michael
Gravatar Sorry, it was late, i meant when doing it all programatically, is there any way to know the GUID when that field is created, or will it always be a random guid?

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 8/4/2011 9:57 AM AC [MVP SharePoint]
Gravatar @michael - No worries... the technique is to create it and get it right back to check it's ID... like a two-phase commit.

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 8/4/2011 10:50 AM michael
Gravatar Yeah the problem lies that i need to know the id, so in my page layout i can add the TaxonomyFieldControl so that anyone who creates a page with that layout will be able to categorize it. But when i do it declarativly i keep getting the error about it not finding the hidden note field even though its valid. And when i do it programmaticly i won't know what id to supply the fieldname property of the TaxonomyFieldControl.

Am i missing something?

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 8/10/2011 10:36 PM AC [MVP SharePoint]
Gravatar @Michael - Nope... if you need to know the ID, then you have to do it declaratively.

# re: SharePoint 2010 Managed Metadata - Creating Managed Metadata Columns 1/20/2012 2:09 AM Lavanya
Gravatar Hi Andrew,

I need to create a wsp to add managed metadata properties for the search configuration. we haev around 54 metadata properties to be added. and its tedious to do as a manual task everytime we deploy a new web application. Can you please give me any pointers to do this task using a wsp.
Thanks in advance!

Regards,
Lavanya

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 8 and 1 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelevant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2012 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License

 
SharePoint Training
Looking for SharePoint 2010 training for developers, administrators, power users, information workers, end users & web designers? Look no further! My company, Critical Path Training offers the best SharePoint training around! We offer public & private classes both as in-person instructor-loed hands-on classes and online classes. Check out our schedule and course catalog for all the ways we can get you going on your SharePoint path!