General code:
using (SPSite site = new SPSite("http://site"))
{
TaxonomySession _TaxonomySession = new TaxonomySession(site);
//Get instance of the Term Store
TermStore _TermStore = _TaxonomySession.TermStores["My Term Store"];
//Now create a new Term Group
Group _Group = _TermStore.CreateGroup("My New Group");
//Create a new Term Set in the new Group
TermSet _TermSet = _Group.CreateTermSet("My New Termset");
//Add terms to the term set
Term _term1 = _TermSet.CreateTerm("First Term", 1033);
Term _term2 = _TermSet.CreateTerm("Second Term", 1033);
Term _term3 = _TermSet.CreateTerm("Third Term", 1033);
Term _term4 = _TermSet.CreateTerm("Last Term", 1033);
//commit changes
_TermStore.CommitAll();
}
Scenario:
1) The "Term Store Management" is located in Central admin site (http:\\Spadmin\).
2) Pages library is located in the publishing site.
3) We have to write and event handler such that when ever a page is created in pages library in publishing site , the title field of the page should be added to the Central administration "Term Store Management" Taxanomy group related Term Set.
Solution:
We will write an event handler for Pages library such that which will read the title and using Elevated privileges it will add the title to Central admin taxanomy term set.
Note(Trick): Make sure the Application pool running account(service account) of the central admin should be added as admin or Contributor to the Taxanomy :Term Store Administrators.
Event Handler code:
using (SPSite site = new SPSite("http://site"))
{
TaxonomySession _TaxonomySession = new TaxonomySession(site);
//Get instance of the Term Store
TermStore _TermStore = _TaxonomySession.TermStores["My Term Store"];
//Now create a new Term Group
Group _Group = _TermStore.CreateGroup("My New Group");
//Create a new Term Set in the new Group
TermSet _TermSet = _Group.CreateTermSet("My New Termset");
//Add terms to the term set
Term _term1 = _TermSet.CreateTerm("First Term", 1033);
Term _term2 = _TermSet.CreateTerm("Second Term", 1033);
Term _term3 = _TermSet.CreateTerm("Third Term", 1033);
Term _term4 = _TermSet.CreateTerm("Last Term", 1033);
//commit changes
_TermStore.CommitAll();
}
Scenario:
1) The "Term Store Management" is located in Central admin site (http:\\Spadmin\).
2) Pages library is located in the publishing site.
3) We have to write and event handler such that when ever a page is created in pages library in publishing site , the title field of the page should be added to the Central administration "Term Store Management" Taxanomy group related Term Set.
Solution:
We will write an event handler for Pages library such that which will read the title and using Elevated privileges it will add the title to Central admin taxanomy term set.
Note(Trick): Make sure the Application pool running account(service account) of the central admin should be added as admin or Contributor to the Taxanomy :Term Store Administrators.
Event Handler code:
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
string strTitle = string.Empty;
using (SPSite site = new SPSite(properties.WebUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list =
web.Lists[properties.ListTitle ];
SPListItem item =
list.GetItemByIdSelectedFields(properties.ListItemId, "Title");
strTitle = (String)item.DisplayName ;
}
}
//Code to add capture field (Title field value of Pages library) value to Taxanomy in Central administration
_____________________________________________________________________________________
**********Another Code to access central administration site URL***************
Microsoft.SharePoint.Administration.SPAdministrationWebApplication centralWeb =SPAdministrationWebApplication.Local;
string cntrlAdmnwebUrl = centralWeb.Sites[0].Url;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite CntrlAdmsite = new SPSite(cntrlAdmnwebUrl))
{
TaxonomySession _TaxonomySession = new TaxonomySession(CntrlAdmsite);
//
________________________________________________________________________________
_____________________________________________________________________________________
**********Another Code to access central administration site URL***************
Microsoft.SharePoint.Administration.SPAdministrationWebApplication centralWeb =SPAdministrationWebApplication.Local;
string cntrlAdmnwebUrl = centralWeb.Sites[0].Url;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite CntrlAdmsite = new SPSite(cntrlAdmnwebUrl))
{
TaxonomySession _TaxonomySession = new TaxonomySession(CntrlAdmsite);
//
________________________________________________________________________________
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite CntrlAdmsite = new SPSite("http://hccspdev03:2013/"))
{
TaxonomySession
_TaxonomySession = new TaxonomySession(CntrlAdmsite);
//Get instance of the Term Store
TermStore _TermStore =
_TaxonomySession.TermStores["Managed
Metadata Service"];
// Get instance of Term Group
Group _Group = _TermStore.Groups["HCCCOM"];
// Get instance of Term Set in the new Group
TermSet _TermSet =
_Group.TermSets["BannerLocation"];
//Add terms to the term set
Term term =
_TermSet.CreateTerm(strTitle, 1033);
//commit changes
_TermStore.CommitAll();
}
});
}
No comments:
Post a Comment