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

[玩转系统] 使用 PowerShell 更新 SharePoint 列表中的计算列公式

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

使用 PowerShell 更新 SharePoint 列表中的计算列公式


要求:更新整个网站集中列表的特定列的计算字段的公式。

PowerShell 更新 SharePoint 中的计算字段公式:

这将更新网站集中所有子网站的网站列公式。


Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Variables
$SiteURL = "https://intranet.crescent.com"
$ColumnName="Productivity"

#Iterate through each web in the given site collection
$Webs = Get-SPSite $SiteURL | Get-SPWeb -Limit All | ForEach-Object {
    
    #Check if the column exists
    if($_.Fields.ContainsField($ColumnName))
    {
        $column = $web.Fields[$ColumnName]
        #Update calculated field formula
        $Column.Formula = "=(([PlannedEffort]/5)/ActualEffort)*8"
        $Column.update($true)
        write-host "Updated Calculated Field formula at $($_.URL)"
    }
}

相同的代码也可用于列表级别公式。

PowerShell 更新 SharePoint 列表中的计算列公式:

在另一种情况下,我们必须替换所有站点的计算列公式中的字段。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteURL = "https://intranet.crescent.com"
$ListName="Projects"
$ColumnName="Productivity"
$OldField="PlannedEffort"
$NewField="EstimatedHours"

#Iterate through each web in given site collection
$Webs = Get-SPSite $SiteURL | Get-SPWeb -Limit All | ForEach-Object {
    
    #Get the List
    $List = $_.Lists.TryGetList($ListName)

    If($List)
    {
        #Check if the column exists
        if($List.Fields.ContainsField($ColumnName))
        {
            $Column = $List.Fields[$ColumnName]
            #Update calculated field formula
            $Column.Formula = $Column.Formula.Replace($OldField,$NewField)
            $Column.update($true)
            write-host "Updated Calculated Field formula at $($_.URL)"
        }
    }
} 

[玩转系统] 使用 PowerShell 更新 SharePoint 列表中的计算列公式

另一篇文章中解释了如何使用 PowerShell 创建计算字段:如何使用 PowerShell 将计算列添加到 SharePoint 列表?

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

取消回复欢迎 发表评论:

关灯