Wednesday, September 21, 2011

Programmatically create a term in term set....

While adding a term in term set, though you can have other ways as well to inform users but if you want to add a term with coding in term set, this is how we can achieve that.



public class TaxonomyItemReceiver : SPItemEventReceiver

{

public override void ItemUpdated(SPItemEventProperties properties)

{

if (properties.ListItem["Approval Status"].ToString() == "0")

{

string newTerm = properties.ListItem.Title;

TaxonomyFieldValue parentTerm = properties.ListItem["Parent term"] as TaxonomyFieldValue;


TaxonomySession session = new TaxonomySession(properties.Web.Site);

TermStore mainTermStore = session.TermStores[0];

Term foundTerm = session.GetTerm(new Guid(parentTerm.TermGuid));

Term addedTerm = foundTerm.CreateTerm(newTerm, session.TermStores[0].DefaultLanguage);

mainTermStore.CommitAll();

}

base.ItemUpdated(properties);

}

}


Cheers!!!

No comments: