How to Change SharePoint List or Library Settings Programmatically with PowerShell?
There is a requirement to change the SharePoint document library draft item security on all document libraries in a sub-site. We used to achieve it by going to:
- Document Library settings >> Versioning Settings.
- Change “Draft Item Security” from “Any user who can read items (default setting)” to “Only users who can edit items” as per the requirement.
Not bad! but there were 50+ document libraries! OMG!! It kept the developer busy for some time!!! Why don’t we script it? sure, Lets change SharePoint List or Library settings programmatically with PowerShell.
Change SharePoint List Settings Programmatically with PowerShell
To set list settings in SharePoint list, use this PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to Get a Single List
function Get-SPList([string]$URL, [string]$ListName)
{
$Web = Get-SPWeb $URL
$List = $Web.Lists[$ListName]
return $List
$Web.Dispose()
}
$WebURL= "https://sharepoint.crescent.com"
#Get the List
$list= Get-SPList $WebURL "Shared Documents"
#Set Draft item security - Who should see draft items in this list?
$list.DraftVersionVisibility = 1
#Any user who can read items = 0. Only users who can edit items = 1. Only users who can approve items (and the author of the item) = 2
#Update list settings
$list.Update()
After making sure it works as expected, I changed the code to loop through required document libraries and apply the settings.
# For MOSS 2007 compatibility
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#Region MOSS2007-CmdLets
Function global:Get-SPSite()
{
Param( [Parameter(Mandatory=$true)] [string]$SiteCollURL )
if($SiteCollURL -ne '')
{
return new-Object Microsoft.SharePoint.SPSite($SiteCollURL)
}
}
Function global:Get-SPWeb()
{
Param( [Parameter(Mandatory=$true)] [string]$SiteURL )
$site = Get-SPSite($SiteURL)
if($site -ne $null)
{
$web=$site.OpenWeb();
}
return $web
}
#EndRegion
#Site URL
$WebURL= "https://sharepoint.crescent.com/regions/emea/"
$Web=Get-SPWeb $WebURL
foreach($List in $web.lists)
{
#Change the setting for all document libraries
if ( ($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) )
{
write-host "Applying Settings on List: $($list.title)"
#Set Draft item security - Who should see draft items in this list?
$list.DraftVersionVisibility = 1
#Any user who can read items = 0. Only users who can edit items = 1. Only users who can approve items (and the author of the item) = 2
#Update list settings
$list.Update()
}
}
Later, I was playing a bit with list properties programmatically. I’m giving it for reference:
Title, description and navigation
#List Name
$list.Title = "Project Metrics"
#List Description
$list.Description = "List to Capture Project Artifacts"
#Navigation: Display this document library on the Quick Launch?
$list.OnQuickLaunch=$false
Versioning Settings
#Content Approval
$list.EnableModeration = $true
#Document Version History Settings
$list.EnableVersioning = $true
$list.EnableMinorVersions = $true #Applicable only to Libraries
$list.MajorWithMinorVersionsLimit = 5 #No. of Drafts in Lists
$list.MajorVersionLimit = 5
#Set Draft item security
#Who should see draft items in this list?
$list.DraftVersionVisibility = [Microsoft.SharePoint.DraftVisibilityType]::Author
#Any user who can read items = 0. Only users who can edit items = 1. Only users who can approve items (and the author of the item) = 2
#Require Check Out:
$list.ForceCheckout = $true
#Set Validation Settings
$list.ValidationFormula = "=[EndDate] > [StartDate] "
$list.ValidationMessage = "End Date must be > Start Date!"
Advanced Settings
#Allow management of content types?
$list.ContentTypesEnabled = $true
Opening Documents in the Browser – Applicable only to Libraries
#Open in the client application
$list.DefaultItemOpen = "PreferClient"
#Open in the browser
$list.DefaultItemOpen = "Browser"
#Use the server default (Open in the client application)
$list.DefaultItemOpenUseListSetting = $false
#Custom Send To Destination - Applicable only to Libraries
$list.SendToLocationName = "Record Center"
$list.SendToLocationUrl = "https://recordcenter.crescent.com/archives/2013/"
#enable disable New Folders
$list.EnableFolderCreation = $false
#Search - Allow items from this document library to appear in search results?
$list.NoCrawl = $true
E-Mail Notification – Send e-mail when ownership is assigned or when an item has been changed. – Applicable only to Task list
# Send e-mail when ownership is assigned?
$list.EnableAssignToEmail = $true
#Offline Client Availability: Allow items from this document library to be downloaded to offline clients?
$list.ExcludeFromOfflineClient = $true
#Site Assets Library: Should this document library be a site assets library?
$list.IsSiteAssetsLibrary = $false
#Datasheet - Allow items in this document library to be edited using the datasheet?
$list.DisableGridEditing = $true
#Dialogs - Launch forms in a dialog?
$list.NavigateForFormsPages = $true
Item-level Permissions: Applicable only to Lists!
#Read access
$list.ReadSecurity = 2
#Read all items = 1. Read items that were created by the user = 2
#Create and Edit access
$list.WriteSecurity = 4
#Create and edit all items = 1. Create items and edit items that were created by the user = 2
.None = 4
#Attachments - Applicable only to Lists!
$list.EnableAttachments = $false
Incoming E-mail settings
$list.EnableAssignToEmail = $true
$list.EmailAlias="TravelClaims" #Incoming E-mail Address
$list.rootFolder.Properties["vti_emailusesecurity"] = 0
$list.rootFolder.Properties["vti_emailsaveattachments"] = 1
#Group attachments in folders. "subject" "sender" "root"
$list.rootFolder.Properties["vti_emailattachmentfolders"] = "subject"
$list.rootFolder.Properties["vti_emailoverwrite"] = 0
$list.rootFolder.Properties["vti_emailsavemeetings"] = 0
$list.rootFolder.Properties["vti_emailsaveoriginal"] = 1
$List.RootFolder.Update();
RSS Settings
$List.EnableSyndication = $true;
$List.RootFolder.Properties["vti_rss_LimitDescriptionLength"]=0
$List.RootFolder.Properties["vti_rss_DocumentAsEnclosure"] = 0
$List.RootFolder.Properties["vti_rss_DocumentAsLink"] = 1
$List.RootFolder.Properties["vti_rss_ItemLimit "] = 25
$List.RootFolder.Properties["vti_rss_DayLimit"] = 7
$List.RootFolder.Update();
Don’t forget to Update the list settings!
$list.Update()
How do we do this for SharePoint Online Library and is it available using PnP?
Here you go: Change List Settings in SharePoint Online using PowerShell
Thank you for this, helped with a problem of configuring incoming mail settings on SharePoint 2016 for document libraries