sharepointsharepoint-2016

How to create modern web pages in Sharepoint and add web parts to it using sharepoint API?


I need to create modern web page with image and text web parts using sharepoint API. Is it possible to do that?

I tried with below API which creates samplePage.aspx inside Site pages library but when i tried to open it it gives me error.

https://domain-url/sites/SiteName/_api/web/GetFolderByServerRelativeUrl('/sites/SiteName/SitePages')/Files/AddTemplateFile(urlOfFile='samplePage.aspx',templateFileType=3)

enter image description here

Can someone please help me on this.


Solution

  • You could use pnp powershell to create a modern page.

    Download pnp powershell Here: https://learn.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps

    Sample code:

    $cred = Get-Credential
    Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/dev -Credential $cred
    Add-PnPClientSidePage  -Name "smaple Page"
    $page=Get-PnPClientSidePage "smaple Page"
    Add-PnPClientSideText -Page $page -Text "Welcomes To SharePoint"
    Add-PnPClientSideWebPart -Page $page -DefaultWebPartType Image -WebPartProperties @{imageSource="https://contoso.sharepoint.com/sites/dev/Shared%20Documents/Dell.png"}
    

    Test result:

    enter image description here