Lists manipulation using REST API in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#---------------------------------------------------------------------------------------------- | |
# List CRUD operations via SharePoint REST API | |
#---------------------------------------------------------------------------------------------- | |
# Create a List | |
Function New-SPOList(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$False)] | |
[String]$Password, | |
[Parameter(Mandatory=$True)] | |
[String]$Title, | |
[Parameter(Mandatory=$True)] | |
[String]$BaseTemplate | |
) | |
$listMetadata = @{ | |
__metadata = @{'type' = 'SP.List' }; | |
Title = $Title; | |
BaseTemplate = $BaseTemplate} | ConvertTo-Json | |
$Url = $WebUrl + "/_api/lists" | |
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password | |
Invoke-RestSPO $Url Post $UserName $Password $listMetadata $contextInfo.GetContextWebInformation.FormDigestValue | |
} | |
# Update a List | |
Function Set-SPOList(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$False)] | |
[String]$Password, | |
[Parameter(Mandatory=$True)] | |
[String]$Identity, | |
[Parameter(Mandatory=$False)] | |
[String]$Title, | |
[Parameter(Mandatory=$False)] | |
[String]$Description | |
) | |
$listMetadata = @{ | |
__metadata = @{'type' = 'SP.List' }; | |
} | |
if($Title) { | |
$listMetadata['Title'] = $Title | |
} | |
if($Description) { | |
$listMetadata['Description'] = $Description | |
} | |
$listMetadata = $listMetadata | ConvertTo-Json | |
$Url = $WebUrl + "/_api/lists/getbytitle('" + $Identity + "')" | |
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password | |
Invoke-RestSPO -Url $Url -Method Post -UserName $UserName -Password $Password -RequestDigest $contextInfo.GetContextWebInformation.FormDigestValue -Metadata $listMetadata -ETag "*" -XHTTPMethod "MERGE" | |
} | |
#Get List(s) | |
Function Get-SPOList(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$False)] | |
[String]$Password | |
) | |
$Url = $WebUrl + "/_api/lists" | |
$data = Invoke-RestSPO $Url Get $UserName $Password | |
$data.results | |
} | |
#Delete a List | |
Function Remove-SPOList(){ | |
Param( | |
[Parameter(Mandatory=$True)] | |
[String]$WebUrl, | |
[Parameter(Mandatory=$True)] | |
[String]$UserName, | |
[Parameter(Mandatory=$False)] | |
[String]$Password, | |
[Parameter(Mandatory=$True)] | |
[String]$Identity | |
) | |
$Url = $WebUrl + "/_api/lists/getbytitle('" + $Identity + "')" | |
$contextInfo = Get-SPOContextInfo $WebUrl $UserName $Password | |
Invoke-RestSPO -Url $Url -Method Post -UserName $UserName -Password $Password -RequestDigest $contextInfo.GetContextWebInformation.FormDigestValue -ETag "*" -XHTTPMethod "DELETE" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment