Introduction
In this article, you will learn how we can create, retrieve and delete the list views 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 list views.
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).
# Get Current Context Site (Root)
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$ctx = Get-SPOContext
Once connected, you can carry out any of the operations mentioned below, based on the requirement:
Create List View
The list view can be added to SharePoint lists on the sites by setting the context, using the site URL. PnP CSOM PowerShell can be used to complete the operation.
Add-SPOView command, which is used to create the list views on SharePoint sites. The required parameters for creating the new list views are existing list name, new view name, view type and the fields. The other parameters are set as default for setting the view as default view, row limit, paged for paging value, personal for creating personal views and a query for setting the queries. The new values can be passed as the parameters. In my example, I have created a new list view called "PnPView".
The following command snippet helps in creating a new list view:
Add-SPOView -List "PnPList" -Title "PnPView" -ViewType None -Fields @("ID", "CustomColumn")
The following snapshot shows the list view which is created:
Retrieve List View
The list views available for a list on the site can be retrieved by setting the content with the URL, using PnP PowerShell. Get-SPOView command can be used to retrieve the list views.
The following code shows the retrieval of all the list views for a specified list from the site:
Get-SPOView -List "PnPList"
The list view can be retrieved, using the list view name or list view Id, using the identity parameter. The list view properties, which we can view from the retrieved view are:
Title
DefaultView: Boolean shows whether the view is default view or not
Id: View Id
JsLink: Existing JS links
ListViewXml: View XML
Paged: Is view paged?
PersonalView: Personal view
RowLimit: View row limit
ServerRelativeUrl: View URL
ViewFields: Fields of the view
The following code shows retrieving the list view for a list specified from the site using view name.
$view = Get-SPOView -List "PnPList" -Identity "PnPView"
$view.Title
$view.DefaultView
$view.BaseViewId
$view. Id
$view.Hidden
$view.HtmlSchemaXml
$view.JSLink
$view.ListViewXml
$view.MobileDefaultView
$view.OrderedView
$view.Paged
$view.PersonalView
$view.RowLimit
$view.ServerRelativeUrl
$view.ViewFields
$view.ViewQuery
Delete List View
The list view can be deleted for a list from a SharePoint site, using PnP PowerShell. Remove-SPOView, which is used to delete the list views from SharePoint list. The view name is passed to delete the list view. The name or Id can be passed with the command, using an identity parameter.
The following code shows deleting the list view, using the view name with the force attribute. The Force attribute is used to delete the view without any prompts.
Remove-SPOView -List "PnPList" -Identity "PnPView" -Force
Summary
Thus, you have learned how to create, retrieve or delete the list views on SharePoint sites, using PnP PowerShell on SharePoint 2013 / SharePoint online sites.
Comments