Update User Profile Property Using PNP JS in SharePoint online

In this blog. I have explained how to update the user profile property using pnp js library.

Note:- It works only on SharePoint online itself. not working for SharePoint on premises

First thing am going to retrieve all the properties of my office 365 account

import pnp from "@pnp/pnpjs"; 

//For demo purpose hard-coded the loginname of the user

let loginName = "i:0#.f|membership|vinodhvignesh@sharepointtechie.onmicrosoft.com";

pnp.sp.profiles.getPropertiesFor(loginName).then(resp => {
        let props = {}
        resp.UserProfileProperties.map(function(val){
            props[val.Key] = val.Value
        })

        console.log("props", props)
});

It will return all the properties of the supplied user

Set “setSingleValueProfileProperty” for the user

So now “AboutMe” property is returned as empty am going to update this using “setSingleValueProfileProperty” Method

let loginName = "i:0#.f|membership|vinodhvignesh@sharepointtechie.onmicrosoft.com";

//Pass the login name, property, and property value as strings

// Hard-coded the property value 'techie'

pnp.sp.profiles.setSingleValueProfileProperty(loginName, 'AboutMe', 'Techie').then(res => {
    console.log("Update success")
})

It’s updated successfully see the response below

Set “setMultiValueProfileProperty” for the user

So now “SPS-Skills” property is returned as empty am going to update this using “setMultiValuedProfileProperty” Method

let skill = ["sharepoint", "flow", "powerapps"];

//Pass the login name, property, and property value as strings

pnp.sp.profiles.setMultiValuedProfileProperty(loginName, 'SPS-Skills', skill).then(res => {
    console.log("Update success")
})

It’s updated successfully see the response below

So now you are able to update the user profile properties of the user. you can also do the same for multiple user accounts

Happy SharePointing !….

Add a Comment

Your email address will not be published. Required fields are marked *