Showing posts with label PnPPowerShell. Show all posts
Showing posts with label PnPPowerShell. Show all posts

Monday 27 August 2018

Provisioning Layout and Static Content for Office 365 SharePoint Wiki Pages Using PnP PowerShell

Here let us look how to add text to the wiki pages using text layouts with predefined text. In the samples, let us use PnP PowerShell scripts to create a wiki page, apply default layout and for adding content.

Problem Statement: Programmatically creating or updating wiki pages which uses OOB wiki page layouts with some predefined text. This is easily possible with manual actions. While creating a page using client side scripts, adding content to the page is not feasible. So, let us look how to add the text to the created wiki pages programmatically using PnP PowerShell scripts.


Applying Wiki Page Layouts with Content


The following are the points to be considered for adding text to wiki pages.
  • Create a Wiki Page (This is required if the page doesn’t exist already)
  • Retrieve the wiki page to be updated – Using CAML query to get the required page list item.
  • Apply text layout with predefined text to a retrieved wiki page.
Before getting into operations, let us connect to the SharePoint site using PnP PowerShell using the following snippet.

Create a wiki page: Let us programmatically add a page to a SharePoint site using PnP PowerShell script. The following snippet creates a page with the required layout.
Note: Skip the above step if wiki page is already created.

Tuesday 14 August 2018

Creating Page Layouts with Rich HTML Field Editors on Office 365 SharePoint

You would have seen several articles for creating page layouts with custom content types. But in this article let us look how to provision page layouts with multiple rich text fields.

Problem Statement: OOB publishing HTML field (Rich Field Text Column) can be used to create the content type for page layouts manually. But if you require more than one Rich Field Text Column, then we need to create those publishing HTML field programmatically.


Create Page Layout with Rich HTML Fields:


The following are the points to be considered while creating page layouts.
  • Create Site Columns
  • Create Site Content type
  • Add Site Columns to the Content type
  • Create Page Layout using the Content type.
Since few properties needs to enabled on a field, this is only possible with programming way. The field XML is built and then the field is created using the field XML.

This approach might be possible in multiple programming ways including PowerShell, CSOM PowerShell, CSOM, JSOM or REST API. But in the samples below, let us use PnP PowerShell for provisioning required components.

Wednesday 18 October 2017

Working With SharePoint Site Collections On Office365 Tenant Site Using PnP PowerShell

In this article, you will learn how we can create, retrieve, update and delete SharePoint site collections on Office 365 tenant, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Prerequisite:

You need to have PowerShell 3.0, available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation on the official site. The installers are available here. Online version installer is preferred. You can also install all the three installers for testing (SharePoint 2013, 2016, online).

The PnP PowerShell is supported from SharePoint 2013 On Premise and Office 365 versions. The following operations are compatible/tested on Office 365 environments/sites. These operations are not supported by On Premise versions.

Connect To Tenant Site:

To create, retrieve or modify the site collections, we need to connect to the SharePoint tenant (admin) site on Office 365, using the snippet given below. PnP PowerShell code, given below, helps in getting the current context of the admin site, using the Client Side Object Model (CSOM):
  1. $siteurl = "https://abc-admin.sharepoint.com"  # Tenant Admin URL  
  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.

The site collections can be accessed from the site collections page (The URL will look like https://abc-admin.sharepoint.com/_layouts/15/online/SiteCollections.aspx). 

Retrieve Site Collections:

From the admin site, the available site collections can be retrieved, using PnP CSOM PowerShell.
Get-SPOTenantSite command is used to retrieve the site collections from the O365 SharePoint tenant site. The optional parameters used to retrieve the site collections are 
  • Detailed: Used to explicitly pull out information like the title. If not passed, the properties like the title value will be null.
  • IncludeOneDriveSites: Includes one drive sites in the result.
  • Force: Used to get the site collection results without any prompts. 
The following code snippet helps to retrieve all the site collections from the O365 SharePoint tenant site:
  1. $sites = Get-SPOTenantSite -Detailed -IncludeOneDriveSites -Force  
  2. Write-Host "There are " $sites.count " site collections present"  
  3. foreach($site in $sites){  
  4.     Write-Host "Title       : " $site.Title  
  5.     Write-Host "URL         : " $site.Url  
  6.     Write-Host "Template    : " $site.Template  
  7.     Write-Host "Status      : " $site.Status  
  8.     Write-Host "Storage (MB): " $site.StorageMaximumLevel  
  9.     Write-Host "Used (MB)   : " $site.StorageUsage  
  10.     Write-Host "Resources   : " $site.UserCodeMaximumLevel  
  11.     Write-Host "Owner       : " $site.Owner          
  12.     Write-Host "Sharing     : " $site.SharingCapability  
  13.     Write-Host "subsites    : " $site.WebsCount  
  14.     Write-Host "-----------------------------------------"    
  15. }  
The URL parameter is additionally used to get the information about the particular site collection. In the URL parameter, the site collection URL is passed to get the required information.
  1. $site = Get-SPOTenantSite -Url "https://abc.sharepoint.com/" -Detailed  
  2. Write-Host "Title       : " $site.Title  
  3. Write-Host "URL         : " $site.Url  
  4. Write-Host "Template    : " $site.Template  
  5. Write-Host "Status      : " $site.Status  
  6. Write-Host "Storage (MB): " $site.StorageMaximumLevel  
  7. Write-Host "Used (MB)   : " $site.StorageUsage  
  8. Write-Host "RESOURCES   : " $site.UserCodeMaximumLevel  
  9. Write-Host "Owner       : " $site.Owner          
  10. Write-Host "Sharing     : " $site.SharingCapability  
  11. Write-Host "subsites    : " $site.WebsCount  
  12. Write-Host "-----------------------------------------"  

Create Site Collection:

New-SPOTenantSite command is used to create the site collections on O365 SharePoint tenant.
The necessary parameters to create the site collections are:
  • Title
  • URL - New site collection full URL
  • Description
  • Template – site template Id
  • Resource quota – Number of the resources
  • Storage quota – Storage limit
  • Owner – Owner user Id
  • Time zone – Time zone number, which can be retrieved, using Get-SPOTimeZoneId command. 
The following code snippet helps to create a new site collection with the necessary parameters. The new site collection URL is passed.
  1. New-SPOTenantSite -Title "Test" -Url "https://abc.sharepoint.com/sites/Test" -Description "TestDesc" -Template "STS#0" -ResourceQuota 2 -StorageQuota 30 -Owner "abc@abc.onmicrosoft.com" -TimeZone 13  

Update Site Collection:

Set-SPOTenantSite is used to update the site collections present in the tenant site. The required parameter for the update is the site collection URL. The parameters like title, sharing and storage values can be updated.
The following code snippet helps to update the existing site collection with the necessary parameters. The existing site collection URL is passed.
  1. Set-SPOTenantSite -Url "https://abc.sharepoint.com/sites/Test" -Title "TestUpdated" -Sharing ExistingExternalUserSharingOnly  

Delete Site Collection:

Remove-SPOTenantSite command is used to delete a site collection. The required parameter for the delete operation is the existing site collection URL.
The following code snippet helps to remove a site collection from the O365 tenant. The site will be moved to the recycle bin.
  1. Remove-SPOTenantSite -Url "https://abc.sharepoint.com/sites/Test"  
The following code snippet helps to remove the site collection from the O365 tenant permanently.
  1. Remove-SPOTenantSite -Url "https://abc.sharepoint.com/sites/Test" -SkipRecycleBin -Force  

Summary:

Thus, you have learned how to create, retrieve, update or delete the site collections on Office 365 tenant/admin sites, using PnP PowerShell.

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. }