Tutorial: How to use SharePoint PnP with Powershell

Thangu Thangu

There are various ways to do perform CRUD (Create, Read, Update, Delete) operations and queries against SharePoint such as ‘using the REST API‘ or the Microsoft Graph API. In this post, I am going to show you how to use PowerShell to perform CRUD operations against SharePoint. 

PnP PowerShell

The major benefit of using PnP PowerShell is that it’s also applicable to SharePoint Administrators who have with very little programming experience. For example, if you want to create a SharePoint site without having to do it all manually in the UI, you can use the following command:

New-PnPWeb -Title "Site1" -Url "Site1" -Description "Dev SubSite" -Template "STS#0"

I often forget where I saved a code sample or document. I know that I did some work in a folder but forgot the location and was only able to remember the name!

Example 2, if you want to get a report of all items in SharePoint with Title “garden” here is one line code.

(Submit-PnPSearchQuery -Query "Title:Garden*").ResultRows|%{$_["Title"],$_["Path"],$_["Author"]}

As you can see, instead of having to write long lines of code or use complex software like Visual Studio or SharePoint Designer, you can use PowerShell which ships free with Windows. You can save both time and money, meaning you don’t have to spend on the tools for analysis, design, installing, coding, testing. As Microsoft support the PnP library you don’t need to worry about maintaining and fixing bugs as you would if you’d coded it all yourself.

After reading this article (and watching the video), you will learn the following:

  1. What is PnP PowerShell
  2. Installation of PnP PowerShell
  3. PnP commands Structure (similar to learning Abc)
  4. Writing PnP Commands (similar to writing words)
  5. Mixing PnP commands (similar to creating sentences)

What is PnP PowerShell?

Many people from Microsoft and within the wider community (like Eric) have created excellent examples explaining how to get your job done faster in one line. You can have look at the code sample here.

If you didn’t know, PNP is short for “Patterns and Practices” and is developed by Microsoft and the community. However, I like to think of PnP as “Put an end to Problems” from lots of coding 🙂

Though I personally love CSOM coding, when I want to do something very fast, I use PnP to create new sites from existing site templates using Provisioning Template commands. The commands to do actions are meaningful and self-explanatory.

Install

This article focuses on SharePoint Online. To install, open PowerShell ISE as Administrator and type

Install-Module SharePointPnPPowerShellOnline

For On-Premises

Install-Module SharePointPnPPowerShell2016

Install-Module SharePointPnPPowerShell2013

Updates

Update-Module SharePointPnPPowerShell*

Once the install is done, get connected to your SharePoint site

#Connect to your SharePoint Site. Replace with your url
$siteurl = "https://xxx.sharepoint.com/sites/dev"

Connect-PnPOnline –Url $siteUrl –Credentials (Get-Credential)

Structure of PnP Command

[verb]-PnP[SpObject]

A PnP command consists of the following:

  • Verb
    • Get
    • Set
    • Remove
    • New
    • Add
  • PnP
  • SPObject
    • Web
    • List
    • ListItem
    • File
    • …Many more

Common Verbs

CRUD PNP PowerShell Verb
Create Add/New
Read Get
Update Set
Delete Remove

SPObject:

Below are some sample SharePoint Objects.

  1. Site
  2. List
  3. File

Suppose, you want to add a Website. The command is “New-PnPWeb”.

CRUD FOR List

You can search for PnP commands using

Get-Command *PnP*

Even if you do not know how to execute a chosen command, you can execute the commands without needing to use Google or Bing. To find documentation on a command you can use the following:

Get-Help Get-PnPFile -examples

You execute one of the examples based on your needs and see the output.

Below are the CRUD commands for a SharePoint List. Try guessing the CRUD command for items like File and check the result from Get-Command *PnPFile*

  Create Read Update Delete
List New-PnPList Get-PnPList Set-PnPList Remove-PnPList
List Item Add-PnPListItem Get-PnPListItem Set-PnPListItem Remove-PnPListItem

Example – How to create a “Garden” SharePoint Site along with a “Fruits” SharePoint List

So let’s apply what you have learnt from above. Just as in a natural language, after learning words, you can join them into different permutations and combinations to create sentences. Similarly, you create meaningful applications by using PnP commands.

Let’s go ahead do the following:

  1. Create a Garden site
  2. Create a Fruits List with column Location
  3. Add a few fruits
  4. Delete One Fruit
#Ensure Latest SharePointPnPPowerShell* is installed
Install-Module -Name "SharePointPnPPowerShellOnline"

#Connect to your SharePoint Site.Replace with your url
$siteurl = "https://xxx.sharepoint.com/sites/dev"

Connect-PnPOnline –Url $siteUrl –Credentials (Get-Credential)

#Create a Garden subsite
New-PnPWeb -Title "GardenPnP" -Url "GardenPnP" -Description "Garden Subsite PnP" -Template "STS#0"

#Update the URL
$gardenUrl=$siteUrl+"/GardenPnP"
Connect-PnPOnline –Url $gardenUrl –Credentials (Get-Credential)
(Get-PnPContext).Url

#Create a List Named Fruits
New-PnPList -Title "Fruits" -Template GenericList

#Create a column named Location of Text or GeoLocation type
Add-PnPField -List "Fruits" -DisplayName "Location" -InternalName "Location" -Type Text -AddToDefaultView

#Add 5 items to Fruits List
$fruits="Apple","Orange","Mango","Kiwi","Pomogranate"
$location="Washington"
foreach ($item in $fruits)
{ 
$newItem = Add-PnPListItem -List Fruits
Set-PnPListItem -List "Fruits" -Identity $newItem -Values @{"Title"="$($item)";"Location"="$($location)";} 
}

#Read the Fruit Details.
Get-PnPListItem -List "Fruits"

#Find Apple
$appleItem=Get-PnPListItem -List Fruits -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value 
Type='Text'>Apple</Value></Eq></Where></Query></View>"

#Update the Place for Apple from Washington to Australia.
Set-PnPListItem -List "Fruits" -Identity $appleItem -Values @{"Title"="Apple";"Location"="Australia"}

#Find Mango
$mangoItem=Get-PnPListItem -List Fruits -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value 
Type='Text'>Mango</Value></Eq></Where></Query></View>"

#Delete Mango
Remove-PnPListItem -List Fruits -Identity $mangoItem

You can view the code here

Advantages of PnP

  1. Works on both Online and On-Premises
  2. One line of code to do common tasks
  3. Simple install
  4. Has a very powerful set of commands to enable complex coding in a single line like Sending mail, adding scripts using Custom Action, enable Responsive Design.

Disadvantages of PnP

  1. From time to time, some commands may not work. Example, Add-PnPListItem. Alternate solution is Set-PnPListItem and Add-PnPListItem

Real World Applications:

Where can we apply PnP in the real world?

  1. Action on Sites like creating sites using PnPProvisioningTemplate
  2. Upload Files and folders to Document Library
  3. Create lists and libraries from code
  4. Custom Action to show Subsite from Gear icon

Hope this article is of use to you and will help you to get started with PnP PowerShell coding!

 

(Visited 9,647 times, 21 visits today)

2 Free Ebooks: How to govern Microsoft teams 

Download your 2 free Microsoft Teams governance Ebooks and learn the steps necessary to create a bullet-proof governance strategy.