Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.
Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.

VIDEO – Toggle Required Fields OFF

When making bulk data changes to SharePoint List or Library you may need to temporarily turn off Required fields.   Opening this can allow us to import records, apply changes, and modify metadata without prompting that required fields are missing.   PowerShell below can help disable all Required fields OFF.    Script generates a CSV snapshot of prior configuration during run.    CSV can be used to restore original configuration for which fields are Required.

Cheers

shades_smile

VIDEO

PowerShell Code

[CmdletBinding()]
param (
    [bool]$required,
    [string]$restoreFilename
)

# Module
Import-Module "SharePointPnPPowerShellOnline" -ErrorAction "SilentlyContinue" | Out-Null

# Config
$appid = "APP ID HERE"
$appsecret = "APP SECRET HERE"

function Main() {
    # Connect
    Connect-PnPOnline -Url "https://tenant.sharepoint.com/" -AppId $appid -AppSecret $appsecret 
    $ctx = Get-PnPContext
    $list = Get-PnPList "Test"
    $list

    if ($restoreFilename) {
        # ENABLE Required Fields
        $csv = Import-Csv $restoreFilename
        $fields = Get-PnPField -List $list
        foreach ($row in $csv) {
            $row.Guid
            $f = $fields | ? { $_.Id -eq $row.Guid }
            $f.Required = $true
            $f.Update()
        }
        $ctx.ExecuteQuery()
    }
    else {
        # DISABLE Required Fields
        $coll = @()
        $guid = (New-Guid).ToString()
        $fields = Get-PnPField -List $list
        foreach ($f in $fields) {
            if ($f.Required) {
                Write-Host "CHANGED FIELD $($f.Title) NOT REQUIRED"
                $f.Required = $false
                $f.Update()
                $coll += $f.Id
            }
        }
        $ctx.ExecuteQuery()
        $coll | Export-Csv "PNPToggleRequiredField-$guid.csv"
    }
}

# Main
Main

GitHub Link

References

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