sharepointnullcustom-fieldsevent-receiversharepoint-userprofile

[SharePoint 2010]Value from a User Profile custom field returns Null


In an EventReceiver I call this method GetPernNr on Item Added:

public override void ItemAdded(SPItemEventProperties properties)
{
        SPSite site = properties.Web.Site;
        using (SPWeb currentWeb = site.OpenWeb(properties.Web.ID))
        {
             .....

             perNr = UserProfileUtils.GetPernNr(currentWeb, assignedTo.ToString());
             .....
        }
}

where assignedTo is a SPUser.

public static string GetPernNr(SPWeb web, string accountName)
{
        string perNr = string.Empty;
        UserProfile upUser = null;
        try
        {
            PermissionSet ps = new PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
            ps.Assert();

           SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               using (SPSite siteColl = new SPSite(web.Site.ID))
               {
                   SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
                   UserProfileManager upm = new UserProfileManager(serviceContext);
                   if (upm.UserExists(accountName))
                   {
                       upUser = upm.GetUserProfile(accountName);
                       if (upUser["PersonNumber"] != null)
                       {
                           perNr = upUser["PersonNumber"].Value.ToString();
                       }
                   }
               }
           });
        }
        catch (Exception ex)
        {
            ..
        }
        finally { System.Security.CodeAccessPermission.RevertAssert(); }
        return perNr;
    }

It's strange, this code works when I try to get value from a default field in UserProfile (Office, Manager, etc). And also works when I call this method outside EventReceiver, but in my case, upUser["PersonNumber"].Value returns null. Any help will be much appreciated


Solution

  • Did you check the custom property permission in the central admin.

    Central Administration -> Edit User Profile Property -> Policy Settings

    Make default privacy policy to everyone and then try.