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

[玩转系统] 如何使用 PowerShell 更新 SharePoint 中的列表项?

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

如何使用 PowerShell 更新 SharePoint 中的列表项?


要求:使用 PowerShell 更新 SharePoint 中的列表项

[玩转系统] 如何使用 PowerShell 更新 SharePoint 中的列表项?

如何使用 PowerShell 脚本更新 SharePoint 中的列表项?

更新 SharePoint 中的列表项可能是一个乏味的过程,尤其是如果您每天必须手动执行多次的话。你讨厌一遍又一遍地做同样的事情,不是吗?好吧,PowerShell 来拯救你了!这篇博文将向您展示如何使用 PowerShell 快速更新列表项。让我们开始吧!

更新列表项的基本语法如下:


#Get Web and List 
$Web = Get-SPWeb "https://sharepointsite.com"
$List = $Web.Lists["ListName"]

#Get the List item to update
$ListItem = $List.GetItembyID($ItemID)
 
#Set Column values
$ListItem["ColumnName"] = "New Value for the Field!"
 
#Update the Item, so that it gets saved to the list
$ListItem.Update()

使用 PowerShell 脚本更新 SharePoint 列表项

我们再举一个例子:


Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

$web = Get-SPWeb https://SharePointSite.com

#Get the Contacts List
$List = $web.Lists["Contacts"]

#Get the List Item by its Title field value
$ListItem = $List.Items | Where {$_["Title] -eq "Salaudeen Rajack"}

#Set "Department" column value
$ListItem["Department"] = "IT"

$ListItem.Update() 

使用 PowerShell 脚本更新所有列表项:


$web = Get-SPWeb https://SharePointSite.com

#Get the Contacts List
$List = $web.Lists["Contacts"]

#Get All Items by update its title
$ListItems = $List.items
 
#Iterate through each item
foreach($Item in $ListItems)
{
    #Update the value of the "Title" column
    $item["Title"] = "Title Updated!"
 
    #Update the item
    $item.Update()
}

批量更新选择性列表项怎么样?

让我们通过 id 字段更新列表项。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteURL = "https://portal.crescent.com/pipeline/"
$ListName = "Deals"

#Get the List
$List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)

#List of Item IDs to update
$ItemIDs = 16,52,69,70,97,170,194,195,196,220,259

If($list)
{
    foreach($ItemID in $ItemIDs)
    {
        #Get Item by ID
        $ListItem = $List.GetItembyID($ItemID) 

        #Update List Item Fields
        $ListItem["Health and SafetyRisk"]="Low"
        $ListItem["Environmental Risk"]="Medium"
        $ListItem["Social Risk"]="High"       

        $ListItem.SystemUpdate()
        Write-host "Updated Item ID:"$ItemID
    }
}

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

取消回复欢迎 发表评论:

关灯