Steve Atkinson
Steve Atkinson

Reputation: 1239

How can I create an "Empty" sharepoint site page and add webparts later

I want to use the Graph API to create a page on a sharepoint site, and then later add some HTML webparts to it. I can get the example page here working in Python using the requests library exactly as shown on that page but if I remove the inner text I get errors. What I want to do is something like this, and then later add the HTML webparts in a separate or future request:

def create_two_column_page(headers, site_id, page_name, page_title):
    print(f"Creating page {page_name} with title {page_title} ...")
    url = f"{ACTIVE_DIRECTORY_GRAPH_API_URL_PREFIX}/sites/{site_id}/pages"
    page_payload = {
        "@odata_type": "#microsoft.graph.sitePage",
        "name": page_name,
        "title": page_title,
        "canvasLayout": {
            "verticalSection": {
                "columns": [
                    {"id": "1", "width": 6, "webparts": []},
                    {"id": "2", "width": 6, "webparts": []}
                ]
            }
        }
    }
    response = requests.post(url, headers=headers, 
    json=page_payload, verify=False)

I get a 400 back with a rather cryptic "invalidRequest", "innerError" error message with no useful information. The bearer token is fine as I can call other APIs to list site pages etc.

Is what I am trying to do not possible? I can work around it by creating the example site and later use a HTTP PATCH to update the webpart but that feels very clunky?

Advice please!

Upvotes: 0

Views: 39

Answers (0)

Related Questions