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.
- $credentials= Get-Credential
- $siteurl="https://<tenant-name>.sharepoint.com"
- 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,
- Template - Type of list to created
- Title -Title of the list
The following cmdlets help to create SharePoint lists on the SharePoint site.
- $listName="HubflyTeam"
- 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------------------
- Remove-PnPList -Identity HubflyTeam
Remove the list named “HublyTeam”, Before deleting ask for a confirmation.
------------------EXAMPLE 2------------------
- Remove-PnPList -Identity HubflyTeam -Force
Removes the list named HubflyTeam without asking for confirmation.
------------------EXAMPLE 3------------------
- 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,
- $listName="HubflyTeam"
- Add-PnPField -List $listName -DisplayName NameHF -InternalName NameHF -Type Text -AddToDefaultView
- $choices="Sofware Developer","Sofware Tester","Release Manager"
- Add-PnPField -List $listName -DisplayName RoleHF -InternalName RoleHF -Type Choice -Choices $choices -AddToDefaultView
- 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------------------
- Remove-PnPField -Identity NameHF
Remove the Field named "NameHF", before removing ask for a confirmation.
------------------EXAMPLE 2------------------
- 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.
| | | | | | | | | |
Text-to-speech function is limited to 200 characters