我正在尝试通过PowerShell删除SharePoint Online中的所有版本历史记录文件。我的研究提供了大量关于如何在SharePoint 2010和2013中执行此操作但不在SharePoint Online中执行此操作的示例。这些文件位于文档库中。下面的脚本似乎很适合我的任务,但我无法修改它以适用于SharePoint Online。要使其适用于SharePoint Online,需要进行哪些更改?
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
# get site
$site = new-object Microsoft.SharePoint.SPSite("http://xxx.sharepoint.com")
# loop through webs
foreach ($web in $site.AllWebs)
{
write-host $web.url
# loop through all lists in web
foreach ($list in $web.Lists)
{
# examine if BaseType of list is NOT a Document Library
if (($list.BaseType -eq "DocumentLibrary") -and ($list.EnableVersioning))
{
# Delete all version history
foreach ($item in $list.Items)
{
# work with the file object as we're in a document library
$file = $item.File
# delete all versions
$file.Versions.DeleteAll()
}
}
}
}
$web.Dispose();
$site.Dispose();
答案 0 :(得分:1)
以下代码结合了PowerShell和CSOM。它对我有用。您可以使用循环中的计数器删除所有版本或调整要删除的版本。
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$password = Read-Host -Prompt "Enter password" -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("userID@yourtenant.com", $password)
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursitecollection"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$context.Credentials = $credentials
$fileUrl = "/sites/yoursitecollection/path_to_file/filename";
$versions = $context.Web.GetFileByServerRelativeUrl($fileUrl).Versions;
$context.Load($versions)
$context.ExecuteQuery()
for($i=2;$i -lt $versions.Count-2; $i++)
{
$versions[$i].DeleteObject()
$context.ExecuteQuery()
}
答案 1 :(得分:0)
对于旧版本的SharePoint,您可能需要模仿浏览器。本质上,您将请求版本历史记录页面内容,获取REQUESTDIGEST和VIEWSTATE,并使用获得的值对特制URL执行POST请求,并为每个不需要的版本执行请求正文。
例如:
$httpResponse = Invoke-WebRequest -Uri "$baseUri/_layouts/15/Versions.aspx?list=$listId&ID=$id" -UseDefaultCredentials
$httpResponse.Forms["aspnetForm"].Fields["__VIEWSTATE"]
$httpResponse.Forms["aspnetForm"].Fields["__REQUESTDIGEST"].Replace(" ","+")
...
$httpResponse = Invoke-WebRequest -UseDefaultCredentials -MaximumRedirection 0 -ErrorAction SilentlyContinue -Method "POST" `
-Uri "$baseUri/_layouts/15/versions.aspx?list=$listId&ID=$($_.ID)&col=Number&order=d&op=Delete&ver=$($_.Version)" `
-ContentType "application/x-www-form-urlencoded" -Body ("MSOWebPartPage_PostbackSource=&MSOTlPn_SelectedWpId=&MSOTlPn_View=0&MSOTlPn_ShowSettings=False&MSOGallery_SelectedLibrary=&MSOGallery_FilterString="+
"&MSOTlPn_Button=none&__EVENTTARGET=&__EVENTARGUMENT=&MSOSPWebPartManager_DisplayModeName=Browse&MSOSPWebPartManager_ExitingDesignMode=false&MSOWebPartPage_Shared=&MSOLayout_LayoutChanges=&MSOLayout_InDesignMode=&MSOSPWebPartManager_OldDisplayModeName=Browse&MSOSPWebPartManager_StartWebPartEditingName=false&MSOSPWebPartManager_EndWebPartEditing=false&_maintainWorkspaceScrollPosition=0"+
"&__REQUESTDIGEST=$digest&__VIEWSTATE=$([System.Net.WebUtility]::UrlEncode($viewstate))&__VIEWSTATEGENERATOR=01175E75&__SCROLLPOSITIONX=0&__SCROLLPOSITIONY=0"+
"&__EVENTVALIDATION=$([System.Net.WebUtility]::UrlEncode($ev))")
if ($httpResponse.StatusCode -eq 302) { #success
write-host "v" -NoNewline -foregroundcolor DarkGreen
$vCount++
} else { #some error
write-host "x" -NoNewline -foregroundcolor Red
if ($httpResponse.Forms[0].Action.StartsWith("error")) {
write-host $httpResponse.Content.Substring($httpResponse.Content.IndexOf("ctl00_PlaceHolderMain_LabelMessage")+36,300).Split("<")[0] -ForegroundColor Magenta
}
...
}
请注意,POST之后无需遵循302重定向。