Add site column to document library (Add-PnPField) #1722
-
I'm (desperately) trying to add a site column to a document library. Creating a site column succeeds, adding content types to it also succeeds, but the column is not visible in the "web view" of the library, i.e. when I use a browser to get to the library the column doesn't show. However, when I add a document and look at its properties then I do see the column (but only for editing its properties). Also in the site's and library's properties the column is there. The code I use to create the column goes as follows: #Define column attributes #Add site column #Add content types to the column |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Would something like this do it: Connect-PnPOnline -url "https://contoso.sharepoint.com/sites/marketing" -Credentials 'user@domain.com'
#Define variables
$ColumnDisplayName = "ChoiceTest05"
$ColumnInternalName = "ChoiceTest0501"
$Choices = "Choice 05.1", "Choice 05.2", "Choice 05.3"
$ListName = "Documents"
$contentTypeName = "MyContentType01"
$viewName = "All Documents"
$Context = Get-PnPContext
#Add site column
Add-PnPField -DisplayName $ColumnDisplayName -InternalName $ColumnInternalName -Type Choice -Choices $Choices
#Add content types to the column
#Creates Content Type
$ct = Get-PnPContentType -Identity "Document"
$contentType = Add-PnPContentType -Name $contentTypeName -ParentContentType $ct -Description "Create a new document" -Group "_PnP"
#Add Site Column To Content Type
$add = Add-PnPFieldToContentType -Field $ColumnInternalName -ContentType $contentType
#Add Site Content Type To List
Add-PnPContentTypeToList -List $ListName -ContentType $contentTypeName -DefaultContentType
#Get the List View from the list
$ListView = Get-PnPView -List $ListName -Identity $viewName
#Add Column to View
$ListView.ViewFields.Add($ColumnInternalName)
$ListView.Update()
$Context.ExecuteQuery() |
Beta Was this translation helpful? Give feedback.
@KrisVG-CRS
Would something like this do it: