Showing posts with label PnP. Show all posts
Showing posts with label PnP. Show all posts

Wednesday 29 June 2016

Create, Retrieve, Update Or Delete Sites On SharePoint Using PnP PowerShell

In this post, you will learn, how we can create, retrieve, update and delete SharePoint sites, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Prerequisite:

You need to have PowerShell 3.0, available on Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or cmdlets from the official site.

The following operations will be compatible for SharePoint 2013 / 2016 on-premise or Office 365 versions.

Connect To Site:

Connect to the site, using the snippet, given below. The below PnP PowerShell code helps in getting the current context of the site, using the client side object model (CSOM).
  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext   
Once connected, you can carry out any of the operations, mentioned below, based on the requirement.


Create Sub Site:

From the existing site collection, the sub sites can be created. The same can be accomplished, using PnP CSOM PowerShell.

New-SPOWeb command is used to create sub sites on SharePoint sites. The necessary parameters to create the site are Title, URL and template. Other parameters, that can be passed to create site are description, break inheritance etc.

There are two ways to create a site. When the command New-SPOWeb is entered on the PowerShell, you will be prompted for the parameters. The site will be created.

Other way of creating the site is to pass the parameters along with the command. The following command helps in creating a sub site.
  1. New-SPOWeb -Title "PnPSite1" -Description "PnPDescription1" -Url "PnPSite1" -Template "STS#0"    


Retrieve Site or Sub Site:

From the site collection, the sub site properties can be retrieved. The same can be accomplished, using PnP CSOM PowerShell.

Get-SPOWeb is used to retrieve the sub site properties. Along with the command, the sub site name or the URL is passed to retrieve the information. When the command is just passed without any parameters, the current site properties will be retrieved.

The basic properties which can be retrieved using the command are title, GUID, URL.

The following PowerShell code snippet helps to retrieve the site properties:
  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  
  4. #Get Current Context Site (Root)  
  5. function RetrieveSite(){  
  6.     $web = Get-SPOWeb  
  7.     Write-Host "Title : " $web.Title  
  8.     Write-Host "Description : " $web.Description  
  9.     Write-Host "URL : " $web.Url  
  10. }  
  11. #Get Sub Site  
  12. function RetrieveSubSite(){  
  13.     $web = Get-SPOWeb "PnPSite1"  
  14.     Write-Host "Title : " $web.Title  
  15.     Write-Host "Description : " $web.Description  
  16.     Write-Host "URL : " $web.Url  
  17. }  
  18. RetrieveSite #Get Current Context Site (Root)  
  19. RetrieveSubSite #Get Sub Site   


Update Site:

From the site collection, the sub site properties can be updated. The same can be accomplished, using PnP CSOM PowerShell.

To change the site properties, retrieve the corresponding site, using title or URL. The parameters are not required, if you are trying to change the current context site (root) properties.
Update the site with the necessary parameters, using Set-SPOWeb.

The parameters that can be changed are title, site logo URL.

The following PowerShell snippet helps to update the site or sub site properties.
  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  
  4. #Update Current Site  
  5. function UpdateSite(){  
  6.     $web = Get-SPOWeb  
  7.     Set-SPOWeb -Web $web -Title "Title Update"  
  8. }  
  9. #Update Sub Site  
  10. function UpdateSubSite(){  
  11.     $web = Get-SPOWeb "/PnPSite1"  
  12.     Set-SPOWeb -Web $web -Title "PnPSite1 Update"  
  13. }  
  14.  
  15. # Call above Functions  
  16. UpdateSite #Update Current Site  
  17. UpdateSubSite #Update Sub Site  

Delete Site:


From the site collection, the sub sites can be deleted. The same can be accomplished, using PnP CSOM PowerShell.

The parameter that needs to passed is an identity. By using Remove-SPOWeb command, the site or sub site can be deleted.

The following PowerShell snippet helps to delete the site from the site collection.
  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext  
  4. # Delete site  
  5. function DeleteSubSite(){  
  6.     Remove-SPOWeb -Identity "PnPSite1" -Force      
  7. }