NewSPOList.ps1
<#PSScriptInfo .VERSION 1.0 .GUID 2ecce848-1f33-4863-bfe7-7c9a67e70aff .AUTHOR Chendrayan Venkatesan .COMPANYNAME .COPYRIGHT .TAGS SharePointOnline CSOM PowerShell .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION Create New List in SharePoint Online Using ExceptionHandlingScope Class #> <# .SYNOPSIS New-xSPOList is to create a List or Document Library in the given SharePoint Site or Web .DESCRIPTION This cmdlet uses ExceptionHandlingScope class which avoid throtting. While creating the SharePoint List or Document Library the code checks if it exists in try block. If not exists code creates one in catch block and finally updates the list in finally block. All three request to the server is sent only once which eventually increase performance. .EXAMPLE C:\PS> New-SPOList -Url "http://contoso.sharepoint.com" -Title "YourListName" -TemplateType "Announcements" If the list or document library not existing new list named YourListName with Announcements type will be created. .EXAMPLE C:\PS> New-SPOList -Url "http://contoso.sharepoint.com" -Title "YourListName" -TemplateType "Announcements" -Description "Set the Description" Creates a List or Document Library with Description .EXAMPLE C:\PS> New-SPOList -Url "http://contoso.sharepoint.com" -Title "YourListName" -TemplateType "Announcements" -Hidden $true Creates a hidden list or document library .EXAMPLE C:\PS> New-SPOList -Url "http://contoso.sharepoint.com" -Title "YourListName" -TemplateType "Announcements" -Hidden $true -Description "Set the Description" Does all at once if list is not created. .EXAMPLE C:\PS> $Cred = Get-Credential C:\PS> "https://contoso.sharepoint.com" , "https://contoso.sharepoint.com/site" | New-xSPOList -Title "YourList" -TemplateType "Choose Yours" -Credential $cred Creates same list in two site collection and on a web. .LINK https://msdn.microsoft.com/en-us/library/office/ee534976(v=office.14).aspx .NOTES This function is part of a custom module which is built partially. The reason to share this script is to avoid throtting in SharePoint Online. Read the ExceptionHandlingScope by executing "help New-SPOList -Online" #> param ( [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] # SharePoint Url (Site or Web). $Url, [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] # Name of the List needs to created. $Title, [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Microsoft.SharePoint.Client.ListTemplateType] # Enum type allows tab completion to choose the required type. $TemplateType, [Parameter(ValueFromPipeline)] # Sets the Description of the List or Document Library $Description, [Parameter(ValueFromPipeline)] # Sets hidden property of the List or Document Library [bool] $Hidden, [Parameter(Mandatory)] # Propmpts for credential [System.Management.Automation.CredentialAttribute()] [pscredential] $Credential ) # Change the location to load required assemblies Import-Module 'C:\Program Files\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.5312.1200\lib\net45\Microsoft.SharePoint.Client.dll' Import-Module 'C:\Program Files\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.5312.1200\lib\net45\Microsoft.SharePoint.Client.Runtime.dll' function New-SPOList { <# .SYNOPSIS New-xSPOList is to create a List or Document Library in the given SharePoint Site or Web .DESCRIPTION This cmdlet uses ExceptionHandlingScope class which avoid throtting. While creating the SharePoint List or Document Library the code checks if it exists in try block. If not exists code creates one in catch block and finally updates the list in finally block. All three request to the server is sent only once which eventually increase performance. .EXAMPLE C:\PS> New-SPOList -Url "http://contoso.sharepoint.com" -Title "YourListName" -TemplateType "Announcements" If the list or document library not existing new list named YourListName with Announcements type will be created. .EXAMPLE C:\PS> New-SPOList -Url "http://contoso.sharepoint.com" -Title "YourListName" -TemplateType "Announcements" -Description "Set the Description" Creates a List or Document Library with Description .EXAMPLE C:\PS> New-SPOList -Url "http://contoso.sharepoint.com" -Title "YourListName" -TemplateType "Announcements" -Hidden $true Creates a hidden list or document library .EXAMPLE C:\PS> New-SPOList -Url "http://contoso.sharepoint.com" -Title "YourListName" -TemplateType "Announcements" -Hidden $true -Description "Set the Description" Does all at once if list is not created. .EXAMPLE C:\PS> $Cred = Get-Credential C:\PS> "https://contoso.sharepoint.com" , "https://contoso.sharepoint.com/site" | New-xSPOList -Title "YourList" -TemplateType "Choose Yours" -Credential $cred Creates same list in two site collection and on a web. .LINK https://msdn.microsoft.com/en-us/library/office/ee534976(v=office.14).aspx .NOTES This function is part of a custom module which is built partially. The reason to share this script is to avoid throtting in SharePoint Online. Read the ExceptionHandlingScope by executing "help New-SPOList -Online" #> [CmdletBinding()] param( [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] # SharePoint Url (Site or Web). $Url, [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] # Name of the List needs to created. $Title, [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)] [Microsoft.SharePoint.Client.ListTemplateType] # Enum type allows tab completion to choose the required type. $TemplateType, [Parameter(ValueFromPipeline)] # Sets the Description of the List or Document Library $Description, [Parameter(ValueFromPipeline)] # Sets hidden property of the List or Document Library [bool] $Hidden, [Parameter(Mandatory)] # Propmpts for credential [System.Management.Automation.CredentialAttribute()] [pscredential] $Credential ) begin { } process { $SPOClientContext = [Microsoft.SharePoint.Client.ClientContext]::new($Url); $SPOClientContext.Credentials = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($Credential.UserName,$Credential.Password) $Scope = [Microsoft.SharePoint.Client.ExceptionHandlingScope]::new($SPOClientContext) $Start = $Scope.StartScope() #Try Block $xTry = $Scope.StartTry() $List = $SPOClientContext.Web.Lists.GetByTitle($Title); $List.Update() $xTry.Dispose() #Catch Block $xCatch = $Scope.StartCatch() $NewList = [Microsoft.SharePoint.Client.ListCreationInformation]::new() $NewList.Title = $Title $NewList.TemplateType = [int]$TemplateType $NewList.Description = $Description $List = $SPOClientContext.Web.Lists.Add($NewList) $List.Hidden = $Hidden $xCatch.Dispose() #Finaly Block $xFinally = $Scope.StartFinally() $List = $SPOClientContext.Web.Lists.GetByTitle($Title); $List.Update() $xFinally.Dispose() $Start.Dispose() $SPOClientContext.ExecuteQuery() $SPOClientContext.Dispose() } end { } } |