Functions/Sites/New-SitePage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Function New-SitePage {
    [CmdletBinding()]
    Param(      
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$SiteId,

        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [ValidateNotNullOrEmpty()]
        [string]$Title,

        [ValidateNotNullOrEmpty()]
        [string]$PageLayout,

        [ValidateNotNullOrEmpty()]
        [object[]]$webParts = @()
    )

    if ($global:PowerGraph_BaseUrl.ToLower().Contains("beta") -eq $false)
    {
        Write-Warning 'This query/command is currently only supported on the Beta endpoint - please call 'Connect-MSGraph' again with the following setting: -BaseUrl "https://graph.microsoft.com/beta/"'
    } else {

        $body = (@{
            name            = $Name
            title           = $Title
            PageLayout      = $PageLayout
            webParts        = $WebParts
        }) | ConvertTo-Json
    
        $uri = $global:PowerGraph_BaseUrl + "sites/$SiteId/pages"

        Write-Verbose "Adding new page: $Title with $body"
    
        $sitePage = Invoke-MSGraphRequest -Uri $uri -Method Post -Body $body
    
        if ($sitePage -ne $null)
        {
            Write-Verbose "New page created with Id $($sitePage.id)"
        }
        
        return $sitePage
    }
}