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

Wednesday 18 October 2017

Create, Retrieve Or Delete Site Content Type On SharePoint Using PnP PowerShell

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

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.

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

Connect To Site:

Connect to the site, using the snippet, given below. The PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).
  1. #Get Current Context Site (Root)  
  2. $siteurl = "https://abc.sharepoint.com"  
  3. Connect-SPOnline -Url $siteurl  
  4. $ctx = Get-SPOContext  
Once connected, you can carry out any of the operations mentioned below, based on the requirement.

Create Site Content Type:

The content types can be added to the site collections by setting the context, using the site URL. PnP CSOM PowerShell can be used to complete the operation.

Add-SPOContentType command is used to create the site content types on SharePoint sites. The required parameters to create the new site content type are name, description and group. The new values can be passed as the parameters. In my example, I have created new site content called "PnPContentType". The default parent content type will be the item.

The following command snippet helps to create the new content type.
  1. function AddContentType(){  
  2.     Add-SPOContentType -Name "PnPContentType" -Description "PnP Content Type" -Group "PnPContentTypeGroup"  
  3. }  
  4. AddContentType # Create New Content Type with name, description  
The parent content type can be used as a base content type to create the new content types. The following example shows to create the  new content type, using the page content type, given below:
  1. function AddContentTypeUsingCT(){  
  2.     $pageCT = Get-SPOContentType -Identity Page  
  3.     Add-SPOContentType -Name "PnPPageContentType" -Description "PnP Content Type" -Group "PnPContentTypeGroup" -ParentContentType $pageCT  
  4. }  
  5. AddContentTypeUsingCT # Create New Content Type with name, description using page content type  
The following snapshot shows the new content types added to the site.


Retrieve Site Content Type:

The content types available on the site can be retrieved by setting the content with the URL, using PnP PowerShell. The following snapshot shows retrieval of all the content types from the site:
  1. function RetrieveContentTypes(){  
  2.     Get-SPOContentType        
  3. }  
  4. RetrieveContentTypes # Retrieves all the content types from site level  
The content type can be retrieved, using the content type Id.
  1. function RetrieveContentTypeByName(){  
  2.     Get-SPOContentType -Identity "Item"  
  3. }  
  4. RetrieveContentTypeByName # Retrieves the content type from site using name  
The following snapshot shows the content type retrieval, using PnP command:


The content type can be retrieved using, the content type name.
  1. function RetrieveContentTypeById(){  
  2.     Get-SPOContentType -Identity 0x01  
  3. }  
  4. RetrieveContentTypeById # Retrieves the content type from site using id  
The site content types from the particular list can be retrieved, using the list name.
  1. function RetrieveSiteCTfromList(){  
  2.     Get-SPOContentType -List "PnPList"  
  3. }  
  4. RetrieveSiteCTfromList # Retrieves the site content type from particular list  

Delete Site Content Type:

The site content type can be deleted from SharePoint site, using PnP PowerShell. Remove-SPOContentType is used to delete the content types. The content type name is passed for deleting the content type. The name or ID can be passed with the command, using the identity parameter.
  1. function RemoveContentType(){  
  2.     Remove-SPOContentType -Identity "PnPContentType" –Force  
  3. }  
  4. RemoveContentType # Delete the content type from site using name  

Monday 4 July 2016

Working With List Columns On SharePoint Using PnP PowerShell

In this post, you will learn how we can create, retrieve and delete the columns on SharePoint lists, using PnP PowerShell. The Client Side Object Model is used internally for these operations. The update operation is not available for the site/list columns (fields).

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 and is named On premise or Office 365 operations. You can also install all 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 tested on SharePoint 2013 and Office 365 environments.

Connect To Site:

Connect to the site, using the snippet, given below. The PnP PowerShell code, given below, 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.

Retrieve List Columns:

