Create SharePoint Online Site using REST API

James 41 Reputation points
2021-12-08T22:19:35.393+00:00

Hey guys, currently I am trying to create a modern site using the SharePoint REST API for SharePoint online. I followed this link https://learn.microsoft.com/en-us/sharepoint/dev/apis/site-creation-rest#create-a-modern-site and managed to create a site using Power Automate. However, I was wondering is it possible to call this endpoint using a curl request or through code? If so, will I need some authentication token and where would I get it?

Thanks

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

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-12-09T02:59:51.763+00:00

    Hi @James ,
    I will recommend you to use App-Only to access SharePoint Online. Please refer to the following steps to grant access.
    https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
    And here is the ajax call to get access token:

    $.ajax({  
        type: 'POST',  
        crossDomain: true,  
        url: 'https://accounts.accesscontrol.windows.net/<tenantID>/tokens/OAuth/2',  
        headers: {  
            "content-type": "application/x-www-form-urlencoded"  
        },  
        data: {  
            "grant_type": "client_credentials",  
            "client_id": "<ClientID>@<TenantID>",  
            "client_secret": "<ClientSecret>",  
            "resource": "00000003-0000-0ff1-ce00-000000000000/<sitename>.sharepoint.com@<TenantID>"  
        },  
        success: function(data) {  
            //data.token_type returns "Bearer"  
            //data.access_token returns < AccessToken >  
            var at = data.token_type + " " + data.access_token;  
          //caal the REST API with the at variable in header  
      
        },  
        error: function(data, errorThrown, status) {  
      
        }  
    });  
    

    Here is the detailed steps for your reference:
    https://global-sharepoint.com/sharepoint-online/in-4-steps-access-sharepoint-online-data-using-postman-tool/

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.


    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.