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

[玩转系统] SharePoint Online:用于创建列表视图的 PowerShell

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

SharePoint Online:用于创建列表视图的 PowerShell


SharePoint Online 视图有助于将列组织在一起,并通过排序、分组和筛选功能来呈现数据。视图可以是私有视图或公共视图 - 公共视图可供所有用户使用,而私有视图仅可供创建它的用户使用。当您创建列表或库时,它带有默认视图,例如具有特定列的“所有项目”。您可以创建任意数量的视图来支持列表或库中的不同用例。

如何在 SharePoint Online 中创建视图?

列表视图使您能够对 SharePoint 列表中的数据进行排序、筛选、分组和格式化,以便您可以轻松访问它。要创建新视图,请执行以下操作:

  1. 导航到要在 SharePoint Online 中创建新视图的列表。
  2. 在“列表”选项卡下,单击功能区中的“创建视图”按钮。在下一页上,选择视图类型,例如“标准视图”、“数据表视图”、“甘特图视图”等。

    [玩转系统] SharePoint Online:用于创建列表视图的 PowerShell

  3. 为您的视图提供名称,选择要在视图中显示的字段,设置排序和筛选选项,然后单击“确定”在 SharePoint Online 列表中创建视图。

    [玩转系统] SharePoint Online:用于创建列表视图的 PowerShell

除非确实需要,否则不要编辑默认视图!相反,您可以根据您的要求创建一个新的!

SharePoint Online:如何在现代网站中创建新视图?

在 SharePoint 现代体验中创建新视图变得更加简单!

  1. 将过滤器应用到视图,添加或删除列,根据需要进行排序和分组。

    [玩转系统] SharePoint Online:用于创建列表视图的 PowerShell

  2. 视图准备就绪后,单击“视图”下拉列表,然后单击“视图另存为”,将当前更改保存为“视图”。

    [玩转系统] SharePoint Online:用于创建列表视图的 PowerShell

SharePoint Online:用于创建列表视图的 PowerShell

您是否厌倦了在 SharePoint Online 中手动创建视图?需要在 SharePoint Online 中创建视图,但不想通过 Web 界面执行此操作?或者您可能需要根据无法通过 UI 轻松访问的特定标准创建视图?如果是这样,PowerShell 就是你的朋友。我将向您展示如何使用 PowerShell 在 SharePoint Online 中创建视图。我还将向您展示如何向您的视图添加过滤器和列!

以下是如何在 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"

#Custom function to Create List view in SharePoint Online
Function Create-ListView()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ViewName,
        [Parameter(Mandatory=$true)] [String[]] $ViewFields,
        [Parameter(Mandatory=$true)] [string] $viewQuery,
        [Parameter(Mandatory=$false)] [string] $ItemLimit = "30",
        [Parameter(Mandatory=$false)] [string] $IsDefaultView="$True"
    )

    Try {
        $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)
        $Ctx.Load($List)
        $Ctx.ExecuteQuery()

        #Check if the View exists in list already
        $ViewColl=$List.Views
        $Ctx.Load($ViewColl)
        $Ctx.ExecuteQuery()
        $NewView = $ViewColl | where { ($_.Title -eq $ViewName) }
        if($NewView -ne $NULL)  
        {
            Write-host "View '$ViewName' already exists in the List!" -f Yellow
        }
        else
        {
            $ViewCreationInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
            $ViewCreationInfo.Title = $ViewName
            $ViewCreationInfo.Query = $ViewQuery
            $ViewCreationInfo.RowLimit = $ItemLimit
            $ViewCreationInfo.ViewFields = $Viewfields 
            $ViewCreationInfo.SetAsDefaultView = $IsDefaultView

            #sharepoint online powershell create view
            $NewView =$List.Views.Add($ViewCreationInfo)
            $Ctx.ExecuteQuery()    
            
            Write-host "New View Added to the List Successfully!" -ForegroundColor Green  
        }
    }
    Catch {
        write-host -f Red "Error Adding View to List!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ViewName="ActiveProjects"
$ViewFields=@("Title","IsActive","Priority") # Comma separated - Internal Names
$ViewQuery="<OrderBy><FieldRef Name='Title' /></OrderBy><Where><Eq><FieldRef Name='IsActive' /><Value Type='Boolean'>1</Value></Eq></Where>"

#Call the function to Create View in the list
Create-ListView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName -ViewFields $ViewFields -ViewQuery $ViewQuery

SharePoint Online PnP PowerShell 创建列表视图

PnP PowerShell 提供了在 SharePoint Online 中轻松创建视图的功能。当您需要快速轻松地获取数据的特定视图时,这会很有帮助。以下是用于在 SharePoint Online 中创建视图的 PnP PowerShell:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"
$ViewFields = @("ProjectID", "Title","Category","ProjectStatus")
$Query = "<Where><Eq><FieldRef Name = 'ProjectStatus' /><Value Type = 'Choice'>Active</Value></Eq></Where>"

#Get Credentials to connect
$Cred = Get-Credential

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

    #sharepoint online pnp powershell create view
    Add-PnPView -List $ListName -Title $ViewName -ViewType Html -Fields $ViewFields -Query $Query -ErrorAction Stop
    Write-host "View '$ViewName' Created Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

此外,Add-PnPView cmdlet 支持 -RowLimit、-SetAsDefault 开关。您还可以使用查询参数来设置排序或分组依据。例如。,

  • 排序顺序:“ ”
  • 分组依据:“”

要使用 PowerShell 更新现有列表视图,请参阅:PowerShell 更新 SharePoint Online 中的列表视图

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

取消回复欢迎 发表评论:

关灯