The list columns (fields) can be retrieved using PnP commands.
  • Get-SPOField command is used to get all the columns available on the SharePoint list.
  • The required parameters to get list column are list and identity. The list name is passed through list parameter. The field name is passed through identity parameter.
  • The properties like title, internal name, default value, description, etc. can be accessed.
The code snippet, given below, shows getting properties of a list column from the SharePoint list using column name.
  1. $column = Get-SPOField -List "PnPList" -Identity "PnPListColumn"  
  2. Write-Host "Column Title  :" $column.Title  
  3. Write-Host "Description   :" $column.Description  
  4. Write-Host "Group Name    :" $column.Group  
  5. Write-Host "Internal Name :" $column.InternalName  
  6. Write-Host "Static Name   :" $column.StaticName  
  7. Write-Host "Scope         :" $column.Scope  
  8. Write-Host "Type          :" $column.TypeDisplayName  
  9. Write-Host "Schema XML    :" $column.SchemaXml  
  10. Write-Host "Is Required?  :" $column.Required  
  11. Write-Host "Is read only? :" $column.ReadOnlyField  
  12. Write-Host "Unique?       :" $column.EnforceUniqueValues  
  13. Write-Host "-------------------------------------------"  
To get all the fields from a SharePoint list, identity parameter is not required. The code snippet, given below, shows getting all columns from the SharePoint list.
  1. $columns = Get-SPOField -List "PnPList"  
  2. foreach($column in $columns){  
  3.     Write-Host "Column Title  :" $column.Title  
  4.     Write-Host "Description   :" $column.Description  
  5.     Write-Host "Group Name    :" $column.Group  
  6.     Write-Host "Internal Name :" $column.InternalName  
  7.     Write-Host "Static Name   :" $column.StaticName  
  8.     Write-Host "Scope         :" $column.Scope  
  9.     Write-Host "Type          :" $column.TypeDisplayName  
  10.     Write-Host "Schema XML    :" $column.SchemaXml  
  11.     Write-Host "Is Required?  :" $column.Required  
  12.     Write-Host "Is read only? :" $column.ReadOnlyField  
  13.     Write-Host "Unique?       :" $column.EnforceUniqueValues  
  14.     Write-Host "-------------------------------------------"  
  15. }  

Create List Column:

The columns can be created on the lists available on SharePoint site or sub site by setting the context, using the site URL. PnP CSOM PowerShell can be used to complete the operation. 
  • Add-SPOField command, which is used to create the columns on SharePoint lists.
  • The required parameters for creating new column on the list are display name, internal name, group name and field type. 
  • The new values can be passed as the parameters. 
  • AddToDefaultView option is used to make to list column available on default views. The column can be made as mandatory, using the required parameter.
In my example, I have created the new column called "PnPListColumn", which is a text type. The below code snippet shows adding new column. 
  1. Add-SPOField -DisplayName "PnPListColumn" -InternalName "PnPListColumn" -Group "PnPGroup" -Type Text -List "PnPList" -AddToDefaultView -Required  
 As of now, only few properties can be updated while creating the fields.

The image given below shows list default view with list column.


Delete List Column:

The columns can be deleted from a SharePoint list, using PnP PowerShell.
  • Remove-SPOField is used to delete the columns (fields).
  • The field name and list name are required to delete the column. The name or Id can be passed with the command, using the identity parameter. The list name is passed using list parameter.
  • The force attribute is used to delete the field without any confirmation prompts.
The below code snippet shows removing list column.
  1. Remove-SPOField -List "PnPList" -Identity "PnPListColumn" -Force  

Note: The above operations are tested on SharePoint 2013 and Office 365 environments.

Sunday 3 July 2016

Working With Site Columns On SharePoint Using PnP PowerShell

In this post, you will learn, how we can create, retrieve and delete the site columns on SharePoint sites, using PnP PowerShell. Also you will see how to add site column to a site content type. The Client Side Object Model is used internally for these operations. The update operation is not available for the site columns.

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 for on premise or office 365 operations. You can also install all three installers for testing (SharePoint 2013, 2016, online).

