Question

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

Was it helpful?

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.

OTHER TIPS

Here is the steps for SharePoint Server 2013:

Central Admin > Application Management > Manage service applications > Your User Profile Application > Manage User Properties

Select Edit option from property menu.

Now Under "Policy Settings":
Set Default Privacy Setting: Everyone

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top