Tuesday, March 19, 2013

Create Managed Metadata Site Column in SharePoint 2010

In this post we are going to see how we can create a site column that links with the managed meta data.

Creating a site column that holds a value from managed meta data term store is not directly available.

We first need to create a site field which is of type taxonomy and then we need to create one feature which is scoped at site level and in feature activation we access the taxonomy store and set the site column to required term set.

Go to site settings, under site administration click on term store management. I have following settings.

Now let's create one Visual studio project. Select blank project.

Now add SharePoint content type project item.

Choose item content type. This is a sample that I have.

Finally I have something like this.

Now add an event receiver to the site level feature.

What we are doing here is we are first taking reference of the field that we created. Then we take a term store in to the account and then locate the Microsoft group.

After that we take Web Technologies group and then associate this group to our site column.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
            SPSite site = properties.Feature.Parent as SPSite;

            Guid fieldId = new Guid("{B87F6D98-564F-43CA-B4A6-1539A3A3E7C2}");

            if (site.RootWeb.Fields.Contains(fieldId))
                TaxonomySession session = new TaxonomySession(site);

                if (session.TermStores.Count != 0)
                    var termStore = session.TermStores["Managed Metadata Service"];

                    foreach (Group grp in termStore.Groups)
                        if (grp.Name == "Microsoft")
                            var group = grp;

                            var termSet = group.TermSets["Web Technologies"];

                            TaxonomyField field = site.RootWeb.Fields[fieldId] as TaxonomyField;

                            field.SspId = termSet.TermStore.Id;
                            field.TermSetId = termSet.Id;
                            field.TargetTemplate = string.Empty;
                            field.AnchorId = Guid.Empty;


Deploy the project.

To check this, go to a task list. go to the list settings and enable the management of content type. 

Once enabled, add from existing site content type and add the SPKings content type.

and there you go

I hope this helps.

No comments:

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com