Friday, December 04, 2009

Getting the ContentType from an ItemAdding Event Handler

Inside an event handler for an ItemAdded event, getting the List Item’s ContentType is as easy as

var contentType = properties.ListItem.ContentType;

However, for ItemAdding, properties.ListItem is null.

Luckily, the ContentType name and Id are part of the properties.AfterProperties collection – the following will work:

SPList list = properties.List; 
string contentTypeName = properties.AfterProperties["ContentType"].ToString(); 
SPContentType contentType = list.ContentTypes[contentTypeName]; 
SPFieldCollection fields = contentType.Fields;

Between the AfterProperties field values and the content type field collection, you typically have all that you need... Just remember to not depend on properties.ListItem.

Labels: , , , , ,

Wednesday, December 02, 2009

SharePoint 2010: Managed Metadata fields and the TaxonomyHiddenList

Notes to self and others:

When you create a Managed Metadata (aka Taxonomy) field in a SharePoint 2010 list or library, the field’s schema will look something like this:

<field id="{f6d2b908-4ed4-42f8-a491-e1177ed57596}" version="1"
rowordinal="0" colname="int3" name="Tags" staticname="Tags"
enforceuniquevalues="FALSE" required="FALSE"
displayname="Tags" type="TaxonomyFieldType">

<default>1;#A|6832dce7-bd84-4afc-8311-d5a1367dc282</default> <customization> <arrayofproperty> <property> <name>SspId</name> <value xmlns:p4="x-instance" p4:type="q1:string"
xmlns:q1="x">5dc61acc-dfa4-41dd-aa85-dd71d054ab1f</value> </property> <property> <name>GroupId</name> </property> ...

(field definition and namespaces shortened for readability)

Some of the notable attributes are:

  • Type – obvious
  • WebId - The current list’s web
  • SourceID - The current list’s id
  • List - The ID of a “TaxonomyHiddenList” which resides in the current web, but as the name implies, is hidden.
  • ShowField - the field in the TaxonomyHiddenList to display – looks like it’s locale enabled.
  • The SspId property is also crucial – it is the TermStore ID used to access the TermSet – more on that in another blog post…

The TaxonomyHiddenList’s schema looks like most lists’ schema – HUGE.  Some of the interesting fields are:

  • IdForTerm - in the case of the default value above (A) this field’s value matches the GUID 6832dce7-bd84-4afc-8311-d5a1367dc282
  • IdForTermSet - this appears to be the term set id from the MM data store – I have another column in my list which is not tied to a centrally managed metadata store – its value shows as 0000….-00… etc.
  • IdForTermStore – in my instance, this GUID is the same for both the managed and non-managed metadata field – matches the SspId above.

Labels: , ,