Wednesday, September 14, 2011

Update User Profile Programmatically Sharepoint 2010

You can update properties of existing profile using object model as demonstrated below. UserProfileManager class can be used for this purpose. UserProfileManager can be found under namespace Microsoft.Office.Server.UserProfiles.


Code to update user profile:

using (SPSite _site = new SPSite("http://anmol"))
{
       SPServiceContext _context = SPServiceContext.GetContext(_site);

       UserProfileManager _profileManager = new UserProfileManager(_context);
       
       string _userName = "domain\\AnmolRehan"
       UserProfile _profile = _profileManager.GetUserProfile(_userName);
       _profile[PropertyConstants.AboutMe].Value = "This is about me";
       _profile[PropertyConstants.Manager].Value = "domain\Manager1";
       _profile[PropertyConstants.Skills].Value = "Skill1, Skill2";
       _profile.Commit();

}


PropertyConstants contains the property constants to be passed into the indexer of the UserProfile object. All fields in the PropertyConstants class are string values that contain the names of properties that hold user profile information.


How to create user profile programmatically?



4 comments:

  1. What is the method to add Value in custom property ?

    ReplyDelete
  2. you can update custom property like
    _profile["MyCustomProperty"] = "set new val";

    and you can also add custom property to profile like:

    if (_profileManager .Properties.GetPropertyByName("MyCustomProperty") == null)
    {
    PropertyCollection properties = _profileManager.Properties;
    Property property = properties.Create(false);
    property.Name = "MyCustomProperty";
    property.DisplayName = "MyCustomProperty";
    property.Type = PropertyDataType.String;
    property.Length = 255;
    property.IsUserEditable = true;
    property.PrivacyPolicy = PrivacyPolicy.OptIn;
    property.DefaultPrivacy = Privacy.Public;

    properties.Add(property);
    }

    ReplyDelete
  3. Is it possible to edit other users profile properties programmatically ... I have the app pool acct as the user profile administrator with full control on user profile service application.. Still I'm not able to update .. I'm running the code under elevated previleges ... I have network service as my app pool acct.....

    ReplyDelete
    Replies
    1. Please try to use Domain account in place of Network Service

      Delete

Popular Posts