1

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.

3

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("user.name@tenantname.onmicrosoft.com")
.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");

        });
    });

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.