Friday, January 8, 2021

get user profile property value when privacy for property set to “Private” (only me)

 RunWithElevatedPrivileges required because only under elevated code .
we can run that WindowsIdentity.GetCurrent() returns application pool account


SPSecurity.RunWithElevatedPrivileges(delegate

{

    var context = HttpContext.Current;

    HttpContext.Current = new HttpContext(new HttpRequest(string.Empty, SPContext.Current.Site.Url, string.Empty), new HttpResponse(new StringWriter()));

    HttpContext.Current.User = new GenericPrincipal(WindowsIdentity.GetCurrent(), new string[0]);

    var manager = new UserProfileManager(SPServiceContext.GetContext(HttpContext.Current));

    var profile = manager.GetUserProfile(@"domain\name");

    var value = profile["my_property"].Value;

    HttpContext.Current = context;

});  

No comments:

Post a Comment