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;
field.Update();
break;
}
}
}
}
}
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:
Post a Comment