Find if user is Owner of a Sharepoint Site

Sanju Varghese 6 Reputation points
2021-03-11T02:36:31.213+00:00

In my SPFX webpart, I retrieve a list of sites that the user has access to. For each of the sites, I need to find out if the user is a site owner of that site. One approach which I thought was

  1. Get current user's user id in that site through: http://server/site/_api/Web/ currentUser
  2. Get all groups that the user belongs to in that site using https://server/sites/projects/_api/web/GetUserById(user id)/Groups
  3. Loop through results and find if title contains "Owner".

But this method could be flawed as I could create a group which has owner permissions and which does not have the word Owner in the group name/title.

So could someone please tell me how to do this? Graph Api approach is not being considered.

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sanju Varghese 6 Reputation points
    2021-03-11T05:21:35.023+00:00

    Resolved by using pnpjs

    web.getCurrentUserEffectivePermissions().then((permissions)=>{
    
      if(web.hasPermissions(permissions,PermissionKind.ManageWeb) && web.hasPermissions(permissions,PermissionKind.ManagePermissions)&& web.hasPermissions(permissions,PermissionKind.CreateGroups))
      {
        console.log("User is site owner");
      }
      else
      {
        console.log("User is not site owner");
      }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.