functions/SPClientWebTemplate/Get-SPClientWebTemplate.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#Requires -Version 3.0

<#
  Get-SPClientWebTemplate.ps1
 
  Copyright (c) 2017 karamem0
 
  This software is released under the MIT License.
  https://github.com/karamem0/SPClient/blob/master/LICENSE
#>


function Get-SPClientWebTemplate {

<#
.SYNOPSIS
  Gets one or more site templates.
.DESCRIPTION
  The Get-SPClientWebTemplate function lists all site templates or retrieves the specified site template.
  If not specified filterable parameter, returns all site templates of the site collection or site.
  Otherwise, returns a site template which matches the parameter.
.PARAMETER ClientContext
  Indicates the client context. If not specified, uses default context.
.PARAMETER ParentObject
  Indicates the site collection or site to which the site templates are contained.
.PARAMETER Locale
  Indicates the locale ID in which the site templates is used. If not specified, uses the current thread locale.
.PARAMETER NoEnumerate
  If specified, suppresses enumeration in output.
.PARAMETER Name
  Indicates the site template name.
.EXAMPLE
  Get-SPClientWebTemplate
.EXAMPLE
  Get-SPClientWebTemplate $web -Locale 1033
.EXAMPLE
  Get-SPClientWebTemplate -Name "STS#0"
.INPUTS
  None or SPClient.SPClientWebTemplateParentPipeBind
.OUTPUTS
  Microsoft.SharePoint.Client.WebTemplate[]
.LINK
  https://github.com/karamem0/SPClient/blob/master/doc/Get-SPClientWebTemplate.md
#>


    [CmdletBinding(DefaultParameterSetName = 'All')]
    param (
        [Parameter(Mandatory = $false)]
        [Microsoft.SharePoint.Client.ClientContext]
        $ClientContext = $SPClient.ClientContext,
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true)]
        [SPClient.SPClientWebTemplateParentPipeBind]
        $ParentObject = $ClientContext.Site,
        [Parameter(Mandatory = $false)]
        [string]
        $Locale = [System.Threading.Thread]::CurrentThread.CurrentCulture.LCID,
        [Parameter(Mandatory = $false, ParameterSetName = 'All')]
        [switch]
        $NoEnumerate,
        [Parameter(Mandatory = $false, ParameterSetName = 'Name')]
        [string]
        $Name
    )

    process {
        if ($ClientContext -eq $null) {
            throw "Cannot bind argument to parameter 'ClientContext' because it is null."
        }
        if ($ParentObject -eq $null) {
            throw "Cannot bind argument to parameter 'ParentObject' because it is null."
        }
        if ($ParentObject.ClientObject -is [Microsoft.SharePoint.Client.Site]) {
            $PathMethod = New-Object Microsoft.SharePoint.Client.ObjectPathMethod( `
                $ClientContext, `
                $ParentObject.ClientObject.Path, `
                'GetWebTemplates', `
                [object[]]@($Locale, 0))
            $ClientObjectCollection = New-Object Microsoft.SharePoint.Client.WebTemplateCollection($ClientContext, $PathMethod)
            Invoke-ClientContextLoad `
                -ClientContext $ClientContext `
                -ClientObject $ClientObjectCollection
        } else {
            $PathMethod = New-Object Microsoft.SharePoint.Client.ObjectPathMethod( `
                $ClientContext, `
                $ParentObject.ClientObject.Path, `
                'GetAvailableWebTemplates', `
                [object[]]@($Locale, $true))
            $ClientObjectCollection = New-Object Microsoft.SharePoint.Client.WebTemplateCollection($ClientContext, $PathMethod)
            Invoke-ClientContextLoad `
                -ClientContext $ClientContext `
                -ClientObject $ClientObjectCollection
        }
        if ($PSCmdlet.ParameterSetName -eq 'All') {
            Write-Output $ClientObjectCollection -NoEnumerate:$NoEnumerate
        }
        if ($PSCmdlet.ParameterSetName -eq 'Name') {
            $ClientObject = $ClientObjectCollection | Where-Object { $_.Name -eq $Name }
            if ($ClientObject -eq $null) {
                throw 'The specified site template could not be found.'
            }
            Write-output $ClientObject
        }
    }

}