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

[玩转系统] SharePoint Online:使用 PowerShell 对所有列表和库启用版本控制

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

SharePoint Online:使用 PowerShell 对所有列表和库启用版本控制


要求:对 SharePoint Online 网站中的所有列表启用版本控制。

如何在 SharePoint 中的列表或库上启用版本控制?

SharePoint 中的版本历史记录功能允许您访问以前保存的文档或列表项的副本。默认情况下,SharePoint Online 中的文档库启用版本控制。发生保存操作时,SharePoint 会创建一个新版本以及元数据,例如项目的保存者、创建时间等。要在 SharePoint Online 中启用列表或库的版本控制,请按照以下步骤操作:

  1. 转到文档库 >> 单击功能区上的“库”选项卡 >> 单击“库设置”。
  2. 在常规设置下,单击版本控制设置链接。
  3. 从版本控制设置页面中,选择“创建主要版本”。您还可以选择设置版本数量限制。

    [玩转系统] SharePoint Online:使用 PowerShell 对所有列表和库启用版本控制

  4. 这将启用 SharePoint Online 文档库版本控制。启用版本控制功能后,您可以选择文档或列表项,然后从上下文菜单或功能区中单击“版本历史记录”以查看创建的版本。

SharePoint Online:使用 PowerShell 启用版本控制

您是否需要在 SharePoint Online 中启用文档库的版本控制? PowerShell 可以提供帮助!以下是用于在给定列表或库上启用版本历史记录的 PowerShell:


#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"

Function Enable-SPOVersioning()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )
    Try {
        #Setup Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
         
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        
        #sharepoint online powershell enable versioning
        $List.EnableVersioning = $True
        $List.MajorVersionLimit = 50
        $List.Update()
        $Ctx.ExecuteQuery() 
        Write-host -f Green "Versioning has been turned ON at $ListName"
 
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL="https://Crescent.sharepoint.com"
$ListName="Projects"

#Call the function 
Enable-SPOVersioning -SiteURL $SiteURL -ListName $ListName

用于在 SharePoint Online 中的所有列表和库上启用版本控制的 PowerShell 脚本:

如果您像我一样,您总是在寻找自动化任务并节省时间的方法。考虑到这一点,我想分享一个关于使用 PowerShell 为 SharePoint Online 网站中的所有列表和库启用版本控制的快速技巧。让我们激活 SharePoint Online 网站的所有列表和库上的版本历史记录:


#Load SharePoint Online 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"
   
##Variables for Processing
$SiteUrl = "https://portal.crescent.com.com/sites/Sales/"
$UserName="[email protected]"
$Password ="Password goes here"

try{  
    #Setup the context
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the site and lists
    $web = $context.Web
    $context.Load($web)
    $context.load($web.lists)
    $Context.executeQuery() 
 
    #Iterate through each list in the web
    foreach($list in $web.lists){
    if ($list.hidden -eq $false)
    {
        #Enable versioning
        $list.EnableVersioning = $true
        $lIST.MajorVersionLimit = 50
        $list.Update()
        $Context.ExecuteQuery() 
        Write-host "Versioning has been turned ON at :"$List.title
    }
  }
}
catch{
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
} 

此脚本在 SharePoint Online 中为给定网站上的所有列表和库启用版本控制。如果您想仅对文档库进行过滤和启用版本控制,请使用以下命令:


If($List.BaseType -eq "DocumentLibrary")
{
   #Enable version history
}

SharePoint Online:使用 PnP PowerShell 启用版本控制

要检查是否为列表启用了版本控制功能,请使用:$List.EnableVersioning


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get List
$List = Get-PnPList $ListName 

If($List)
{ 
    #sharepoint online enable versioning powershell
    Set-PnPList -Identity $ListName -EnableVersioning $True -MajorVersions 20
}

同样,您可以为站点上的所有列表配置版本控制设置


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Lists from the web
$Lists = Get-PnPList | Where {$_.Hidden -eq $false}
ForEach($List in $Lists)
{ 
    #Enable versioning and set Number of versions to 50
    Set-PnPList -Identity $List -EnableVersioning $True -MajorVersions 50
    Write-host -f Yellow "Configured Versioning on List:"$List.Title
}

同样,要启用次要版本,请使用:


Set-PnPList -Identity $ListName -EnableVersioning $True -EnableMinorVersions $True -MajorVersions 25 -MinorVersions 25

为站点中的所有文档库启用主要和次要版本的版本控制

以下是为所有库启用次要版本的 PnP PowerShell:


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Neo"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all document libraries
$Libraries =  Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

ForEach($Library in $Libraries)
{ 
    #sharepoint online enable versioning powershell
    Set-PnPList -Identity $Library -EnableVersioning $True -EnableMinorVersions $True
}

如何在整个网站集的所有文档库上设置版本历史记录配置?


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Retail"

#Exclude certain libraries
$ExcludedLists = ("Preservation Hold Library","Site Assets","Site Pages","Style Library","Form Templates")

Function Enable-PnPVersionHistory($SiteURL)
{
    
    Write-host "Processing Web $($_.URL)" -f Cyan
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get All Document Libraries
    $Lists = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.BaseType -eq "DocumentLibrary" -and $ExcludedLists -notcontains $_.Title}

    #Enable Version History for Document Libraries
        $Lists | ForEach-Object {
        $Params = @{
            "Identity"         = $_.Title
            "EnableVersioning" = $True
            "MajorVersions"    = 50
            "MinorVersions"    = 10
        }
        #Set-PnPList @Params
        Write-host "Enabled Version History for "$_.Title -f Green
    }
    
}

#Connect to SharePoint Online Site from PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all Webs and call the function to set version history
Get-PnPSubWeb -IncludeRootWeb | ForEach-Object { Enable-PnPVersionHistory $_.URL }
SharePoint Online 版本历史记录保留多长时间?永远!

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

取消回复欢迎 发表评论:

关灯