The following operations will be compatible with SharePoint 2013 on premise and Office 365 versions.

Connect To Site:

Connect to the site, using the snippet, given below. The PnP PowerShell code, given below, 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 Site Column:

The columns can be created on the site collections by setting the context, using the site URL. PnP CSOM PowerShell can be used to complete the operation.
Add-SPOField command is used to create the site columns on SharePoint sites. The required parameters to create the new site column are display name, internal name, group and type. The column can be made as mandatory using required parameter. The new values can be passed as the parameters. In my example, I have created new site column called "PnPTextColumn" which is text type.
  1. Add-SPOField -DisplayName "PnPTextColumn" -InternalName "PnPTextColumn" -Group "PnPGroup" -Type Text -Required  
The below code snippet shows creating new column with choice type. Similarly other field types can be created.
  1. Add-SPOField -DisplayName "PnPColorColumn" -InternalName "PnPColorColumn" -Group "PnPGroup" -Type Choice -Choices @("red","blue","green")  
 As of now only few properties can be supported for creating site columns.
The below image shows creating new site column using PowerShell.



The below image shows the site column created. The page can be accessed from http://siteurl/_layouts/15/mngfield.aspx.


Retrieve Site Columns:

The site columns can be retrieved using PnP commands. Get-SPOField command is used to get all the site columns available on the SharePoint site collection. The identity parameter is used to retrieve specific site columns. The properties like title, internal name, default value, description, is required can be accessed.

The below code snippet shows getting all the site columns from the site.
  1. Get-SPOField  
 The below code snippet shows getting a site column using column name.
  1. $field = Get-SPOField -Identity "PnPTextColumn"  
  2. Write-Host "Title " $field.Title  
  3. Write-Host "Internal Name" $field.InternalName  
  4. Write-Host "Default value " $field.DefaultValue  
  5. Write-Host "Description " $field.Description  
  6. Write-Host "Is Required" $field.Required  
The below image shows the get operation for birthday field or column.


Add Site Column To Content Type:
The site columns can be added to site content types. The column and content type should exist before adding. Add - SPOFieldToContentType command is used in adding the fields as site columns to content types. The below code snippet shows adding text column to a content type using names.
  1. Add-SPOFieldToContentType -Field "PnPTextColumn" -ContentType "PnPContentType"  
The below code snippet shows adding field to content type using identities.
  1. $colorColumn = Get-SPOField -Identity "PnPColorColumn"  
  2. $pnpCT = Get-SPOContentType -Identity "PnPContentType"  
  3. Add-SPOFieldToContentType -Field $colorColumn -ContentType $pnpCT 


Delete Site Column:

The site column can be deleted from SharePoint site or sub site, using PnP PowerShell. Remove-SPOField is used to delete the columns (fields). The field name is passed for deleting the column. The name or ID can be passed with the command, using the identity parameter. The force attribute is used to delete the field without any confirmation prompts.
  1. Remove-SPOField -Identity "PnPTextColumn" -Force  

Working With SharePoint Web Scoped Features Using PnP PowerShell

In this post, you will learn, how we can retrieve, activate or deactivate Web scoped features on SharePoint site or sub 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 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 for On Premise or Office 365 operations. You can also install all the three installers for testing (SharePoint 2013, 2016, online).

The following operations will be compatible with SharePoint 2013 On Premise and Office 365 versions.

Connect To Site:

Connect to the site, using the snippet, given below. PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).
  1. #Get Current Context Site (Root or subsite)  
  2. $siteurl = "https://abc.sharepoint.com/subsite"  
  3. Connect-SPOnline -Url $siteurl  
  4. $ctx = Get-SPOContext  
Since, we are working with the features with Web Scope, the site URL can be the site collection or sub sites available. Once connected, you can carry out any of the operations mentioned below, based on the requirement.

