0

I try to access to UserProfile from FillClaimsForEntity.

protected override void FillClaimsForEntity(Uri context, SPClaim entity, List<SPClaim> claims)
{
    if (entity == null) throw new ArgumentException("entity");
    if (claims == null) throw new ArgumentException("claims");


    var userLoginEncoded = SPUtility.FormatAccountName("i", entity.Value);
    var userLogin = SPClaimProviderManager.Local.DecodeClaim(userLoginEncoded).Value;

    if (userLogin.IndexOf("nt", StringComparison.InvariantCultureIgnoreCase) >= 0)
    {
        return;
    }


    SPSecurity.RunWithElevatedPrivileges(() =>
    {
        using (var site = new SPSite(context.ToString()))
        {
            var serviceContext = SPServiceContext.GetContext(site);
            var profileManager = new UserProfileManager(serviceContext);
        }
    });
}

Unfortunately, this does not work. I get this exception:

'Microsoft.Office.Server.UserProfiles.UserProfileApplicationNotAvailableException' occurred in Microsoft.Office.Server.UserProfiles.dll but was not handled in user code

Additional information: No User Profile Application available to service the request. Contact your farm administrator.

  1. In permissions of the User Profile Service Application, I have permissions to connect for Administrator and for Network Service.
  2. I tried to use LogonUser to impersonate user as Administrator, but issue is the same.
0
protected override void FillClaimsForEntity(Uri context, SPClaim entity, List<SPClaim>
claims)
{ 
if (entity == null) throw new ArgumentNullException(providerInternalName + ":Invalid 
   Entity");
 if (claims == null) throw new ArgumentNullException(providerInternalName + ":Invalid   
claims collection");

string userId = entity.Value.Split('|')[1];
Dictionary<String, String> userProfileProperties =   

ClaimsHelperClass.GetUserProfileProperties(userId);

 foreach (KeyValuePair<String,String> userProfileProperty in userProfileProperties)
 { 
 claims.Add(CreateClaim(string.Format("  

{0}/{1}",providerClaimType,userProfileProperty.Key),userProfileProperty.Value, 
 claimValueTypeString));

}}

 public static Dictionary<String, String> GetUserProfileProperties(string user)
    {
        Dictionary<String, String> userProfilePropList = new Dictionary<string, string>();
        SPSecurity.RunWithElevatedPrivileges(() =>
        {
            ClaimsEntities claimsDb = new ClaimsEntities();
            // This can return multiple Properties for the provider, even if Value is an exact match.  Need to do a secondary filter on Property.
            var userProfileValues = from upp in claimsDb.UserProfileProperties
                                  where upp.UserProfile.NTName == user
                                  select upp;


            foreach (UserProfileProperty upp in userProfileValues)
            {
                userProfilePropList.Add(upp.PropertyName, upp.PropertyVal);
            }

        });

        return userProfilePropList;

    }

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.