当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 管理未签入版本的文件

作者:精品下载站 日期:2024-12-14 14:40:32 浏览:14 分类:玩电脑

SharePoint Online:使用 PowerShell 管理未签入版本的文件


当用户在上传文件时(在经典体验中)没有为强制元数据字段值提供值,或者当用户忘记签入文档而任何库的版本控制设置下的“需要签出”选项设置为“是”时,则这些文件被称为“没有签入版本的文件”。上传的文件将存储在 SharePoint 上,但将被检出并仅对上传者可见!当用户使用文件资源管理器视图上传时经常会发生这种情况!

在 SharePoint Online 中管理没有签入版本的文件:

作为站点管理员,您可以取得没有签入版本的文件的所有权。为了控制,

  • 转到“库设置”下的“管理没有签入版本的文件”链接,
  • 选择文档并单击“取得选择的所有权”按钮并确认提示。
  • 一旦您获得所有权,文档就会从此页面中删除,并开始出现在上传的列表中(仅向您显示,直到您签入它们为止!)作为网站所有者,您可以根据需要设置元数据属性,检查-in 并使这些文档对所有用户可见。

    [玩转系统] SharePoint Online:使用 PowerShell 管理未签入版本的文件

PowerShell 获取所有权并签入所有文件SharePoint Online 中没有签入版本的文件:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/sites/Marketing/"
$ListName ="Documents"

Try{
    #Get Credentials to connect
    $Cred= Get-Credential
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Get Checked out Files with no checked-in version
    $List = $Ctx.Web.lists.GetByTitle($ListName)
    $CheckedOutFiles = $List.GetCheckedOutFiles()
    $Ctx.Load($List)
    $Ctx.Load($CheckedOutFiles)
    $Ctx.ExecuteQuery()

    #Loop through each checked-out file
    ForEach($File in $CheckedOutFiles)
    {
        #Take Ownership of the File
        $CheckedOutByUser=$File.CheckedOutBy
        $Ctx.Load($CheckedOutByUser)
        $File.TakeOverCheckOut()
        $Ctx.ExecuteQuery()

        #Get the File and Check-in
        $CheckInFileURL = [String]::Concat($List.ParentWebUrl, $File.ServerRelativePath.DecodedUrl.Replace($List.ParentWebUrl, [string]::Empty))
        $CheckInFile = $Ctx.web.GetFileByServerRelativeUrl($CheckInFileURL)
        $Ctx.Load($CheckInFile)
        $Ctx.ExecuteQuery()

        #Check in the file
        $CheckInFile.CheckIn("Checked-in from PowerShell",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
        $Ctx.ExecuteQuery()

        Write-Host -f Yellow "'$($CheckInFile.name)' at $($CheckInFile.ServerRelativeUrl) was Checked Out by: $($CheckedOutByUser.Email)"
        Write-host -f Green "Checked In The File Successfully!"         
    }
}
Catch {
        write-host -f Red "Error:" $_.Exception.Message
}

我可以取得特定用户的所有权并签入所有文件吗?当然:


#Get the User
$User = $Web.EnsureUser("[email protected]");

#Check if the File is checked out to the specific user
If($File.CheckedOutById -eq $User.Id)
{
 #Take ownership and check-in
}

PnP PowerShell 签入所有未签入版本的签出文件

此 PowerShell 脚本签入特定用户检出的所有文件。


#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding"
$UserID = "i:0#.f|membership|[email protected]"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext

#Get the List
$List = Get-PnPList $ListName
#Get the User
$CheckedoutByUser = Get-PnPUser -Identity $UserID

#Get All Checked-Out Files
$CheckedOutFiles = $List.GetCheckedOutFiles()
$Ctx.Load($CheckedOutFiles)
$Ctx.ExecuteQuery()

#Check-in All Files Checked out to the User
$CheckedOutFiles | ForEach-Object {
    If($_.CheckedOutById -eq $CheckedoutByUser.Id)
    {
        #Take over the Checked-Out File
        $_.TakeOverCheckOut()
        $Ctx.ExecuteQuery()

        #Check-in the file
        $CheckInFileURL = [String]::Concat($List.ParentWebUrl, $_.ServerRelativePath.DecodedUrl.Replace($List.ParentWebUrl, [string]::Empty))
        Set-PnPFileCheckedIn -Url $CheckInFileURL -CheckinType MajorCheckIn -Comment "Checked-In by the Script at $(Get-Date)"    
        Write-Host "Check-in the File: $CheckInFileURL"    
    }
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