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

[玩转系统] SharePoint Online:使用 PowerShell 复制列表视图

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

SharePoint Online:使用 PowerShell 复制列表视图


要求:使用 PowerShell 在 SharePoint Online 中复制视图。

如何在 SharePoint Online 中复制列表视图?

您是否需要在 SharePoint Online 中创建列表视图的副本?也许您需要对视图进行一些更改,并且不想影响原始视图。或者您可能只是想拥有视图的备份副本,以防原始视图发生问题。无论如何,复制列表视图都很容易。本文将向您展示如何在 SharePoint Online 中复制列表视图。

在 SharePoint Online 中复制视图是一个简单的过程,可以使用用户界面完成。步骤如下:

  1. 导航到要复制视图的列表或库。
  2. 在“视图”部分下,单击要复制的视图以打开其设置。
  3. 单击视图下拉菜单中的“视图另存为”按钮。

    [玩转系统] SharePoint Online:使用 PowerShell 复制列表视图

  4. 在“视图另存为”对话框中,输入新视图的名称并选择是否将其设为默认视图。
  5. 单击“确定”保存新视图。

新视图将创建为原始视图的副本,具有相同的设置和过滤器。您可以根据需要修改新视图,而不会影响原始视图。如果您使用的是经典体验,要通过 UI 复制 SharePoint 列表中的视图,请执行以下步骤:

  1. 导航到列表设置>>向下滚动到视图部分>>单击“创建视图”链接。
  2. 在“从现有视图开始”部分下,选择任何现有视图。

    [玩转系统] SharePoint Online:使用 PowerShell 复制列表视图

为新视图提供名称,根据需要进行修改,然后单击“确定”复制视图。

如何使用 PowerShell CSOM 复制 SharePoint 视图?

若要复制 SharePoint Online 中的视图,请使用此 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 to copy list view
Function Copy-SPOListView()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ViewName,
        [Parameter(Mandatory=$true)] [string] $NewViewName
    )
    Try { 
        #Get 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 by title
        $List=$Ctx.web.Lists.GetByTitle($ListName)
        
        #Get the View to Copy 
        $View = $List.Views.GetByTitle($ViewName)
        $Ctx.Load($view)
        $Ctx.Load($View.ViewFields)
        $Ctx.ExecuteQuery()

        #Copy the List View
        $ViewCreationInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
        $ViewCreationInfo.Paged = $View.Paged
        $ViewCreationInfo.PersonalView = $View.PersonalView
        $ViewCreationInfo.Query = $View.ViewQuery
        $ViewCreationInfo.RowLimit = $View.RowLimit
        $ViewCreationInfo.Title = $NewViewName
        $ViewCreationInfo.SetAsDefaultView = $View.DefaultView 
        $ViewCreationInfo.ViewFields = $View.ViewFields
        $ViewCreationInfo.ViewTypeKind = $View.ViewType
 
        $List.views.Add($ViewCreationInfo)  | Out-Null
        $Ctx.ExecuteQuery()

        write-host -f Green "List View has been Copied Successfully!"
     }
    Catch {
        write-host -f Red "Error Copying List View!" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"
$ListName="Projects"
$ViewName="All Items"
$NewViewName="All Projects"

#Call the function to copy view
Copy-SPOListView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName -NewViewName $NewViewName

用于克隆 SharePoint Online 中的视图的 PnP PowerShell

或者,您也可以使用 PnP PowerShell 复制视图。这是脚本:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ViewName = "Active Projects"
$NewViewName = "Current Projects"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the Source View
    $View = Get-PnPView -List $ListName -Identity $ViewName -Includes ViewType, ViewFields, Aggregations, Paged, ViewQuery, RowLimit
    
    #Get Properties of the source View
    $ViewProperties = @{
        "List" =  $ListName
        "Title" = $NewViewName
        "Paged" = $View.Paged
        "Personal" = $View.PersonalView
        "Query" = $View.ViewQuery
        "RowLimit" = $View.RowLimit
        "SetAsDefault" = $View.DefaultView 
        "Fields" = @($View.ViewFields)
        "ViewType" = $View.ViewType
        "Aggregations" = $View.Aggregations
    }
    #Create a New View
    Add-PnPView @ViewProperties
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

通过这些简单的步骤,您可以使用用户界面或 PowerShell 轻松创建现有视图的副本,并对其进行自定义以满足您的需求。这是有关 SharePoint 本地 PowerShell 副本列表视图的另一篇文章:SharePoint 2013:PowerShell 复制视图

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

取消回复欢迎 发表评论:

关灯