Tuesday, 12 October 2010

Change SharePoint list settings using PowerShell - Part 1: General and Versioning

Let’s say that you have 100 team sites created and being used with a document library called Shared Documents available in each site. You are then asked to change the versioning settings on each Shared Documents library in every one of those 100 sites to create major and minor versions. To resolve this requirement for any new document libraries and sites created in the future, you can wrap up the changes into list and site templates. However, doing this will not affect any of the 100 existing document libraries already being used.

Enter PowerShell. In part one of this series, I will show you how you can change the General and Versioning Settings of a list. First, we’ll change the settings of a single list in a single site. Whilst this is easy enough to do in the UI and unlikely to be used in a script, it will give you an idea of how the script works:

#Get the site and list objects
$web = Get-SPWeb http://portal/team
$list = $web.Lists["Shared Documents"]

#Make the list changes
$list.Title = "Documents"

#Update the list and dispose of the web object
$list.Update()
$web.Dispose()

The script above gets a document library called “Shared Documents” in the site “http://portal/team”, sets a new title for the document library to “Documents”, and updates the settings for the list. If there is more than one setting to change, we could have any number of settings defined under the “#Make the list changes” line and they will all be updated by the $list.Update() command.

The script below would certainly take a lot longer to do in the UI, as it walks through each site in the “http://portal” site collection, changing the title and description of the document library called “Shared Documents” in each site – all you have to do is tell the script the relevant site collection URL, list name, and which settings you want to change on each list with the name specified:

#Change these variables to your site URL and list name
$site = Get-SPSite http://portal
$listName = "Shared Documents"

#Walk through each site in the site collection
$site | Get-SPWeb | ForEach-Object {

#Get the list in this site
$list = $_.Lists[$listName]

#Make the list changes
$list.Title = "Documents"
$list.Description = "Use the Documents library to store shared files"

#Update the list
$list.Update()
}

#Dispose of the site object
$site.Dispose()

The table below shows the other General and Versioning Settings we can also change, with snapshots of the same settings from the list admin UI. To use any of these settings from the table, copy them into the script above, underneath the “#Make the list changes” line.

General Settings

List Name image
The following command will change the list display name (note, this will not change the list URL):

$list.Title = "Documents"
List Description image
The following command will change the list description:

$list.Description = "Use the Documents library to store shared files"
Navigation image
To configure the list so that it shows on the Quick Launch (left-hand site navigation), use this command:

$list.OnQuickLaunch = $true

Change the $true to $false if you want to remove the list from being shown on the Quick Launch.

Versioning Settings

Content Approval image
This command will change the list to require approval for all items:

$list.EnableModeration = $true

Change the $true to $false if you want to set content approval to No.
Document Version History
(settings and wording can be different between document libraries and lists, as annotated in the script code)
image
There are a few settings on this section, which I have annotated in the commands below:

#Create a version each time you edit an item in this list (lists)
#Create major versions (document libraries)
$list.EnableVersioning = $true

#Create major and minor (draft) versions (document libraries only)
$list.EnableMinorVersions = $true

#Keep the following number of versions (lists)
#Keep the following number of major versions (document libraries)
$list.MajorVersionLimit = 5

#Keep drafts for the following number of approved versions (lists)
#Keep drafts for the following number of major versions (document libraries)
$list.MajorWithMinorVersionsLimit = 5
Draft Item Security image
This setting uses a numerical value to determine which option is set:
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

For example, to configure this setting for “Only users who can edit items”, as pictured above, use this command:

$list.DraftVersionVisibility = 1
Require Check Out
(document libraries only)
image
This command will change the list to require documents to be checked out before they can be edited:

$list.ForceCheckout = $true

In Part 2 of this series, I cover how to use PowerShell for configuring the Advanced Settings of a list.

