Is there any way to add a user as Site Collection Administrator in SharePoint Online Sites using Rest API or pnp js?. I am using SPFx react js webpart. I have code in c#, but I cannot use it in my SPFx webpart.
1 Answer
You can use the pnpjs library to add the site collection admin.
Firstly, you need to add the below import statement:
import { sp, WebEnsureUserResult, UserUpdateResult } from "@pnp/sp";
After that, you can use the code as below:
sp.web.ensureUser("[email protected]")
.then((d: WebEnsureUserResult) => {
var id = d.data.Id;
console.log(d.data);
sp.web.getUserById(id).update({
"IsSiteAdmin":true
}).then((d:UserUpdateResult) =>{
console.log(d);
console.log("Successfully added admin");
});
});