Creating a SharePoint Online site page with a custom webpart using Graph API

Rob Ireland 0 Reputation points
2023-02-28T14:29:51.5733333+00:00

I am using the Microsoft Graph API to create a Site Page in a Modern Communications site using the following

https://learn.microsoft.com/en-us/graph/api/sitepage-create?view=graph-rest-beta

The page contains a custom SPFx webpart that has 1 property. I would like to add the webpart to the page and set the property of the webpart using the Graph API.

The page is created however the webpart is not added to the page. Although the response does not contain any error.

The URL for the POST is

https://graph.microsoft.com/beta/sites/{siteidguid}/pages

The body of the request is pretty straightforward for now.

{
  "name":"Testpage21.aspx",
  "publishingState":{"level":"published","versionId":"0.1"},
  "title":"Testpage21",
"webParts": [
        {
            "@odata.type": "#microsoft.graph.standardWebPart",
            "id": "484831c5-f9ed-495d-927e-1f6adb60af0c",
            "webPartType": "140fc165-6bee-4710-a399-76eb27620c67",
            "data": {
                "audiences": [],
                "dataVersion": "1.0",
                "description": "description",
                "title": "webpart title",
                "properties": {},
                "serverProcessedContent": {
                    "htmlStrings": [
                        {
                            "key": "description",
                            "value": "1"
                        }
                    ],
                    "searchablePlainTexts": [],
                    "links": [],
                    "imageSources": []
                }
            }
        }
    ]
}

Any pointers gratefully received

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

3 answers

Sort by: Most helpful
  1. Rob Ireland 0 Reputation points
    2023-02-28T21:59:06.7733333+00:00

    We finally resolved this by using the following body

    {
        "eTag": "\"{E87E3892-74DC-42B2-8C25-29C5DA6E7657},75\"",
        "id": "e87e3892-74dc-42b2-8c25-29c5da6e7657",
        "name": "pagename.aspx",
        "title": "Page Title",
        "pageLayout": "article",
        "promotionKind": "page",
        "showComments": false,
        "showRecommendedPages": false,
        "parentReference": {
            "siteId": "6a5e97bb-af5b-444b-9958-9a597eb1db6a"
        },
        "contentType": {
            "id": "0x0101009D1CB255DA76424F860D91F20E6C4118008B56984027CAEF4083F170D45D3253A8",
            "name": "Site Page"
        },
        "publishingState": {
            "level": "published",
            "versionId": "15.0"
        },
        "canvasLayout": {
            "horizontalSections": [
                {
                    "layout": "oneColumn",
                    "id": "1",
                    "emphasis": "none",
                    "columns": [
                        {
                            "id": "1",
                            "width": 12,
                            "webparts": [
                                {
                                    "@odata.type": "#microsoft.graph.standardWebPart",
                                    "id": "0fa3f654-229b-41d2-a0d3-be6d2977372e",
                                    "webPartType": "140fc165-6bee-4710-a399-76eb27620c67",
                                    "data": {
                                        "audiences": [],
                                        "dataVersion": "1.0",
                                        "description": "customeWebPartName",
                                        "title": "SharePoint title",
                                        "properties": {
                                            "key": "description",
                                            "value": "1"
                                        },
                                        "serverProcessedContent": {
                                            "htmlStrings": [
                                                {
                                                    "key": "description",
                                                    "value": "1"
                                                }
                                            ],
                                            "searchablePlainTexts": [],
                                            "links": [],
                                            "imageSources": []
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
    
    
    
    0 comments No comments

  2. Rob Ireland 0 Reputation points
    2023-02-28T22:00:59.4766667+00:00

    Resolved this by using the following body. Looks like the webpart should be included in a Horizontal and Vertical section.

    {
        "eTag": "\"{E87E3892-74DC-42B2-8C25-29C5DA6E7657},75\"",
        "id": "e87e3892-74dc-42b2-8c25-29c5da6e7657",
        "name": "pagename.aspx",
        "title": "Page Title",
        "pageLayout": "article",
        "promotionKind": "page",
        "showComments": false,
        "showRecommendedPages": false,
        "parentReference": {
            "siteId": "6a5e97bb-af5b-444b-9958-9a597eb1db6a"
        },
        "contentType": {
            "id": "0x0101009D1CB255DA76424F860D91F20E6C4118008B56984027CAEF4083F170D45D3253A8",
            "name": "Site Page"
        },
        "publishingState": {
            "level": "published",
            "versionId": "15.0"
        },
        "canvasLayout": {
            "horizontalSections": [
                {
                    "layout": "oneColumn",
                    "id": "1",
                    "emphasis": "none",
                    "columns": [
                        {
                            "id": "1",
                            "width": 12,
                            "webparts": [
                                {
                                    "@odata.type": "#microsoft.graph.standardWebPart",
                                    "id": "0fa3f654-229b-41d2-a0d3-be6d2977372e",
                                    "webPartType": "140fc165-6bee-4710-a399-76eb27620c67",
                                    "data": {
                                        "audiences": [],
                                        "dataVersion": "1.0",
                                        "description": "customeWebPartName",
                                        "title": "SharePoint title",
                                        "properties": {
                                            "key": "description",
                                            "value": "1"
                                        },
                                        "serverProcessedContent": {
                                            "htmlStrings": [
                                                {
                                                    "key": "description",
                                                    "value": "1"
                                                }
                                            ],
                                            "searchablePlainTexts": [],
                                            "links": [],
                                            "imageSources": []
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
    
    
    
    0 comments No comments

  3. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-03-01T01:58:44.3033333+00:00

    Hi @Rob Ireland

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    Creating a SharePoint Online site page with a custom webpart using Graph API

    Issue Symptom:

    Create a page with graph api, but the custom web part hasn't been added

    Solution:

    Resolved by using the following body. The webpart should be included in a Horizontal and Vertical section.

    {
        "eTag": "\"{E87E3892-74DC-42B2-8C25-29C5DA6E7657},75\"",
        "id": "e87e3892-74dc-42b2-8c25-29c5da6e7657",
        "name": "pagename.aspx",
        "title": "Page Title",
        "pageLayout": "article",
        "promotionKind": "page",
        "showComments": false,
        "showRecommendedPages": false,
        "parentReference": {
            "siteId": "6a5e97bb-af5b-444b-9958-9a597eb1db6a"
        },
        "contentType": {
            "id": "0x0101009D1CB255DA76424F860D91F20E6C4118008B56984027CAEF4083F170D45D3253A8",
            "name": "Site Page"
        },
        "publishingState": {
            "level": "published",
            "versionId": "15.0"
        },
        "canvasLayout": {
            "horizontalSections": [
                {
                    "layout": "oneColumn",
                    "id": "1",
                    "emphasis": "none",
                    "columns": [
                        {
                            "id": "1",
                            "width": 12,
                            "webparts": [
                                {
                                    "@odata.type": "#microsoft.graph.standardWebPart",
                                    "id": "0fa3f654-229b-41d2-a0d3-be6d2977372e",
                                    "webPartType": "140fc165-6bee-4710-a399-76eb27620c67",
                                    "data": {
                                        "audiences": [],
                                        "dataVersion": "1.0",
                                        "description": "customeWebPartName",
                                        "title": "SharePoint title",
                                        "properties": {
                                            "key": "description",
                                            "value": "1"
                                        },
                                        "serverProcessedContent": {
                                            "htmlStrings": [
                                                {
                                                    "key": "description",
                                                    "value": "1"
                                                }
                                            ],
                                            "searchablePlainTexts": [],
                                            "links": [],
                                            "imageSources": []
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
    
    

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments

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.