Hi i want to run a user as administrator to create terms.

 SPSite site = SPContext.Current.Site;
        SPSecurity.RunWithElevatedPrivileges(() =>
            using (SPSite secureSite = new SPSite(site.ID))
                TaxonomySession ts = new TaxonomySession(secureSite);
                TermStore termStore = ts.TermStores["Managed Metadata Service"];
                Group group = termStore.Groups["MyGroup"];

                TermSet termSet = group.CreateTermSet("Termset");

                termSet.CreateTerm("Term", 1033);



I can only create terms if the current user are a Contributor in the termstore, but i want to run this user as administrator without adding the user to the contributor in the term store?

1 Answer 1


The reason your code is not working is that TaxonomySession uses the user from the HttpContext. I found this link which is in German but the suggested code looks like this:

var siteId = SPContext.Current.Site.ID;
HttpContext oldContext = null;
   //Save actual context and null
   oldContext = HttpContext.Current;
   HttpContext.Current = null;
      using (var elevatedSite = new SPSite(siteID))
         var taxonomySession = new TaxonomySession(elevatedSite);
         var termStore = taxonomySession.DefaultKeywordsTermStore;

         //put your logic here

   HttpContext.Current = oldContext;
  • Something went wrong An unexpected error has occurred. Page Maintenance Web parts: If you have sufficient privileges , you can use this page to temporarily close Web Parts or remove personal settings. Contact the Web site administrator if you want more information . @Andreas
    – Batistuta
    Commented Jun 9, 2015 at 7:35
  • Have you tried to debug the code? I pasted it into my VM first and it worked, although I didn't add anything to the TermStore.
    – Andreas
    Commented Jun 9, 2015 at 7:44
  • SPSite site = SPContext.Current.Site; get object reference not set?
    – Batistuta
    Commented Jun 9, 2015 at 8:04
  • If SPContext.Current is null this is not related to this issue. Are you working in the context of a webpart?
    – Andreas
    Commented Jun 9, 2015 at 8:11
  • on button click in a webpart
    – Batistuta
    Commented Jun 9, 2015 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.