0

Environment: SharePoint 2013 Enterprise (on-premises).

The User Profile property SPS-Department tied to hierarchial Managed Metadata Termset.

I need a way to read this profile property data via CSOM (or may be via asmx or search, not SSOM). As the result of this reading I should be able to get the corresponding Term from the Termset.

0

It does not seem possible to retrieve user profile properties metadata using SharePoint CSOM API, but You could consider the following solution:

Example 1. How to retrieve TermSetId from user profile property

using(var upsProxy = new UserProfileService.UserProfileService())
{
     upsProxy.Url = webUri + "/_vti_bin/UserProfileService.asmx";
     var schema = upsProxy.GetUserProfileSchema();
     var property = upsProxy.GetUserPropertyByAccountName(accountName, propertyName);
     var propertyInfo = schema.FirstOrDefault(pi => pi.Name == propertyName);

     var termSetId = propertyInfo.TermSetId.Value;
}

Complete example

//1 Retrieve User Profile via SharePoint Web Services
using(var upsProxy = new UserProfileService.UserProfileService())
{
     upsProxy.Url = webUri + "/_vti_bin/UserProfileService.asmx";
     //upsProxy.CookieContainer = GetAuthCookies(webUri, userName, password);

     var schema = upsProxy.GetUserProfileSchema();
     var property = upsProxy.GetUserPropertyByAccountName(accountName, propertyName);
     var propertyInfo = schema.FirstOrDefault(pi => pi.Name == propertyName);


     if (propertyInfo != null)
     {
         if (propertyInfo.TermSetId.HasValue && property.Values.Length > 0)
         {
              var termName = (string)property.Values[0].Value; //get first value

              //2.Get Term via SharePoint CSOM
              using (var ctx = GetContext(webUri, userName, password))
              {
                   var term = ResolveTerm(ctx, propertyInfo.TermSetId.Value, termName);
              }

         }
     }

}


public static Term ResolveTerm(ClientContext ctx,Guid termSetId,string termValue)
{
     var ts = TaxonomySession.GetTaxonomySession(ctx);
     var termSet = ts.GetDefaultSiteCollectionTermStore().GetTermSet(termSetId);
     var term = termSet.Terms.GetByName(termValue);
     ctx.Load(term);
     ctx.ExecuteQuery();
     return term;
}
  • 1
    Vadim, thank you for pointing me out. So your idea is to get Termset id by using UserProfileService.asmx and get the corresponding Term id by its name. I suppose the method ResolveTerm wil not work correctly if there are several terms with the same name in hierarchial Termset. So it seems the only option is to hold department id or code in a separate User Profile property and add a custom property to the Termset and search the term by its code with the help of TermSet.GetTermsWithCustomProperty method. – Alexey Grachev Mar 31 '15 at 10:42
  • Alexey, regarding TermSetId, that's right. Unfortunately as far as i know, it is not possible to retrieve user property metadata via CSOM. – Vadim Gremyachev Mar 31 '15 at 11:05
  • 1
    Unfortunately this doesn't work when you have multiple terms with the same name* in the same term set. – Dennis G Aug 27 '15 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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