In SharePoint, you have the ability to set the number of versions to retain through the settings of the specific document libraries. I was recently faced with a challenge where a company implemented a new global policy which mandated only 10 versions be retained. The policy had be implemented by notice of the collection administrators after users were sufficiently notified of the change so I it had to be something that was run
在SharePoint中,您可以通过特定文档库的设置来设置要保留的版本数。 最近,我面临一个挑战,一家公司实施了一项新的全球政策,该政策要求仅保留10个版本。 在通知用户足够的更改后,该策略已通过收集管理员的通知实施,因此我必须将其运行
Unfortunately, with the first library they updated, they noticed when you changed the version retention number, the libraries did not automatically remove the older versions. Only when you performed a future update of the document, did the older versions get trimmed off.
不幸的是,随着他们更新了第一个库,他们注意到当您更改版本保留号时,这些库并未自动删除较旧的版本。 仅当您执行文档的将来更新时,才会删除旧版本。
After a little research, I couldn't find anything online which offered the solution I was looking for. In the end I wrote a simple but very fast, effective, and powerful PowerShell script.
经过一番研究,我找不到任何能提供所需解决方案的在线产品。 最后,我编写了一个简单但快速,有效且功能强大的PowerShell脚本。
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
# 2 variables which need to be set
$siteCollection = "http://SharePoint/Sites/SiteCollection"
$documentRetentionCount=10
# get site collection
$site = new-object Microsoft.SharePoint.SPSite($siteCollection)
# loop through each subsite
foreach ($web in $site.AllWebs)
{
write-host $web.url
# loop through all lists in each subsite
foreach ($list in $web.Lists)
{
# examine if BaseType of list is a Document Library and if versioning is turned on
if (($list.BaseType -eq "DocumentLibrary") -and ($list.EnableVersioning))
{
# Set the Major version limit to keep the latest 10 versions
$list.MajorVersionLimit = $documentRetentionCount
$list.Update()
foreach($item in $list.Items)
{# Perform a system update on each item
$item.SystemUpdate($true)
}
}
}
}
$web.Dispose();
$site.Dispose();
I've documented the code inline but I'm going to walk through it for clarity sake:
我已经内联记录了代码,但是为了清楚起见,我将逐步遍历它:
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.Powe
if((Get-PSSnapin |其中{$ _。Name -eq“ Microsoft.SharePoint.Powe rShell“})-eq $ null){
Add-PSSnapin Microsoft.SharePoint.Power
Add-PSSnapin Microsoft.SharePoint.Power 贝壳;
}
}
1. First you have to make sure the snap-ins are loaded. I've used this method for a long time because when I ran, then subsequently re-ran the script after making edits, it would say the snap-in was already loaded. This way, it will check first, then only load it if necessary.
1.首先,您必须确保已加载管理单元。 我使用这种方法已经有很长时间了,因为当我运行该脚本时,然后在进行编辑后又重新运行了该脚本,这表明该管理单元已经加载。 这样,它将首先检查,然后仅在必要时加载它。
$siteCollection = "http://SharePoint/Sites/SiteCollection"
$ siteCollection =“ http:// SharePoint / Sites / SiteCollection ”
$documentRetentionCount=10
$ documentRetentionCount = 10
2. The next block lists the two variables that are specific to a run. My next version of this will be to set the web application and loop through all site collections, then subsites, and finally all document libraries.
2.下一块列出了运行特定的两个变量。 我的下一个版本是设置Web应用程序并遍历所有网站集,然后遍历子网站,最后遍历所有文档库。
foreach ($web in $site.AllWebs)
foreach($ site.AllWebs中的$ web)
{
{
write-host $web.url
写主机$ web.url
3. The first foreach loop loops through each subsite in the site collection, displaying the current subsite URL in the console window.
3.第一个foreach循环遍历网站集中的每个子网站,在控制台窗口中显示当前子网站URL。
foreach ($list in $web.Lists)
foreach($ web.Lists中的$ list)
{
{
if (($list.BaseType -eq "DocumentLibrary") -and ($list.EnableVersioning))
如果((($ list.BaseType -eq“ DocumentLibrary”)-和($ list.EnableVersioning))
4. It then loops through each list looking only for Document Library type lists which have versioning enabled
4.然后,它遍历每个列表,仅查找启用了版本控制的文档库类型列表
5. The script then sets the major version limit for the versioning-enabled document library to the specified variable and saves the updated property value to the list.
5.然后,脚本将启用版本控制的文档库的主要版本限制设置为指定的变量,并将更新的属性值保存到列表中。
foreach($item in $list.Items)
foreach($ list.Items中的$ item)
{
{
$item.SystemUpdate($true)
$ item.SystemUpdate($ true)
}
}
6. Now the important part in my situation was updating each item to remove any version greater than the one specified in the variable which actually removes the versions outside of the newly defined limit.
6.现在,根据我的情况,重要的部分是更新每个项目,以删除大于该变量中指定的版本的任何版本,而该变量实际上删除了新定义的限制之外的版本。
I know it's not rocket science but it saved an SP administrator time and effort of going through each library and setting the version number and updating each item to apply the setting. Doing it manually would have also updated the "Modified Date" and "Modified By" fields for each document which would have also sent out alerts if there were any set.
我知道这不是火箭科学,但它节省了SP管理员的时间和精力,可以遍历每个库并设置版本号并更新每个项目以应用设置。 手动执行此操作还将为每个文档更新“修改日期”和“修改者”字段,如果有任何设置,它们也会发出警报。