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

[玩转系统] SharePoint Online:使用 PowerShell 重命名列表中的列

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

SharePoint Online:使用 PowerShell 重命名列表中的列


当我们在 SharePoint Online 中创建新列表或库时,会自动创建“标题”列。 SharePoint 不允许我们删除此标题列。但是,我们可以将“标题”列名称更改为相关的名称,例如“项目”列表中的“项目名称”。

如何重命名 SharePoint Online 中的列表列?

您是否曾经需要重命名 SharePoint Online 列表中的列?也许列的名称与正在存储的数据不匹配,或者您想让它对于将要使用该列表的人来说更加用户友好。在这篇博文中,我们将引导您完成重命名 SharePoint Online 列表中的字段的步骤。我们还将向您展示如何使用 PowerShell 重命名 SharePoint Online 列表中的列。

以下是在 SharePoint Online 中重命名列表列的方法:

  1. 导航到目标 SharePoint Online 列表 >> 转到列表设置页面。
  2. 选择任意列,例如“列”部分中的“标题”。您将看到“编辑列”页面。
  3. 现在您可以在“列名称”处重命名该字段,然后单击“确定”按钮保存更改。

    [玩转系统] SharePoint Online:使用 PowerShell 重命名列表中的列

重命名 SharePoint Online 中的字段 现代体验:

在现代用户界面中重命名列比经典用户界面简单得多。以下是在现代 UI 中更改字段名称的方法:

  1. 将鼠标悬停在字段名称上,然后单击小下拉菜单以获取其上下文菜单。
  2. 在上下文菜单中,选择“列设置”>>,然后选择“编辑”。

    [玩转系统] SharePoint Online:使用 PowerShell 重命名列表中的列

  3. 在“编辑列”窗格中,设置该列的“名称”字段,然后单击底部的“保存”按钮以保存更改。

    [玩转系统] SharePoint Online:使用 PowerShell 重命名列表中的列

如果在“列设置”中找不到“编辑”选项怎么办?

编辑选项可能不适用于所有列。那么,如何重命名ID、附件等列呢?好吧,这就是窍门!

  1. 导航到您的列表或库设置页面。
  2. 单击任何可用的列即可进入列设置。例如,“标题”列。 URL 应如下所示:https://YourDomain.sharepoint.com/sites/YourSite/_layouts/15/FldEdit.aspx?List=ListGUID&Field=Title。现在,将此 URL 中的“Field”参数替换为您要重命名的字段。说:“附件”列:https://Crescent.sharepoint.com/sites/Retail/_layouts/15/FldEdit.aspx?List=%7B8791A814%2D94A7%2D42AF%2DB0BF%2D8CD8037B554F%7D&Field=Attachments
  3. 输入附件列的新名称,然后单击“确定”。

这是重命名的附件列的实际效果:

[玩转系统] SharePoint Online:使用 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"

Function Rename-ListColumn()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ColumnName,
        [Parameter(Mandatory=$true)] [String[]] $NewName
    )

    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()

        #Get the column to rename
        $Field = $List.Fields.GetByInternalNameOrTitle($ColumnName)
        $Field.Title = $NewName
        $Field.Update()
        $Ctx.ExecuteQuery()
            
        Write-Host "Column Name Changed successfully!" -ForegroundColor Green

    }
    Catch {
        write-host -f Red "Error Renaming Column!" $_.Exception.Message
    } 
}

#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ColumnName="Titles"
$NewName="Project Title"

#Call the function to rename column
Rename-ListColumn -SiteURL $SiteURL -ListName $ListName -ColumnName $ColumnName -NewName $NewName

PnP PowerShell 更新 SharePoint Online 中的字段标题和说明:

让我们看看如何使用 PnP PowerShell 重命名 SharePoint Online 列表中的列。您可以使用 Set-PnPField cmdlet 更改列表中列的名称。


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

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #Set Field Title and Description
    Set-PnPField -List $ListName -Identity $FieldName -Values @{Title="Status";Description="Status of the Project"} -ErrorAction Stop
    Write-host -f Green "Title and Description Updated for Field '$FieldName'"
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

请注意,您无法使用任何这些方法重命名 SharePoint 文档库中的名称列!其他文章中介绍了从 SharePoint UI 重命名列并使用 PowerShell 和 C# 以编程方式重命名列:

  • 如何重命名 SharePoint 列表中的列?
  • 使用 PowerShell 和 C# 以编程方式重命名 SharePoint 列表/网站列

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

取消回复欢迎 发表评论:

关灯