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.

 

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. 

  1. $credentials= Get-Credential  
  2. $siteurl="https://<tenant-name>.sharepoint.com"  
  3. Connect-PnPOnline -Url $siteurl -Credentials $credentials  

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.

  1. $listName="HubflyTeam"  
  2. New-PnPList -Title $listName -Url "hubflyteam" -Template GenericList -EnableVersioning  

Remove SharePoint List

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

Identity - The ID or Title of the list

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

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

  1. Remove-PnPList -Identity HubflyTeam  

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

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

  1. Remove-PnPList -Identity HubflyTeam -Force  

 Removes the list named HubflyTeam without asking for confirmation.

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

  1. Remove-PnPList -Title HubflyTeam -Recycle  

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,

  1. $listName="HubflyTeam"  
  2. Add-PnPField -List $listName -DisplayName NameHF -InternalName NameHF -Type Text -AddToDefaultView  
  3. $choices="Sofware Developer","Sofware Tester","Release Manager"  
  4. Add-PnPField -List $listName -DisplayName RoleHF -InternalName RoleHF -Type Choice -Choices $choices -AddToDefaultView  
  5. Add-PnPField -List $listName -DisplayName AgeHF -InternalName AgeHF -Type Number -AddToDefaultView  

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

  1. Remove-PnPField -Identity NameHF  

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

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

  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.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now