40 comments:

  1. This saved me, oh... say missing lunch all week. Thanks for the post!! Do you know how what the setting to enable item scheduling?

    ReplyDelete
  2. Hi Razorduck - glad I managed to save you some time. Item scheduling is enabled/disabled using a couple of PublishingWeb methods rather than list settings.

    To disable:
    $web = Get-SPWeb http://portal
    $list = $web.Lists["Pages"]
    [Microsoft.SharePoint.Publishing.PublishingWeb]::DisableScheduling($list)

    To enable:
    $web = Get-SPWeb http://portal
    $list = $web.Lists["Pages"]
    [Microsoft.SharePoint.Publishing.PublishingWeb]::EnableScheduling($list)

    Enjoy your lunch :-)

    ReplyDelete
  3. This is great - do you know how to list all the list objects that could be modfied via PowerShell? Specifically how to change the incoming Email settings?

    Thanks!! Eric

    ReplyDelete
  4. Hi Eric,

    Once you have got the $list variable set up, you can show all list properties and methods available by typing the following:

    $list | get-member

    I'm not sure off the top of my head which properties change the incoming e-mail settings, but there is part three of this article coming soon, so I will investigate and include it in that.

    ReplyDelete
  5. Hmmm - I must be doing something wrong - here is what I typed in after setting list variables (which I tested by changing the title of the list)
    PS D:\backups> $list | get.member
    The term 'get.member' is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:19
    + $list | get.member <<<<
    + CategoryInfo : ObjectNotFound: (get.member:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Any ideas?

    Thanks - Eric

    ReplyDelete
  6. Hi Eric,

    You are doing something wrong :-)

    its "get-member" with a hyphen, not "get.member" with a full stop

    ReplyDelete
  7. Brilliant!! It's always the obvious!

    Thanks - I'll look through the values for the ones I'm looking for. Eric

    ReplyDelete
  8. Thanks Phil!
    This is helps beyond belief. Every time we make a site collection its created with 5 sub-sites...each needing specific settings on document libraries. Previously we've had to go through and set it all manually.
    I've been looking for this code for 2 months...it's a life saver!
    :-)

    ReplyDelete
  9. Hi Phil,

    U have great posts. Congrats

    Do you know how can i show all columns of my content type in default view of document library?

    ReplyDelete
  10. Javier - I'll be posting something in the future on configuring list views with PowerShell

    ReplyDelete
  11. To retrieve the Incoming Mail setting from list you can use the following code sample :
    Function GetIncomingMailSetting([Microsoft.SharePoint.SPList]$thisList)
    {
    Try
    {
    #Receive email
    if($_.EmailAlias.length -ge 1)
    {
    $LReceiveEmail = $true
    $IEAlias = $_.EmailAlias
    [System.Collections.Hashtable]$Hashtable = $_.RootFolder.Properties

    #Group attachments in folders?
    $IEemailattachmentfolders = $Hashtable["vti_emailattachmentfolders"]
    #E-mail security policy:
    $IEemailusesecurity = $Hashtable["vti_emailusesecurity"]
    #Save meeting invitations?
    $IEemailsavemeetings = $Hashtable["vti_emailsavemeetings"]
    #Overwrite files with the same name?
    $IEemailoverwrite = $Hashtable["vti_emailoverwrite"]
    #Save original e-mail?
    $IEemailsaveoriginal = $Hashtable["vti_emailsaveoriginal"]

    }
    else
    {
    $LReceiveEmail = $False
    }
    write-host "$LCanReceiveEmail;$LReceiveEmail;$IEAlias;$IEemailattachmentfolders;$IEemailusesecurity;$IEemailsavemeetings;$IEemailoverwrite;$IEemailsaveoriginal"
    }
    catch [System.Exception]
    {
    $Errors = $Errors.AppendLine("Error in Function GetIncomingMailSetting)
    $Errors = $Errors.AppendLine("Error details : $_")
    }

    }

    ReplyDelete
  12. I must be doing something wrong, can you examine the script below and see whats wrong- this script should run thourhg about 100 document libraries and make minor changes. When I run it I get no output in the PS window and it just goes back to a command prompt.

    #Change these variables to your site URL and list name
    $site = Get-SPSite http://www.sitename.org

    #Go through each document library in the site
    $_.Lists | where { $_.BaseTemplate -eq "DocumentLibrary" } | ForEach-Object {

    write-host "Checking list:"$_.Title

    #Open Documents use server default
    $list.DefaultItemOpenUseListSetting = $false

    #change/add send to location to the teacher drop box
    $list.SendToLocationName = "Submit Item to Teacher"
    $list.SendToLocationUrl = "http://files.sitename.org/Submitted%20Items"

    }

    #Dispose of the site object
    $site.Dispose()

    ReplyDelete
  13. Thanks Weedo .Net Engineering! Nice update...

    ReplyDelete
  14. Damon - try this:

    #Get site collection
    $site = Get-SPSite http://portal

    #Go through each site in the site collection
    $site | Get-SPWeb -limit all | ForEach-Object {

    write-host "Checking site:"$_.Title

    #Go through each document library in the site
    $_.Lists | where { $_.BaseTemplate -eq "DocumentLibrary" } | ForEach-Object {

    write-host "Checking list:"$_.Title

    #Open Documents use server default
    $_.DefaultItemOpenUseListSetting = $false

    #change/add send to location to the teacher drop box
    $_.SendToLocationName = "Submit Item to Teacher"
    $_.SendToLocationUrl = "http://files.sitename.org/Submitted%20Items"

    #Update list
    $_.Update()
    }

    }

    #Dispose of the site object
    $site.Dispose()

    ReplyDelete
  15. That worked perfectly, thank you very much.

    ReplyDelete
  16. Hello, i have an urgent question:
    How can i do the same whit powershell but with a feature instead of a document library? i need to setup a feature in about 600 subsites. Can you help me please?

    ReplyDelete
  17. http://get-spscripts.com/2010/06/install-farm-solution-and-bulk-activate.html

    ReplyDelete
  18. I've just found my newest favorite blog. Thanks so much for this!

    ReplyDelete
  19. Any idea on how to amend the title in the URL too? When you change the title the URL to the list remains with it's old name.

    ReplyDelete
  20. I like the table with the pictures of the fields being edited in sharepoint and their powershell equivalients. Keeps me from having to think... and I'm an american, I hate thinking! :)

    ReplyDelete
  21. Is there a way command that will allow me to search through a site collection of blank sites with one Document library each? I was thinking something like $listName = [$ListName] at the begining of the script, but does not seem to work. Could you help me with the proper syntax to do this for any library in a site regardless of the existing name please?

    ReplyDelete
  22. Hello, I Would like to know if there is a way to reset versioning on multiple document libraries below a given URL, using standard SharePoint admin commands - such as stsadmin or PowerShell

    ReplyDelete
  23. You are a bloody genius. Why can't I find a 'Follow' button on this Blog?

    ReplyDelete
  24. razorduck / Mike O.9 January 2013 at 00:11

    Hello Phil, thanks so much for your help with scripting. I have been able to use the above scripts but on one site colleciton I am getting this type of error "Property 'DraftVersionVisibility' cannot be found on this object; make sure it
    exists and is settable." Do you happen to know why. The account that I am running the script with is a Farm and Site Collection Admin.

    ReplyDelete
  25. Thanks Phil this is great, I'm wondering if its possible to change the description in all libraries within the Site Collection, I just need the same description for all or simply change them to blank...
    Thanks

    ReplyDelete
  26. Great post, and comments though I have hit a problem in my environment; My document libraries do not have the properties mentioned to edit the incoming email settings - does anyone have suggestions?!

    Thanks
    BOB

    ReplyDelete
  27. Thank you how We can set the rating setting yes through power shell

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. Hi,

    If anyone is following post, I have a question:
    how do I _check_ if a library has scheduling enabled ?

    Thanks

    ReplyDelete
  30. Really informative post. Thanks!

    ReplyDelete
  31. For those that have been waiting for new colorways of the Nike LeBron Soldier 10, then check out this newly released Nike Kobe 9 LeBron Soldier 10 Team Collection.This collection includes 14 brand new colorways all dressed in team-inspired colors. Perfect for college football kick-off, you can now rep your school in one of these fourteen colors.Check out Kobe shoes the entire lineup below and for those that are interested in adding one of these to their collection, you can find them available directly on Eastbay. The retail price tag is set at $130 Kyrie 1 USD.
    Is Under Armour, a brand built on hardcore training and in-the-gym apparel, ready for the lifestyle look? There’s no question that the brand has been on the receiving end of the surge in LeBron 12 the fitness lifestyle, and the emergence of Steph Curry has only further legitimized UA as a contender in footwear within and beyond the basketball category. Today, Under Armour unveils their Modern Sport Collection for KD 6 women, a rich selection of sport-inspired lifestyle silhouettes built with rich tonal leathers in black and rose pink as well as a third line with an energetic black/white static-y mesh. One of the standouts KD Shoes For Sale is the Charged Pivot lifestyle shoe, a clear off-shoot of the super popular Curry One (for the Ayeshas, not the Stephs). Right now the black leather options as well as the Charged All Around Curry Shoes are available on the UA site, with prices ranging from $100-140. The rose colorways will drop on November 15th.
    After a decisive Gold Medal-winning game over Serbia, Nike Basketball commemorates the achievement with a Nike Zoom Kobe Icon KD 9, Kyrie 2, and Lebron Soldier 10.Each sneaker’s theme within the pack is simple, a touch of gold mixed with other colors. Both the Kyrie and KD models feature red to match the Kobe venomenon 5 USA jerseys, while the former features a black upper and an icy blue sole. The LeBron Soldier 10 not only matches the golden sneaker trend to a tee with white and gold throughout, the Kobe high tops shoe actually looks like one of the PEs that LeBron James wore in the NBA Finals.You can pick up either the KD 9, Kyrie 2, or LeBron Soldier 10 in the Nike Basketball Gold Nike Hyperrev

    ReplyDelete