已答复 Populating a list with data from a User's Profile

  • Tuesday, May 15, 2012 6:15 PM
     
     

    I'm using SharePoint Server 2010 Standard.  I would like create an event receiver that takes data from a person/group column and populates another column using that person's User Profile.  Something like:

    Assigned To     Business Unit
    CapnGrizzle     Awesome Unit

    where 'Awesome Unit' is the value from a custom property in the User Profile Service (Business Unit).  I have not found that many helpful links on how to go about creating an list event receiver that populates this data on item creation.  Can you lead me to some examples of similar solutions?  I would like to avoid using SPServices.SPGetCurrentUser since I have to do this for mulitple columns and the data may not always be on the userdisp.aspx page.


    James Grizzle
    ---
    http://www.linkedin.com/in/jamesgrizzle


    • Edited by CapnGrizzle Tuesday, May 15, 2012 6:16 PM
    •  

All Replies

  • Tuesday, May 15, 2012 6:49 PM
     
     Answered Has Code

    Hello!

    Use such code:

    public override void ItemAdded(SPItemEventProperties properties)
    {
        EventFiringEnabled = false;
    
        var user = GetUserInfo(properties.AfterProperties["AssignedTo"].ToString());
        properties.ListItem["CapnGrizzle"] = user["Awesome Unit"].Value;
        properties.ListItem.Update();
                
        EventFiringEnabled = true;
    
        base.ItemAdded(properties);
    }
    
    private UserProfile GetUserInfo(string accountName)
    {
        var profileManager = new UserProfileManager(SPServiceContext.Current);
        return accountName != string.Empty
                    ? profileManager.GetUserProfile(accountName)
                    : profileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.RawSid);
    }


    My contributions: SharePoint 2010 Solution Installer
    SharePoint How-To: Create WebPart Page programmatically

  • Thursday, May 17, 2012 3:22 PM
     
      Has Code

    Thank you!  Your post certainly lead me to to the correct answer.  My final solution is below:

    public override void ItemUpdating(SPItemEventProperties properties)
           {
               
               EventFiringEnabled = false;
    
               var userID = properties.AfterProperties["Assigned To"] as string;
               SPFieldUserValue userName = new SPFieldUserValue(properties.Web, userID);
               string thisUser = userName.User.LoginName;
               
               var user = GetUserInfo(thisUser);
    
               properties.AfterProperties["Business Unit"] = user["BU"].Value;
               EventFiringEnabled = true;
    
               base.ItemUpdating(properties);
              
           }
    
           private UserProfile GetUserInfo(string accountName)
           {
               using (SPSite site = new SPSite("http://sp2010/pwi/3PRO"))
               {
                   SPServiceContext context = SPServiceContext.GetContext(site);
                    UserProfileManager profileManager = new UserProfileManager(context);
                         
                    UserProfile testUser = profileManager.GetUserProfile(accountName);
                    return testUser;             
               }
           }


    James Grizzle
    ---
    http://www.linkedin.com/in/jamesgrizzle