1

I am creating a field in SharePoint list using Add-PnPFieldFromXml command

The field is created successfully. I want to add this field to the default view of the list using the same command.

I checked Add-PnPFieldFromXml link but did not find the option to add the column in the default view.

How can I achieve the above requirement?

Or do I have to use Set-PnPView command?

Update

below is the code I am using.

  $listTitle = "Test"
  $listUrl = "Test"
  Remove-PnPList -Identity $listTitle
  Write-Host -ForegroundColor Magenta "Creating list: " $listURL
  New-PnPList -Title $listTitle -Url $listUrl -Template "GenericList"
  $FieldXML= "<Field Type='Note' Name='CustomField' ID='$([GUID]::NewGuid())' DisplayName='CustomField' Viewable ='TRUE' Required ='FALSE' RichText='FALSE'></Field>"
  Add-PnPFieldFromXml -List $listTitle -FieldXml $FieldXML 

2 Answers 2

1

Solution 1:

Try adding Viewable = "TRUE" in Field XML.

Example:

$xml = '<Field Type="Text"
          Name="PSCmdletTest"
          DisplayName="PSCmdletTest"
          ID="{27d81055-f208-41c9-a976-61c5473eed4a}"
          Group="Test"
          Required="FALSE"
          StaticName="PSCmdletTest"
          Viewable="TRUE"
        />'

Add-PnPFieldFromXml -List "Demo List" -FieldXml $xml

Documentation: Field element (Field).

Viewable: Optional Boolean. TRUE to specify that the field is added to the default view. The default value is TRUE.

Solution 2:

You can also set the fields to show in SharePoint list view using Set-PnPView.

Example:

Set-PnPView -List "Documents" -Identity "Corporate Documents" -Fields "Title","Created"

Documentation: Set-PnPView.

2
  • It is not working. I have added the code in the question Feb 10, 2020 at 7:37
  • Strange. According to microsoft documentation it should work. If it is not working then try using Set-PnPView (see updated answer). Feb 10, 2020 at 7:46
1

Modify your PnP PowerShell as below.

$URL='https://tenant.sharepoint.com/sites/test'
Connect-PnPOnline –Url $URL –Credentials (Get-Credential)
$listTitle = "Test"
$listUrl = "Test"
Remove-PnPList -Identity $listTitle
Write-Host -ForegroundColor Magenta "Creating list: " $listURL
New-PnPList -Title $listTitle -Url $listUrl -Template GenericList
$FieldXML= "<Field Type='Note' Name='CustomField' ID='$([GUID]::NewGuid())' DisplayName='CustomField' Viewable ='TRUE' Required ='FALSE' RichText='FALSE'></Field>"
Add-PnPFieldFromXml -List $listTitle -FieldXml $FieldXML
$AllItemsView = Get-PnPView -List $listTitle -Identity "All Items"
$AllItemsView.ViewFields.Add("CustomField")
$AllItemsView.Update()
Invoke-PnPQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.