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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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