Frage

I am trying to create a new term set in SharePoint 2013 using a custom WCF web service deployed within SharePoint 2013 server. I have written below code to create the term set.

SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (Impersonator imp = new Impersonator("Username", "Domain", "Password"))
                {
                    using (SPSite site = new SPSite("http://server:8002/sites/site/"))
                    {
                        site.AllowUnsafeUpdates = true;
                        TaxonomySession session = new TaxonomySession(site);
                        TermStore termStore = session.TermStores["Managed Metadata Service"];
                        var termStoreAdmin = termStore.TermStoreAdministrators.Where(obj => obj.PrincipalName.Contains("domain\\username")).FirstOrDefault();

                        if (termStoreAdmin == null)
                            termStore.AddTermStoreAdministrator("domain\\username");

                        Group group = termStore.GetGroup(new Guid(groupGuid));
                        if (group != null && !string.IsNullOrEmpty(termSetName))
                        {
                            TermSet termset = group.TermSets.FirstOrDefault(obj => obj.Name.Equals(termSetName));
                            if (termset == null)
                            {
                                termset = group.CreateTermSet(termSetName);
                                termSetGuid = termset.Id.ToString();
                            }
                            SetupNavTermSet(termset, session, site.OpenWeb());
                        }
                        termStore.CommitAll();
                    }
                }
            });

I am calling this method from silverlight code using soap message. While calling this code I am getting exception while executing group.CreateTermSet(termSetName); line.

The error is:

Error Message :  Value cannot be null.
Source        : Microsoft.SharePoint
Error Details :     at Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager.GetUserIdentifierEncodedClaim(IIdentity identity)
   at Microsoft.SharePoint.Taxonomy.Internal.CommonUtilities.GetCurrentUserName()
   at Microsoft.SharePoint.Taxonomy.TaxonomySession.get_CurrentUserName()
   at Microsoft.SharePoint.Taxonomy.Group.CreateTermSet(String name, Guid newTermSetId, Int32 lcid)
   at Microsoft.SharePoint.Taxonomy.Group.CreateTermSet(String name)
   at SplitVisionMetadataManagement.CustomManageMetaDataWCFService.<>c__DisplayClassc.<CreateTaxonomyTermSet>b__8()

Has anybody got this issue and a solution?

War es hilfreich?

Lösung

I also encountered the same issue and figured out that the Microsoft.SharePoint.Taxonomy.Internal.CommonUtilities.GetCurrentUserName() method uses the HttpContext.Current.User security principal for arriving at the user name.

I am using similar code in a windows form application and hence the HttpContext was empty. I made a workaround by setting the context and user manually as below.

if (HttpContext.Current == null)  
{  
    HttpRequest request = new HttpRequest("", SiteURL, "");  
    HttpContext.Current = new HttpContext(request, new HttpResponse(TextWriter.Null));  
    HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal;  
}  

SPSecurity.RunWithElevatedPrivileges(delegate()  
{  
    using (SPSite site = new SPSite(SiteURL))  
    {  
        using (SPWeb web = site.OpenWeb())  
        {  
            if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)  
                HttpContext.Current.Items["HttpHandlerSPWeb"] = web;  

            // Your SharePoint Term Creation code
        }
    }
});

In your case it seems like you are using claims based authentication and hence some issue with the claims provider in returning the name. You HTTP context would be the context under which the WCF is running. You may need to investigate further in those angle.

The above knowledge should help you to understand it further.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top