Friday, January 28, 2011

How to save user profile properties programmatically in sharepoint 2010?

Introduction:
In this article we will discuss how to save user profile properties programmatically in SharePoint 2010 using SharePoint 2010 object model? You can also check my last article on get user profiles details programmatically in sharepoint 2010.
Description:
Here is the code to save user profile properties programmatically in SharePoint 2010.

string accountName = SPContext.Current.Web.CurrentUser.LoginName;
Logger.info("ProfileUserControl:btnSave_Click() Saving user profile properties.");
SPSecurity.RunWithElevatedPrivileges(delegate()
{

SPSite sc = new SPSite(SPContext.Current.Site.ID);
SPServiceContext context = SPServiceContext.GetContext(sc);
//ServerContext context = ServerContext.GetContext(sc);
HttpContext currentContext = HttpContext.Current;
HttpContext.Current = null;
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile userProfile = profileManager.GetUserProfile(accountName);

userProfile[PropertyConstants.WorkPhone].Value = txtWorkPhone.Text.Trim();
userProfile[PropertyConstants.CellPhone].Value = txtMobilePhone.Text.Trim();
userProfile[PropertyConstants.HomePhone].Value = txtHomePhone.Text.Trim();
userProfile[PropertyConstants.School].Value = txtSchool.Text.Trim();
userProfile[PropertyConstants.Skills].Value = txtSkills.Text.Trim();
userProfile.Commit();
HttpContext.Current = currentContext;
//UserProfile userProfile = SPSparshService.GetUserProfile("");
});

2 comments:

  1. Don't you have to dispose the new SPSite or wrap it into a using?

    ReplyDelete
  2. SPContext we are using, So I think we do not need to dispose. You can check some articles here. http://www.fewlines4biju.com/2013/01/which-sharepoint-2010-objects-should.html

    ReplyDelete