Reputation: 129
I am trying to update a SharePoint list, because of its size and to make things faster I want to use a DataTable.
$webURL = "http://test/test"
$listName = "Test List"
$web = Get-SPWeb $webURL
$list = $web.Lists[$listName]
$items = $list.items.GetDataTable() | select LinkTitle, Failure_x0020_Step
foreach($item in $items)
{
if(($item.LinkTitle -eq "PC111"))
{
$item.Failure_x0020_Step = "Failure Test"
$item.Update()
}
else {}
}
I get this error when running the script:
Method invocation failed because [System.Data.DataRow] doesn't contain a method named 'Update'. + CategoryInfo : InvalidOperation: (Update:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
I do have it working without the DataTable (changes below), but would prefer the DataTable.
$items = $list.items
if(($item["Computer"] -eq "PC111"))
$item["Failure Step"] = "Failure Test"
Upvotes: 2
Views: 1110
Reputation: 382
When you're calling Update(), that is trying to apply an Update to the DataTable, not the SharePoint list. They are separated. You'll need to retrieve the actual list item from SharePoint and update it.
I'm not sure why a DataTable makes things faster for you but if you want to keep it:
$webURL = "http://test/test"
$listName = "Test List"
$web = Get-SPWeb $webURL
$list = $web.Lists[$listName]
$items = $list.items.GetDataTable() | select LinkTitle, Failure_x0020_Step, ID
foreach($item in $items)
{
if(($item.LinkTitle -eq "PC111"))
{
$realItem = $list.GetItemById($item.ID)
$realItem['Failure_x0020_Step'] = "Failure Test"
$realItem.Update()
}
else {}
}
Upvotes: 2