SharePoint Online List Operations In Office 365 Using PnP PowerShell

Today, we’ll look at SharePoint Online List Operations in office 365 using PnP PowerShell.
 

Introduction

 
This article provides guidance on how to use Microsoft Windows PowerShell commands to create a Microsoft SharePoint Online Office 365 basic operations and it will be useful for SharePoint developers and admins.
 
This article will include the basic retrieve operations, using SharePoint online offie 365 sites.
 

PowerShell /SharePoint management Shell

 
PowerShell is a tool available in Microsoft to run/execute the cmdlets/.exe files to perform some operations.
 
MSDN says that, "Windows PowerShell™ command-line interface is a new command-line tool and supporting scripting language from Microsoft that complements Cmd.exe in the Windows administration context. In the SharePoint administration context, Windows PowerShell supersedes the Stsadm.exe administration tool. Moving forward, you should use Windows PowerShell scripting technology to develop any new command-line scripts in SharePoint Foundation 2010."
 
Ref: https://msdn.microsoft.com/en-us/library/office/ee539977(v=office.14).aspx
 
Before going to the article, we first need to know what PowerShell, is how the commands are used, and basic syntax.
 
Learn about basic Windows PowerShell cmdlets, concepts and how to use Windows PowerShell with SharePoint 2013.
 
Ref: https://technet.microsoft.com/en-us/library/ee806878.aspx#sectionExPol
 
PowerShell/SharePoint Management Shell
 
SharePoint Management Shell is available in your machine after you install SharePoint2013.
 

Open SharePoint Management Shell

  • Start - SharePoint Management Shell.
  • SharePoint Management Shell is not on the Start screen:
  • Computer - All appsàSharePoint Management Shell.
For more info about the Practices & Patterns (PnP), refer to the official documentation, and/or download the modules from the hard workers in GitHub – SharePoint PowerShell PnP.
 
In this article we are going to see the below operations using PowerShell PNP cmdlets,
  1. Return lists from SharePoint
  2. Return list items from SharePoint Online
  3. Retrieve list items from SharePoint Online
  4. Delete an item from a SharePoint Online list
  5. Delete the File from SharePoint Online.
First, we need to connect the SharePoint Online site using the below command,
 
Step 1
 
Open SharePoint Management Shell as an Administrator in your machine.
 
Step 2
 
Run the following command to import PnP to install the PowerShell cmdlets,
 
Install-Module SharePointPnPPowerShellOnline  
 
Step 3
 
To use the library you first need to connect to your tenant using the below cmdlets.
 
connect-PnPOnline –Url https://fortunestrikes.sharepoint.com –Credentials (Get-Credential)  
 

Return a List/ document library

 
Now that we’re logged into the site collection, let’s create our List/document library.
 
Syntax
 
This syntax is referred from msdn
  1. Get-PnPList  
  2. [-Includes <String[]>]  
  3. [-Identity <ListPipeBind>]  
  4. [-ThrowExceptionIfListNotFound [<SwitchParameter>]]  
  5. [-Web <WebPipeBind>]  
  6. [-Connection <PnPConnection>]  
Return Lists
 
This cmdlet will return the lists from the current web,
  1. Get-PnPList  
  2. Write-Host " successfully Completed" -ForegroundColor Green  
Return Lists with GUID
  1. Get-PnPList -Identity 77a00f6e-ce81-5dc8-9eac-e09c6f9132hg  
  2. Write-Host " successfully Completed" -ForegroundColor Green  
Returns all lists in the current web
  1. Get-PnPList | Where-Object {$_.RootFolder.ServerRelativeUrl -like "/lists/*"}  
  2. Write-Host " successfully Completed" -ForegroundColor Green  
Synatx
  1. Get-PnPListItem  
  2. -List <ListPipeBind>  
  3. [-Id <Int>]  
  4. [-Fields <String[]>]  
  5. [-Web <WebPipeBind>]  
  6. [-Connection <PnPConnection>]  
Retrieves all list items from SharePoint online,
  1. Get-PnPListItem -List Demo List  
  2. Write-Host " successfully Completed" -ForegroundColor Green   
Retrieve the list items using query based,
  1. Get-PnPListItem -List Tasks -Query "<View><Query><Where><Eq><FieldRef Name='GUID'/><Value Type='Guid'>ce6c5b3b-f960-4dd7-e02c-85dc6cd78cc4</Value></Eq></Where></Query></View>"  
  2. Write-Host " successfully Completed" -ForegroundColor Green  
Remove the list items from SharePoint online,
 
syntax
  1. Remove-PnPListItem  
  2. -List <ListPipeBind>  
  3. -Identity <ListItemPipeBind>  
  4. [-Recycle [<SwitchParameter>]]  
  5. [-Force [<SwitchParameter>]]  
  6. [-Web <WebPipeBind>]  
  7. [-Connection <PnPConnection>]  
  1. Remove-PnPListItem -List "Demo List" -Identity "1" -Force  
  2. Write-Host " successfully Completed" -ForegroundColor Green  
Remove the file from SharePoint online,
 
Syntax
  1. Remove-PnPFile  
  2. -ServerRelativeUrl <String>  
  3. [-Recycle [<SwitchParameter>]]  
  4. [-Force [<SwitchParameter>]]  
  5. [-Web <WebPipeBind>]  
  6. [-Connection <PnPConnection>]  
Code
  1. #Global Variables  
  2. $SiteURL = "https://fortunestrikes.sharepoint.com/sites"  
  3. $FileRelativeURL = "/sites/Teams/Shared Documents/Document.docx"  
  4. #Get Credentials to connect  
  5. $Cred = Get - Credential  
  6. Try {  
  7.     #Connect to PNP Online  
  8.     Connect - PnPOnline - Url $SiteURL - Credentials $Cred  
  9.     #Try to Get File  
  10.     $File = Get - PnPFile - Url $FileRelativeURL - ErrorAction SilentlyContinue  
  11.     If($File) {  
  12.         #Delete the File  
  13.         Remove - PnPFile - ServerRelativeUrl $FileRelativeURL - Force  
  14.         Write - Host - f Green "File $FileRelativeURL deleted successfully!"  
  15.     }  
  16.     Else {  
  17.         Write - Host - f Yellow "Could not Find File at $FileRelativeURL"  
  18.     }  
  19. catch {  
  20.     write - host "Error: $($_.Exception.Message)" - foregroundcolor Red  
  21. }  

Conclusion

 
This article helped you to understand about SharePoint basic operations with PowerShell commands. In this article, SharePoint online operations were explained. In the next article, we will see about same basic operations in SharePoint lists and libraries, using PowerShell method.