How to create a SharePoint site via Microsoft Graph API

Introduction

Since the Microsoft Graph API was introduced, there was a lack of possibility to create a SharePoint site. Fortunately, the Microsoft Graph API is still evolving and now the beta version of Graph API supports creating SharePoint sites.

Site creation

With new endpoint POST https://graph.microsoft.com/beta/sites you can create either Communication site, Team site, or Team site without Microsoft 365 group.

Microsoft Graph API team also introduced new permission Sites.Create.All which is the least privileged permission to create SharePoint sites. You can still use Site.FullControl.All permission, but it is the highest privileged permission and gives you the full control above all SharePoint sites and lists.

Let's deep dive into how to create different types of SharePoint sites.

Create Communication site

To create a new SharePoint site, you need to send a POST request to the https://graph.microsoft.com/beta/sites endpoint with the request body containing the following properties:

  • name: The name of the SharePoint site
  • webUrl: The URL of the SharePoint site
  • locale: The locale of the SharePoint site (e.g., "en-US")
  • shareByEmailEnabled: A boolean value indicating whether sharing by email is enabled
  • description: A description of the SharePoint site
  • template: The template to use for the SharePoint site (e.g., "sitepagepublishing" for Communication site)
  • ownerIdentityToResolve: An object containing the email or object id of the owner of the SharePoint site

In general, the request body is almost same no matter of what type of site you want to create. The only difference is the value of the template property which defines what type of site you want to create.

The possible values for the template property are:

  • sitepagepublishing: Communication site
  • sts: Team site (no Microsoft 365 group)
  • group: Team site
POST https://graph.microsoft.com/beta/sites

{
  "name": "Communication Site Test",
  "webUrl": "https://4wrvkx.sharepoint.com/sites/commsite1",
  "locale": "en-US",
  "shareByEmailEnabled": false,
  "description": "Test Site Description",
  "template": "sitepagepublishing",
  "ownerIdentityToResolve": {
    "email": "MartinMachacek@4wrvkx.onmicrosoft.com"
  }
}

If you omit the ownerIdentityToResolve in the request body, the site will be created with the user who sent the request as the owner of the site.

Once the request is sent, you should receive 202 Accepted response. Now, you need to check the response headers and search for the location header.

{
    "cache-control": "no-store, no-cache",
    "client-request-id": "0955a0c2-8f81-50e7-d8cd-77fc5c86d86b",
    "content-length": "0",
    "location": "https://4wrvkx.sharepoint.com/_api/v2.1/sites/getOperationStatus(operationId='aHR0cHM6Ly80d3J2a3guc2hhcmVwb2ludC5jb20vc2l0ZXMvY29tbXNpdGUx')",
    "request-id": "cf4bc460-7862-481e-9112-98fea9f87281"
}

Get site creation status

Provisioning of a SharePoint site is long running process. To monitor creation status, you need to send a GET request to the /beta/sites/getOperationStatus(operationId='{siteOperationId}') endpoint. The siteOperationId is the value from the location header, but you need to extract the part after operationId=.

You may noticed that the location header contains SharePoint REST API endpoint. It's a bug in the beta version. The correct URL should be the https://graph.microsoft.com/beta/sites/getOperationStatus(operationId='{siteOperationId}').

The response will looks like this:

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#microsoft.graph.richLongRunningOperation",
    "id": "aHR0cHM6Ly80d3J2a3guc2hhcmVwb2ludC5jb20vc2l0ZXMvdGVhbXNpdGUyMQ==",
    "resourceId": "3a512673-69d5-4ae5-a8dc-01424633ac0c",
    "resourceLocation": "https://4wrvkx.sharepoint.com/sites/teamsite21",
    "status": "succeeded"
}

Check the status property to see if the site creation is still in progress (notStarted or running) or if it has completed (succeeded or failed).

The resourceId should be the site id of the newly created SharePoint site. Currently, the beta version doesn't return the correct site id (which has the format like tenant.sharepoint.com,{guid},{guid}). You will need to get the site resource by relative site path:

GET https://graph.microsoft.com/beta/sites/4wrvkx.sharepoint.com:/sites/teamsite21

Create Team site without Microsoft 365 group

To create a team site without Microsoft 365 group, you need to send a POST request to the https://graph.microsoft.com/beta/sites endpoint with the same request body as for Communication site, but with different value sts of the template property.

POST https://graph.microsoft.com/beta/sites

