SharePoint Online: List and List Items operation with PnP PowerShell

First, you will have to connect to your SharePoint Online Site

Example: Connect to SPO

$siteurl = "https://yourspsite.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

Retrieving List


#Get all lists
$lists = Get-SPOList  
foreach($list in $lists){              
       #Properties
       $list.Title  
       $list.Description  
       $list.ItemCount  
       #etc...
}  

#Get Single list
Get-SPOList "Test"

Create List
This will create the list if the list does not exist

Function CreateList {
	Param (
		[Parameter(Mandatory=$True)]
		[string]$title,
		[Parameter(Mandatory=$True)]
		[string]$removeContentType,
		[Parameter(Mandatory=$True)]
		[string]$contentType,
		[Parameter(Mandatory=$True)]
		[string]$template,
		[Parameter(Mandatory=$False)]
		[string]$url
	)

	$list = Get-SPOList $title -ErrorAction SilentlyContinue
	if($list -eq $null) {
		Write-Host "Creating List title:" $title "template:" $template "url:" $url -ForegroundColor Yellow
		if($url -eq $null) {
			New-SPOList -Title $title -Template $template -Url $title
		}
		else {
			New-SPOList -Title $title -Template $template -Url $url
		}

		if($removeContentType -ne $null){
			Remove-SPOContentTypeFromList -List $title -ContentType $removeContentType
		}
		
		if($contentType -ne $null) {
			Add-SPOContentTypeToList -List $title -ContentType $contentType -DefaultContentType
		}
				
		Write-Host "Created List" $title -ForegroundColor Green
	}
	else {
		Write-Host "List" $title "already exist" -ForegroundColor Yellow
	}
}

Execute the function
CreateList -title "Custom Link List" `
           -removeContentType Link `
           -contentType "SpecialLink" `
           -template Links

Update List

#Update List
Set-SPOList -Identity "Test" -Title "Test List 2" 

#Unique Permission
Set-SPOList -Identity "Test" -BreakRoleInheritance

Delete List

Remove-SPOList -Identity "Test" -Force

How to add an item

#Add an empty item
Add-SPOListItem -List "Test"

#Add an item with value(s)
Add-SPOListItem -List "PnPList" -Values @{"Title"="1";"TestColumn"="1"}

Example: Retrieving view

Get-SPOView -List "Test"

Example: Add view to list

Add-SPOView -List "Target List" -SetAsDefault `
	-Title "View Title" `
	-ViewType None `
	-Fields @("Field you wanted to display: e.g: ID", "URLwMenu", "etc..") `
	-Query "CAML Query: <Where>...</Where>"

Example: Remove view from list

Remove-SPOView -List "Target List" -Identity "View Title" -Force
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s