effectiveBasePermissions Web API is Popping Up User Credentials Dailog

Abbas Aleid 0 Reputation points
2023-09-05T06:25:28.7666667+00:00

Greetings,

I want to check whether a sharepoint site exists/user has access to the site without popping up the credentials dialog.

Currently I'm using effectiveBasePermissions Web API but it's triggering the user credentials to pop-up.

The following is a sample code block from my project used to check the status of modern site in SharePoint:

  _checkModernWorkspaceStatus = async (): Promise<{ data: any, responseOk: boolean }> => {
    const { item, absoluteUrl, context } = this.props;
    try {
      const response = await context.spHttpClient.get(
        `${absoluteUrl}/modern/${item.OrgCode}/_api/web/effectiveBasePermissions`,
        SPHttpClient.configurations.v1,
        {
          headers: [
            ['accept', 'application/json;odata.metadata=none']
          ]
        }
      );

		return response.json();
catch (error) {      console.log('Error: ', error);    }  }

Is there an alternative or a replacement for this method to check whether a sharepoint site exists/user has access to the site without popping up the credentials dialog?

Best Regards,
Abbas.

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

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-09-06T06:30:57.63+00:00

    Hi @Abbas Aleid

    Do you want to implement authentication in code, thus avoiding the authentication window?

    You need to use the HttpClient class to send a POST request to Microsoft Access Control Service (ACS) to obtain an access token. You can use the following code sample:

    Here is link for your refernce:

    https://learn.microsoft.com/en-us/graph/auth-v2-user?tabs=http

    // Define the endpoint and parameters for the ACS request
    const acsEndpoint = 'https://accounts.accesscontrol.windows.net/tokens/OAuth/2';
    const acsParams = {
      grant_type: 'client_credentials',
      client_id: '<your-client-id>@<your-tenant-id>',
      client_secret: '<your-client-secret>',
      resource: '<your-resource-id>@<your-tenant-id>'
    };
    
    // Create a HttpClient object
    const httpClient = new HttpClient(this.props.context.serviceScope);
    
    // Send a POST request to the ACS endpoint with the parameters
    const acsResponse = await httpClient.post(acsEndpoint, HttpClient.configurations.v1, {
      body: JSON.stringify(acsParams),
      headers: new Headers({
        'Content-Type': 'application/x-www-form-urlencoded'
      })
    });
    
    // Parse the response as JSON and get the access token
    const acsResponseJson = await acsResponse.json();
    const accessToken = acsResponseJson.access_token;
    
    

    You can use the access token to call the SharePoint REST API

    _checkModernWorkspaceStatus = async (): Promise<{ data: any, responseOk: boolean }> => {
        const { item, absoluteUrl, context } = this.props;
        try {
          const response = await context.spHttpClient.get(
            `${absoluteUrl}/modern/${item.OrgCode}/_api/web/effectiveBasePermissions`,
            SPHttpClient.configurations.v1,
            {
              headers: [
                ['accept', 'application/json;odata.metadata=none'],
                ['Authorization', 'Bearer ${accessToken}']
                     
              ]
            }
          );
    
    		return response.json();
    catch (error) {      console.log('Error: ', error);    }  }
    

    For rest api authentication issues, please refer to this links:

    https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-rest-endpoints

    (The above code is for reference only. For specific situations, you need to modify the code according to your scenario)


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards

    Cheng Feng


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.