{
  "name": "Team Site Test",
  "webUrl": "https://4wrvkx.sharepoint.com/sites/teamsite1",
  "locale": "en-US",
  "shareByEmailEnabled": false,
  "description": "Team Test Site Description",
  "template": "sts",
  "ownerIdentityToResolve": {
    "email": "MartinMachacek@4wrvkx.onmicrosoft.com"
  }
}

Create Team site

To create a team site, you need to send a POST request to the https://graph.microsoft.com/beta/sites endpoint with the same request body as for Communication site, but with different value group of the template property.

POST https://graph.microsoft.com/beta/sites

{
  "name": "Developers Team Site Test",
  "webUrl": "https://4wrvkx.sharepoint.com/sites/devteamsite1",
  "locale": "en-US",
  "shareByEmailEnabled": false,
  "description": "Developers Team Test Site Description",
  "template": "group",
  "ownerIdentityToResolve": {
    "email": "MartinMachacek@4wrvkx.onmicrosoft.com"
  }
}

Creating multiple sites

If you want to create multiple SharePoint sites, you can use the batch endpoint. In one batch request, you can create up to 20 SharePoint sites.

POST https://graph.microsoft.com/beta/$batch

{
    "requests": [
        {
            "url": "/sites",
            "method": "POST",
            "id": "1",
            "body": {
                "name": "Team Site Test",
                "webUrl": "https://4wrvkx.sharepoint.com/sites/teamsite",
                "locale": "en-US",
                "shareByEmailEnabled": false,
                "description": "Team Test Site Description",
                "template": "sts",
                "ownerIdentityToResolve": {
                    "email": "MartinMachacek@4wrvkx.onmicrosoft.com"
                }
            },
            "headers": {
                "Content-Type": "application/json"
            }
        },
        {
            "url": "/sites",
            "method": "POST",
            "id": "2",
            "body": {
                "name": "Communication Site",
                "webUrl": "https://4wrvkx.sharepoint.com/sites/commsite",
                "locale": "en-US",
                "shareByEmailEnabled": false,
                "description": "Communication Test Site Description",
                "template": "sitepagepublishing",
                "ownerIdentityToResolve": {
                    "email": "MartinMachacek@4wrvkx.onmicrosoft.com"
                }
            },
            "headers": {
                "Content-Type": "application/json"
            }
        }
    ]
}

The response will contain the location header for each request.

{
    "responses": [
        {
            "id": "2",
            "status": 202,
            "headers": {
                "Location": "https://4wrvkx.sharepoint.com/_api/v2.1/sites/getOperationStatus(operationId='aHR0cHM6Ly80d3J2a3guc2hhcmVwb2ludC5jb20vc2l0ZXMvdGVhbXNpdGUyMQ==')",
                "Cache-Control": "no-store, no-cache"
            },
            "body": null
        },
        {
            "id": "1",
            "status": 202,
            "headers": {
                "Location": "https://4wrvkx.sharepoint.com/_api/v2.1/sites/getOperationStatus(operationId='aHR0cHM6Ly80d3J2a3guc2hhcmVwb2ludC5jb20vc2l0ZXMvdGVhbXNpdGUyMA==')",
                "Cache-Control": "no-store, no-cache"
            },
            "body": null
        }
    ]
}

To check the status of multiple sites creation, you can use batch endpoint again to send multiple GET requests to the https://graph.microsoft.com/beta/sites/getOperationStatus(operationId='{siteOperationId}') endpoint.

POST https://graph.microsoft.com/beta/$batch

{
    "requests": [
        {
            "url": "/sites/getOperationStatus(operationId='aHR0cHM6Ly80d3J2a3guc2hhcmVwb2ludC5jb20vc2l0ZXMvdGVhbXNpdGUyMQ==')",
            "method": "GET",
            "id": "1"
        },
        {
            "url": "/sites/getOperationStatus(operationId='aHR0cHM6Ly80d3J2a3guc2hhcmVwb2ludC5jb20vc2l0ZXMvdGVhbXNpdGUyMA==')",
            "method": "GET",
            "id": "2"
        }
    ]
}

Conclusion

The new Microsoft Graph API endpoint to create SharePoint sites is a great addition to the Graph API. It allows SharePoint administrators to automate the creation of all types of SharePoint sites via a single API endpoint. When using the batch endpoint, you can create multiple sites in one request, which is very useful for bulk site provisioning scenarios.

0
Buy Me a Coffee at ko-fi.com
An error has occurred. This application may no longer respond until reloaded. Reload x