Written by

Ravishankar

Share

  

Category

see all categories
intranet as a service

Create And Remove List And Fields In SharePoint Site Using PnP Powershell

PnP provisioning is a community-driven platform that enables fast development of components that define your application infrastructure & the content to some extent. When we say infrastructure, it’s the underlying libraries, lists, pages, columns, content types that form the basis of any SharePoint site. In this article, we will see how to create & remove SharePoint list and fields using PnP PowerShell.

Prerequisite

Before you begin utilizing PowerShell to oversee SharePoint Online, ensure that the SharePoint Online Management Shell is installed. You can install the SharePoint Online Management Shell by downloading and running the SharePoint Online Management Shell. You only need to do this once for each computer from which you are running SharePoint Online PowerShell commands.

Connect to Site

Before connecting to the SharePoint site, we need to get Credentials by using the Get-Credential cmdlet which creates a credential object for a specified username and password. You can use the credential object in security operations. By default, an authentication dialog box appears to prompt the user like the below example picture.

Connect to your site

Then connect to SharePoint site using Connect-PnPOnline cmdlet. The required parameters are -Url and-Credential. In -Url Parameter passes the site URL and in -Credential it passes the Get-Credential.

The following code snippet helps to connect SharePoint sites. 

code1

Create SharePoint List

The list can be created by using New-PnPList cmdlet on SharePoint Site. The required parameters are,

  1. Template - Type of list to created
  2. Title -Title of the list

The following cmdlets help to create SharePoint lists on the SharePoint site.

Code2

Remove SharePoint List

The list can be removed by using Remove-PnPList cmdlet on SharePoint Sites. The required parameters are,

Leaderboard_CDR

Identity - The ID or Title of the list

The following cmdlets help to remove SharePoint list on the SharePoint site.

--------------EXAMPLE 1------------------

Code3

 Remove the list named “HubflyTeam”, Before deleting ask for a confirmation.

------------------EXAMPLE 2------------------

Code 2-1

 Removes the list named HubflyTeam without asking for confirmation.

------------------EXAMPLE 3------------------

Code3-1

Removes the list named 'HubflyTeam' and saves to the Recycle Bin.

Create SharePoint Fields

The fields can be created by using Add-PnPField cmdlet on the SharePoint site. The required parameters are,

  • DisplayName - The display name of the field
  • Field - The name of the field, its ID or an actual field object that needs to be added
  • InternalName - The internal name of the field
  • Type - The type of the field like Choice, Note, MultiChoice

The following cmdlets helps to create SharePoint fields on the SharePoint site,

Code-2

Remove SharePoint Fields

The fields can be removed by using Remove-PnPField cmdlet on the SharePoint site. The required parameters are,

Identity – The field object or name to remove

The following cmdlets help to remove SharePoint list on the SharePoint site

--------------EXAMPLE 1------------------

Code01
  1. Remove-PnPField -Identity NameHF  

Remove the Field named "NameHF", before removing ask for a confirmation.

------------------EXAMPLE 2------------------

Code02
  1. Remove- PnPField -Identity NameHF -Force  

Remove the Field named "NameHF" without asking for confirmation.

Thus, you have learned how to create and remove SharePoint Lists and Fields programmatically using PnP PowerShell commands. PnP PowerShell is supported by SharePoint Online. The operations mentioned above, are tested on SharePoint Online environments.