Add Metadata Taxonomy Custom Field using Visual Studio

Hi,

Custom Taxonomy Field creation is very common requirement while working with the different clients. In this post I will explain how we can create a custom metadata type column. Once metadata field is added as site column, it can easily be added as custom list column or field to any of the content type.

Step 1: Create a fresh project of type "SharePoint 2013 Project". After providing basic properties of project like Farm/Sandbox solution and SharePoint site Url project is ready to add field into the site.

MetadataField_1MetadataField_3

Step 2:  Now its optional to create new folder "Site Columns" as per the project coding standards. But in case you are adding multiple custom columns then I recommend to add a new folder as I did.

3

Step 3 : Next, Just hit right click on the project/folder and say "Add > New Item" from the menu options. Then select "Site Column" from the item list.

MetadataField_2

Give any relevant name to your column.

Step 4: After you hit "Add", a new module will be added to the project or folder with default Element.xml file. Here you have to make some changes as this default xml will create a custom column of type "Text" which is not our requirement.

Delete everything(i.e. <Field> tag) under the <Elements> tag  and add following code in it:

<Field
ID="{ff775dd5-b916-4561-8255-fe7806362437}"
Name=CustomTags"
DisplayName="Custom Tags"
Type="TaxonomyFieldType"
Required="FALSE"
Group="Mohit"
ShowField="Term1033"
EnforceUniqueValues="FALSE"
DisplaceOnUpgrade="TRUE"
Overwrite="TRUE"
StaticName="CustomTags" >
<Customization>
<ArrayOfProperty>
<Property>
<Name>TextField</Name>
<Value xmlns:q6="http://www.w3.org/2001/XMLSchema"
p4:type="q6:string"
xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">{FE7411CC-2F8B-44EA-BB12-56EDF8BDEFF9}</Value>
</Property>
</ArrayOfProperty>
</Customization>
</Field>
<Field Type="Note"
DisplayName="Custom Tags_0"
StaticName="Custom_x0020_TagsTaxHTField0"
Name="Custom_x0020_TagsTaxHTField0"
ID="{FE7411CC-2F8B-44EA-BB12-56EDF8BDEFF9}"
ShowInViewForms="FALSE"
Required="FALSE"
Hidden="TRUE"
CanToggleHidden="TRUE">
</Field>

NOTE:

1) The Id attribute should have unique Guid values for both of the fields.

2) The Guid of "TextField" under customization section of the first field must have same Guid(i.e. {FE7411CC-2F8B-44EA-BB12-56EDF8BDEFF9}) of the note type field

Till this step you are able to add the metadata column but there are little more things to make this functional.

Step 5: You must have noticed a default feature will be added to your project. Now right click on that feature and select "Add Event Receiver" to this feature.

Step 6: Next is to bind our newly created field with a taxonomy term set. This will require name of the group and name of the term set under managed metadata service.

NOTE: Before implementing this, I would like you to think a bit on targeted version on SharePoint. If you are working with SharePoint Server version and deploying Farm solutions then add event receiver code to bind column with Termset.

But If you are planning to use this solution on SharePoint Online or using Sandbox solution then feature reciever will not work as you can not have access "Microsoft.SharePoint.Taxonomy" dll in code. In order to handle this case, you have to follow other approach which I will explain in my future posts. Feature reciever code for binding Termset is given below:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var thisWeb = (SPWeb)properties.Feature.Parent;

using(SPSite site = new SPSite(thisWeb.Url))
{
using (SPWeb web= site.OpenWeb())
{
UpdateNewsCategoryField(web);
}
}
}

private static void UpdateNewsCategoryField(SPWeb web)
{
var site = web.Site;
var session = new TaxonomySession(site);

var termStore = session.DefaultSiteCollectionTermStore;
if (termStore == null) return;

var group = termStore.Groups[Constants.METADATA_GROUP_NAME]; //Name of the group
if (group == null) return;

var termSet = group.TermSets[Constants.METADATAB_TERMSET_NAME];// Name of the Termset
if (termSet == null) return;

//Id of below two fields must be picked form the element.xml

var field = (TaxonomyField)web.Fields[new Guid("FF775DD5-B916-4561-8255-FE7806362437")];
var newsCategoryNote = web.Fields[new Guid("FE7411CC-2F8B-44EA-BB12-56EDF8BDEFF9")];

field.SspId = termSet.TermStore.Id;
field.TermSetId = termSet.Id;
field.AllowMultipleValues = false;
field.TextField = newsCategoryNote.Id;
field.Update(true);
}

This is all you need to do to create a custom metadata site column. Save and deploy your solution in SP site. In order to check the newly created columns please navigate to Site Settings > Site Columns.

Troubleshooting Errors:

If you get any error while activating feature or your site column is not properly configured. Please make sure that you have following point in place:

1) Group Name: Make sure you have added Metadata group with same name mentioned in feature receiver.

2)  Term set Name: Make sure you have added Metadata Term set under the same group with same name mentioned in feature receiver.

3) DefaultSiteCollectionTermStore: May be the case that you are not able to pick default term store. Please refer to my next blog(Sharepoint Defaultsitecollectiontermstore is null) to find out solution of this.

4) If you are creating list by referring this custom column then you might get this error- "The SPListItem being updated was not retrieved with all taxonomy fields."  Please refer to my this blog to see the resolution.

Happy SharePointing :)

Comments

Post a Comment

Popular posts from this blog

Hide Ribbon on SharePoint 2013 using CSS

Get Comment Count in SharePoint

Configure external site as content sources in sharepoint search