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

[玩转系统] SharePoint Online:使用 PowerShell 设置网站栏属性

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

SharePoint Online:使用 PowerShell 设置网站栏属性


要求:使用 PowerShell 更新 SharePoint Online 中网站列的属性。

如何在 SharePoint Online 中编辑网站栏?

在这篇博文中,我们将向您展示如何使用 SharePoint Online 中的 PowerShell 更新网站栏的设置。如果您需要在创建网站栏后对其设置进行更改,或者如果您想要自动执行新网站栏的设置过程,这会很有用。让我们开始吧!

您可以按照以下步骤在 SharePoint Online 中编辑网站栏属性,例如名称、组、栏类型等:

  1. 导航到创建网站栏的 SharePoint Online 网站。
  2. 单击设置齿轮>>站点设置
  3. 在“网站设置”页面上,单击“Web 设计师画廊”组下的“网站栏”链接
  4. 在网站栏页面,单击要编辑的网站栏名称。
  5. 在“编辑网站栏”页面上,您可以进行必要的更改,并设置是否更新基于此网站栏的所有列表栏以反映您的更改。
  6. 单击“确定”按钮保存更改。

    [玩转系统] SharePoint Online:使用 PowerShell 设置网站栏属性

如果网站栏名称不可单击,则可能会在父网站中创建网站栏。导航到创建网站栏的“源列”所示的父网站,然后您可以编辑网站栏。

SharePoint Online:使用 PowerShell CSOM 设置网站栏属性

以下是用于更新 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-SPOSiteColumn($SiteURL,$ColumnName)
{
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred

        #Check if the site column exists
        $Fields = $Ctx.web.Fields
        $Ctx.Load($Fields)
        $Ctx.ExecuteQuery()
        $FieldNames = $Fields | Select -ExpandProperty InternalName

        #Get the site column from sharepoint online site
        If($FieldNames.Contains($ColumnName))
        {
            #Get the site column
            $SiteColumn = $Fields.GetByInternalNameOrTitle($ColumnName)

            #Update Field Properties
            $Sitecolumn.Required = $True
            $Sitecolumn.Description="Current Status of the Project"
            $SiteColumn.Update()
            #Use: $siteColumn.UpdateAndPushChanges() to push changes to all lists

            $Ctx.ExecuteQuery()
            Write-host -f Green "Site Column Updated Successfully!"
        }
        else
        {
            Write-host -f Yellow "Site Column doesn't Exist!"
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Call the Function with Site URL and Internal Name of the Field
Set-SPOSiteColumn -SiteURL "https://Crescent.sharepoint.com" -ColumnName "ProjectName"

使用 PnP PowerShell 更新网站列属性

要更新网站栏的设置,请使用此 PowerShell:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$FieldName = "ProjectStatus" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Set Site Column Properties
    Set-PnPField -Identity $FieldName -Values @{Description="Current Status of the Project";Group="Crescent Site Columns V2"}    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

我们还有一个 -UpdateExistingLists 开关来更新所有现有列表中的网站栏。

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

取消回复欢迎 发表评论:

关灯