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

[玩转系统] SharePoint Online:使用 PowerShell 设置默认视图

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

SharePoint Online:使用 PowerShell 设置默认视图


要求:更改 SharePoint Online 中的默认视图。

如何更改 SharePoint Online 中的默认视图?

想要将特定视图设为所有用户的默认视图吗?在许多情况下,您需要将特定视图设为 SharePoint Online 中列表或库的默认视图。在这篇文章中,我们将逐步介绍为列表或文档库设置默认视图所需的步骤。

要将自定义列表视图设置为 SharePoint Online 中的默认视图,请执行以下步骤:

  1. 转到列表设置>>向下滚动到底部,然后在“视图”部分下单击要设置为默认视图的视图名称。
  2. 在“编辑视图”页面中,选中“将此设为默认视图”旁边的框,然后单击“确定”保存更改。

    [玩转系统] SharePoint Online:使用 PowerShell 设置默认视图

因此,下次当您点击:https://Crescent.sharepoint.com/Lists/Projects/ 时,您将进入默认视图“活动项目”,网址为:https://Crescent.sharepoint.com/Lists/项目/Active%20Projects.aspx

同样,对于现代列表和库,您可以从视图下拉列表中设置默认视图:

[玩转系统] SharePoint Online:使用 PowerShell 设置默认视图

SharePoint Online:使用 PowerShell 设置默认视图

以下是用于更改 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 Set-SPODefaultView()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ViewName
    )
    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)
        
        #Get the list view by title
        $View=$List.Views.GetByTitle($ViewName)

        #Set the view as default view
        $View.DefaultView=$True
        $View.Update()
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "List View '$ViewName' is Set as the Default View!" 
    }
    Catch {
        write-host -f Red "Error Setting Default View!" $_.Exception.Message
    }
}

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

#Call the function 
Set-SPODefaultView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName

我们还有 MobileView、MobileDefaultView 属性来启用移动视图并为移动设备创建默认视图!

PnP PowerShell 将列表视图设置为默认视图:

以下是如何在 SharePoint Online 中设置默认视图:


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

#Get Credentials to connect
$Cred = Get-Credential

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

    #Get the Context
    $Context = Get-PnPContext

    #Get the List View from the list
    $ListView  =  Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop

    #Set View as Default View
    $ListView.DefaultView = $True
    $ListView.Update()
    $Context.ExecuteQuery()
    Write-host -f Green "View '$ViewName' Set as Default View of the List!"
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

您还可以使用:Set-PnPView 将视图设置为默认:


Connect-PnPOnline -Url $SiteURL -Interactive

#Set the List View as Default view
Set-PnPView -List $ListName -Identity $ViewName -Values @{DefaultView=$True}

使用 Set-PnPView cmdlet,您可以更新列表视图的属性,例如:标题、隐藏、JSLink、RowLimit、ViewQuery 等。

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

取消回复欢迎 发表评论:

关灯