1

Is there a REST API for updating User Profiles?

I've found the one to read them, but can't seem to work out how to update them.

User Profile Properties With REST API–SharePoint Office 365.

Thanks.

0

Update SingleValueProperty

function updateSingleValue(){
var Headers = {
        'X-RequestDigest': $("#__REQUESTDIGEST").val(),
        "accept": "application/json; odata=nometadata",
        "content-type": "application/json;odata=nometadata"
    };

    var aboutData = {
        'accountName': "i:0#.f|membership|user@yourtenant.onmicrosoft.com",
        'propertyName': 'AboutMe', //can also be used to set custom single value profile properties
        'propertyValue': 'A tech savvy and computer geek which lives by earning bread on computers, passes time by watching/sitcoms and playing games on computers, also a loving husband, father, brother, son and friend and most importantly trying to be human being :)'
    }

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetSingleValueProfileProperty",
        type: "POST",
        headers: Headers,
        data: JSON.stringify(aboutData),
        success:profileUpdateSuccess ,
        error: profileUpdateFail
    });

function profileUpdateSuccess(response){
    console.log(response);

}
function profileUpdateFail(jqxr, errorCode, errorThrown){
    console.log(jqxr);

}
}

Updating Multi Value property

function updateMultiValue(){

var Headers = {
            'X-RequestDigest': $("#__REQUESTDIGEST").val(),
            "accept": "application/json; odata=nometadata",
            "content-type": "application/json;odata=nometadata"
        };

        var aboutData = {
            'accountName': "i:0#.f|membership|user@yourtenant.onmicrosoft.com",
            'propertyName': 'SPS-Skills', //can also be used to set custom single value profile properties
            'propertyValue': ["SharePoint", "Office 365", "Node JS", "Ionic", "Gsuite"]
        }

        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetMultiValuedProfileProperty",
            type: "POST",
            headers: Headers,
            data: JSON.stringify(aboutData),
            success:profileUpdateMultiSuccess ,
            error: profileUpdateMultiFail
        });



}

function profileUpdateMultiSuccess(response){
        console.log(response);

    }
    function profileUpdateMultiFail(jqxr, errorCode, errorThrown){
        console.log(jqxr);

    }

Hope this helps...Happy coding..!!!

0

You can also set user profile properties using JSOM:

SP.SOD.executeFunc("sp.js", "SP.ClientContext", function(){
        SP.SOD.registerSod("sp.userprofiles.js", SP.Utilities.Utility.getLayoutsPageUrl("sp.userprofiles.js"));
        SP.SOD.executeFunc("sp.userprofiles.js", "SP.UserProfiles.PeopleManager", SetCurrentUserProperties);
    });

    var userProfileProperties;

    function SetCurrentUserProperties(){

        //Get Current Context   
        var clientContext = SP.ClientContext.get_current();

        //Get Instance of People Manager Class
        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

        //Get properties of the current user
        userProfileProperties = peopleManager.getMyProperties();

        //Get only the accountname instead of all the properties. 
        clientContext.load(userProfileProperties, "AccountName");

        //Execute the Query.
        clientContext.executeQueryAsync(function(){

            //Get the account name of the current user. It will be in the following format: "i:0#.f|membership|username@yoursite.onmicrosoft.com"
            var currentUserAccountName = userProfileProperties.get_accountName();

            //Set a single value property
            peopleManager.setSingleValueProfileProperty(currentUserAccountName, "AboutMe", "Value updated from JSOM!");

            //Set a multivalue property 
            var multipleValues = ["SharePoint", "Office 365", "Architecture"];
            peopleManager.setMultiValuedProfileProperty(currentUserAccountName, "SPS-Skills", multipleValues);

        //Execute the Query.
            clientContext.executeQueryAsync(function(){

                console.log("properties updated!");
            }, 
            function(sender,args){
                //On Error
                console.log(args.get_message());
            });

        }, function(sender,args){
            //On Error
            console.log(args.get_message());
        });

    }

Reference:

Set user profile properties using JSOM & JavaScript

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.