0

I am writing a WebMethod in an Application Page for a SharePoint 2010 site, that updates the current user's profile properties.

Web Method:

[WebMethod]
public static string setProfileProperties(ProfileProperties data)
{
    if (data == null)
    {
        return "{\"result\": \"" + false + "\"}";
    }
    SPSite site = SPContext.Current.Site;
    SPWeb web = SPContext.Current.Web;
    SPServiceContext context = SPServiceContext.GetContext(site);
    UserProfileManager upm = new UserProfileManager(context);
    string loginName = web.CurrentUser.LoginName;

    if (upm.UserExists(loginName))
    {
        UserProfile profile = upm.GetUserProfile(loginName);
        profile[PropertyConstants.WorkEmail].Value = data.email;
        profile[PropertyConstants.FirstName].Value = data.name;
        profile.Commit(); // error here!!
        return "{\"result\": \"" + true + "\"}";
    }
    return "{\"result\": \"" + false + "\"}";
}

NOTE: ProfileProperties is a custom object with fields such as name and email.

The data being passed in is already correct, yet when I commit the changes, I get a 500 server error. My assumption is permissions. Does anyone know how I can get my profile to save/commit changes? Thanks.

1
  • what is the value in loginname ? it should be in the format of domain\user – Gautam Sheth Jan 31 '17 at 5:54
0

Looks like server context is not available when you call the GetContext method.

Try below code:

[WebMethod]
public static string setProfileProperties(ProfileProperties data)
{

    SPUtility.ValidateFormDigest();

    SPWebApplication spWebApp = null;
    try
    {
        if (data == null)
        {
            return "{\"result\": \"" + false + "\"}";
        }
        SPSecurity.RunWithElevatedPrivileges(delegate()     
        { 
            //Create a SPSite Object with a valid Site collection address
            using (SPSite spSite = new SPSite("https://sitecollection"))
            {
                //Open the web object
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    //Create the Web Application object from the current site
                    spWebApp = spSite.WebApplication;    

                    //Create the server context
                    ServerContext context = ServerContext.GetContext(spSite);

                    //Update the HttpHandlerSPWeb with the web object and 
                    //the Microsoft.Office.ServerContext with the server context
                    if (HttpContext.Current != null)
                    {
                        if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)
                            HttpContext.Current.Items["HttpHandlerSPWeb"] = spWeb;
                        if (HttpContext.Current.Items["Microsoft.Office.ServerContext"] == null)
                            HttpContext.Current.Items["Microsoft.Office.ServerContext"] = context;
                    }

                    //Create the user profile object from the context object
                    UserProfileManager profileManager = new UserProfileManager(context);                                

                    string loginName = web.CurrentUser.LoginName; // account format should be "domain\\user";

                    if (profileManager.UserExists(loginName))
                    {
                        UserProfile profile = upm.GetUserProfile(loginName);
                        profile[PropertyConstants.WorkEmail].Value = data.email;
                        profile[PropertyConstants.FirstName].Value = data.name;
                        profile.Commit();
                        return "{\"result\": \"" + true + "\"}";
                    }

                    return "{\"result\": \"" + false + "\"}";
                }
            }
        });
    }
    catch (Exception ex)
    {   
        return "{\"result\": \"" + false + "\"}";
    }       

}

Reference - Faking SPContext

Update user profile properties programmatically

Also, ensure that "Allow user to edit values for this property is set to true".

enter image description here

6
  • This code gives me an access denied exception when changing the FormDigestSettings to false. – Benjamin Snider Jan 31 '17 at 13:33
  • Can you check updated answer ? – Gautam Sheth Jan 31 '17 at 14:13
  • Where is that setting within the site? I can't seem to find it. – Benjamin Snider Jan 31 '17 at 15:15
  • Go to central admin > manage service application > user profile application > user profile properties. There you will find the properties. Click on work email or first name and edit it. There you will find this setting. – Gautam Sheth Jan 31 '17 at 15:29
  • I've made it so both profile properties are editable as per your suggestion, however I'm still getting an access denied when changing the FormDigestSettings to false. – Benjamin Snider Jan 31 '17 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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