Retrieve Web Scope Features:

The Web scoped features active on the site or sub sites are retrieved in the operation, given below. Get-SPOFeature command is used to get all the features from the site. Since, we are retrieving the Web scoped features, the scope parameter is not required in this operation.

Each and every feature information is retrieved, using for each loop. The display name and definition Id are retrieved from the features. The code snippet given below helps retrieving all the Web scoped features from the site or sub site, specified in the context.
  1. function RetrieveFeatures(){  
  2.     # Get Web Scoped features  
  3.     $features = Get-SPOFeature -Scope Web  
  4.     Write-Host "Total active features count " $features.Count  
  5.     foreach($feature in $features){  
  6.         Write-Host "Feature Name : " $feature.DisplayName  
  7.         Write-Host "Feature ID   : " $feature.DefinitionId  
  8.     }  
  9.       
  10. }  
  11. RetrieveFeatures #Get all web scoped features from site   
The feature can be retrieved, using the identity. The identity can be the feature name or Id. The scope parameter is optional. Though, Web is passed as a scope. The following code snippet retrieves the feature, using the feature Id.
  1. function RetrieveFeature(){  
  2.     $features = Get-SPOFeature -Scope Web  
  3.     # Check whether FollowingContent feature is active  
  4.     $feature = Get-SPOFeature -Identity a7a2793e-67cd-4dc1-9fd0-43f61581207a -Scope Web  
  5.     if($feature.DisplayName -ne $null){  
  6.         Write-Host $feature.DisplayName " feature is active"          
  7.     }  
  8.     else{  
  9.         Write-Host "Couldn't find info about feature id. Feature is not active"  
  10.     }  
  11. }  
  12. RetrieveFeature #Get web scoped feature from site using feature id   

Enable Web Scope Feature:

The feature can be activated, using PnP PowerShell script. Enable-SPOFeature command is used to activate the features. Along with the command, the necessary parameter is the identity. The identity is the feature Id. The scope is an optional parameter.

The code snippet, given below, helps in activating the feature at the Web scope.
  1. function EnableFeature(){  
  2.     # Active Mobile View web feature  
  3.     Enable-SPOFeature -Identity d95c97f3-e528-4da2-ae9f-32b3535fbb59 -Scope Web  
  4.   
  5.     $feature = Get-SPOFeature -Identity d95c97f3-e528-4da2-ae9f-32b3535fbb59 -Scope Web
  6.     if($feature.DisplayName -ne $null){  
  7.         Write-Host $feature.DisplayName " feature is activated"  
  8.     }  
  9.     else{  
  10.         Write-Host "Feature is not activated"  
  11.     }  
  12. }  
  13. EnableFeature # Activates Mobile View web feature using feature id   

The image, given below, shows the operation.



The image, given below, shows the activated feature. The URL is required to access the Web scope feature.




Disable Web Scope Feature:

The feature can be deactivated, using PnP PowerShell script. Disable-SPOFeature command is used to deactivate the features. Along with the command, the necessary parameter is the identity. The identity is the feature Id. The scope is an optional parameter.

The code snippet, given below, helps in deactivating the feature at the Web scope.
  1. function DisableWebFeature(){  
  2.       
  3.     Disable-SPOFeature -Identity d95c97f3-e528-4da2-ae9f-32b3535fbb59 -Scope Web  
  4.       
  5.     $feature = Get-SPOFeature -Identity d95c97f3-e528-4da2-ae9f-32b3535fbb59 -Scope Web
  6.     if($feature.DisplayName -eq $null){  
  7.         Write-Host $feature.DisplayName " feature is deactivated"  
  8.     }  
  9.     else{  
  10.         Write-Host "Feature is still active"  
  11.     }  
  12. }  
  13. DisableWebFeature # Deactivates Mobile View web feature using feature id  
Note: The operations will be compatible for SharePoint 2013/2016 On Premise / SharePoint online sites.