Hiding Fields in a SharePoint Form with PnP and PowerShell [O365]
I decided to no longer keep my small snippets and scripts that I create (only) in my OneDrive… but to publish them here. Hiding fields in a SharePoint form with PnP and PowerShell is thus the first article with such content.
What’s the Problem?
Well, the problem is: In the SchemaXml of a field, visibility within forms can be controlled with ShowInDisplayForm, ShowInNewForm, and ShowInEditForm. At the surface, you only have the option to hide the field in the content type. A dedicated show/hide is thus not possible.
The experienced SharePoint PowerUser - or admin - used to be able to accomplish this via the SharePoint Manager. Something like this also exists for SharePoint Online - I’m thinking of the SharePoint Online Client Browser - but it doesn’t save changes.
Which brings us to our problem. Now I, as an old SharePoint developer, have several options to realize my plan. I could write a console application (.NET), a UWP application, or a PowerShell script with CSOM. The latter can be written, tested, and executed faster. But CSOM (or generally .NET) in PowerShell isn’t really fun. Plus I have to write many lines of code - it can also be simpler, namely with PnP.
PnP and PowerShell
The PnP PowerShell commands for SharePoint have been around for a while. The PnP PS commands provide a wrapper around CSOM to write less code and also execute simple commands directly in PowerShell.
Here’s an example with pure CSOM and the PnP counterpart. The description of “My Field” is to be set programmatically:
PowerShell with CSOM:
$user = "aytac@kirmizi.online"
$pass = "laangesPa$$w04t"
$siteUrl = "https://aytac.sharepoint.com/sites/Example"
$listTitle = "My List"
$fieldTitle = "My Field"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, (ConvertTo-SecureString $pass -AsPlainText -Force))
$list = $context.Web.Lists.GetByTitle($listTitle)
$field = $list.Fields.GetByTitle($fieldTitle)
$field.Description = "This is my field"
$field.UpdateAndPushChanges($true)
$context.Load($field)
$context.ExecuteQuery()
PowerShell with PnP:
$siteUrl = "https://aytac.sharepoint.com/sites/Example"
$listTitle = "My List"
$fieldName = "MyField"
Connect-PnPOnline -Url $siteUrl
$field = Get-PnPField -List $listTitle -Identity $fieldName
$field.Description = "This is my field"
$field.UpdateAndPushChanges($true)
$field.Context.ExecuteQuery()
The second example looks much tidier. It can even be shorter! With the Set-PnPField command, we can set this property directly:
$siteUrl = "https://aytac.sharepoint.com/sites/Example"
$listTitle = "My List"
$fieldName = "MyField"
Connect-PnPOnline -Url $siteUrl
Set-PnPField -List $listTitle -Identity $fieldName -Values @{Description="This is my field"}
Great! Can I start right away?
So that the PnP PowerShell commands can be used, they must first be installed. How to install these extensions can be read here. The installation is worth it - don’t shy away from it.
Back to the Topic: Hiding Fields with PnP and PowerShell
In the article “Hiding Fields in a SharePoint Form with PnP and PowerShell” there’s a clear task. The visibility of fields should be easily manipulated.
Now we package everything into a PowerShell script and call this script HideFieldInList.ps1. This file is empty, now we fill it with the content from the following code block.
Param(
[parameter(Mandatory=$true)]
[alias("url")]
$siteUrl,
[parameter(Mandatory=$true)]
[alias("list")]
$listTitle,
[parameter(Mandatory=$true)]
[alias("field")]
$fieldName,
[alias("display")]
$hideInDisplayForm=$true,
[alias("new")]
$hideInNewForm=$true,
[alias("edit")]
$hideInEditForm=$true
)
Connect-PnPOnline -Url $siteUrl -Credentials (Get-Credential)
$field = Get-PnPField -List $listTitle -Identity $fieldName
if($field.Sealed){
throw 'field is sealed'
}
if($field.Hidden){
throw 'field is hidden'
}
$field.SetShowInDisplayForm(!$hideInDisplayForm)
$field.SetShowInNewForm(!$hideInNewForm)
$field.SetShowInEditForm(!$hideInEditForm)
try{
$field.UpdateAndPushChanges($true)
$field.Context.ExecuteQuery()
} catch {
$errorMessage = "error while applying changes for field: " + $field + "... " + $_.Exception.Message
throw $errorMessage
}
Write-Host "field updated" $field
Done! How do I use this Script?
We now need a PowerShell instance. Either we start it directly via Windows and then navigate via cd to the folder where we’ve saved our script, or we click in the address bar of our Explorer instance and type powershell and press Enter. A PowerShell instance should then open in the correct path.
The execution is now quite simple. We have 3 required parameters url, list, and field. These parameters must be provided when calling the script:
./HideFieldInList.ps1 -url https://aytac.sharepoint.com/sites/Example -list "My List" -field MyField
Hides the field in the Display, New, and Edit form.
Otherwise, we can control the respective visibility via the optional parameters. Suppose the field in question should only be hidden in the Edit and New form, but should still be visible in the Display form:
./HideFieldInList.ps1 -url https://aytac.sharepoint.com/sites/Example -list "My List" -field MyField -display $false
Hides the field in the New and Edit form.
I probably don’t need to say more, except that any use is at your own risk and on your own responsibility. Have fun with it :)