2
votes

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"
1
DataRows (and DataTables) have BeginEdit() and EndEdit() methods, that I think you need to use instead of Update(). Unfortunately, the sharepoint environment I have is currently broken, so I can't test to see if that'll behave better.Hunter Eidson
@HunterEidson I added to my IF statement: "$item.BeginEdit()" and "$item.EndEdit()" with $item.Failure_x0020_Step = "Failure Test" between. It does not error out, but nothing is updated in the list. Is there another way to use those methods?user3208164

1 Answers

2
votes

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 {}
 }