Skip to content

Latest commit

 

History

History
72 lines (60 loc) · 2.83 KB

python-app.md

File metadata and controls

72 lines (60 loc) · 2.83 KB

Yes, you can use Python to create a new SharePoint site based on a specific site design and site scripts by leveraging the Microsoft Graph API or the SharePoint REST API. Python doesn’t have a direct SDK for SharePoint but can interact with these APIs by sending HTTP requests.

Here’s a general approach using the Microsoft Graph API and the requests library in Python:

Steps to create a new SharePoint site with a site design:

1. Register an Azure AD App:

• Go to the Azure portal and register a new app with the required permissions (such as Sites.Manage.All for creating new sites). • Generate a client ID and client secret for authentication.

2. Obtain an OAuth token:

• Use the registered app credentials to get an OAuth token. This token is needed to authenticate your API calls to Microsoft Graph.

3. Create a Communication Site or Team Site using Microsoft Graph API:

• Use the Microsoft Graph API endpoint to create a new site. • Provide the relevant siteDesignId to apply a specific site design during site creation.

4. Apply site scripts:

• Once the site is created, you can apply additional site scripts using the SharePoint REST API or continue using the Graph API for further customization.

Pseudocode Outline:

  1. Register an app in Azure AD and gather credentials.
  2. Use requests to get an OAuth token.
  3. Create a SharePoint site with a POST request to the Graph API.
  4. Pass the siteDesignId and additional parameters in the request.

Example Code:

import requests
from requests.auth import HTTPBasicAuth

# Azure AD app credentials
client_id = "your_client_id"
client_secret = "your_client_secret"
tenant_id = "your_tenant_id"
site_design_id = "your_site_design_id"  # The ID of the site design you want to apply

# Obtain an OAuth token from Azure AD
def get_access_token():
    url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
    payload = {
        "grant_type": "client_credentials",
        "client_id": client_id,
        "client_secret": client_secret,
        "scope": "https://graph.microsoft.com/.default"
    }
    response = requests.post(url, data=payload)
    response.raise_for_status()
    return response.json().get("access_token")

# Create a new SharePoint site
def create_sharepoint_site(access_token):
    url = "https://graph.microsoft.com/v1.0/sites"
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json"
    }
    site_data = {
        "displayName": "New Site",
        "description": "Description of the site",
        "siteCollection": {
            "hostname": "your-tenant.sharepoint.com",
            "root": {
                "siteDesignId": site_design_id  # Applying the site design
            }
        }
    }
    
    response = requests.post(url, json=site_data, headers=headers)
    if response.status_code ==

jajhj