1

The code works. There is no error.

Please let me know if this is the best practice to read/write to the user profile property in SharePoint 2013.

Read:

string currentUser = SPContext.Current.Web.CurrentUser.LoginName;
                SPSite spSite = SPContext.Current.Site;

                try
                {
                    // Set the context of the site to the SPSite site
                    SPServiceContext context = SPServiceContext.GetContext(spSite);
                    //Create and instance of the UserProfileManager
                    UserProfileManager profileManager = new UserProfileManager(context);
                    UserProfile userProfile = profileManager.GetUserProfile(currentUser);
                    string customValue=userProfile["customPropertyName"].Value.ToString();
                }

Write:

string currentUser = SPContext.Current.Web.CurrentUser.LoginName;
                SPSite spSite = SPContext.Current.Site;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
                    System.Web.HttpContext.Current = null;
                    try
                    {
                        // Set the context of the site to the SPSite site
                        SPServiceContext context = SPServiceContext.GetContext(spSite);
                        //Create and instance of the UserProfileManager
                        UserProfileManager profileManager = new UserProfileManager(context);
                        UserProfile userProfile = profileManager.GetUserProfile(currentUser);
                        userProfile["customPropertyName"].Value = "True";
                        userProfile.Commit();
                    }                   

                    finally
                    {
                        System.Web.HttpContext.Current = currentContext;
                    }
                });

Do I have to use for read also the below code as used in case of write?

before try

 System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
                            System.Web.HttpContext.Current = null;

in finally:

 System.Web.HttpContext.Current = currentContext;

Why is it that we have to do like this for the write?

1 Answer 1

2

You should not have to get the current user. Calling GetUserProfile(bool) will get the user profile of the current user, and optionally create the user profile if it does not exist. The user should have permission to update their own profile, so it is very bad practice to use RunWithElevatedPriveledges. You should only need the Service Context for the request, not sure why you need any of the other code.

You have to decide what is best for your case. It is possible that a user will hit your page without a Profile, if that happens you have to determine what to do in code. Create a profile or handle the error and indicate that they need a profile to use the page. This is not something set in stone. You have to decide based on your business and how you manage Profiles in your farm.

RunWithElevatedPrivileges = RWEP as a shorthand. Again, this is only to be used as a last resort, and should be avoided then too. If you set up the properties on the User Profile correctly, this should be unnecessary.

8
  • can you provide a reference to support that profileManager.GetUserProfile() without a parameter will get the user profile of the current user
    – variable
    Commented Apr 8, 2014 at 12:37
  • I am not clear on why you need that at all. The code inside your try block with the site reference should be enough. As long as the properties in the IPA are configured so that the user can update them. Commented Apr 8, 2014 at 12:38
  • My reference for writing value to user profile property: tejinderrai.org/2011/07/25/…
    – variable
    Commented Apr 8, 2014 at 12:39
  • Also, if we dont set: System.Web.HttpContext.Current = null; before the TRY block, then, we get error on COMMIT.
    – variable
    Commented Apr 8, 2014 at 12:40
  • Sorry, should have been the GetUserProfile(bool) version. Then you decide if you want to trip a profile create. Commented Apr 8, 2014 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.