another technical blog...technically

Thursday, June 4, 2015

Updating term set in SP2013

First of all, this is not something i discovered on my own, i just read it somewhere in the web and I never found the original post again, maybe because it was written in german (credits to the original author), so I decided to write this useful trick in english in order to help more people to accomplish the same task: updating a termset from code behind.
Let’s assume you need to do this from an application page, you need to use the SPSecurity and RunWithElevatedPrivileges as usual.
SPSecurity.RunWithElevatedPrivileges(() =>
{
 using (SPSite elevatedSite = new SPSite(site.ID, site.Zone))
 {
  TaxonomySession taxonomySession = new TaxonomySession(elevatedSite);
  //Do here the update work
 }
});
Ok! This code simply doesn’t work: this is due to the fact you have to switch the context to the service account one like this.
SPSecurity.RunWithElevatedPrivileges(() =>
{
 using (SPSite elevatedSite = new SPSite(site.ID, site.Zone))
 {
  HttpContext oldContext = HttpContext.Current;

  try
  {
   HttpContext.Current = null;
   TaxonomySession taxonomySession = new TaxonomySession(elevatedSite);

   //Do here the update work
  }
  finally
  {
   HttpContext.Current = oldContext;
  }
 }
});
le jeux sont fait
Share:
written in: Milano, Italia

0 commenti:

Post a Comment

Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?