2

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"
2
  • 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. Commented Apr 2, 2014 at 15:50
  • @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? Commented Apr 2, 2014 at 21:14

1 Answer 1

2

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 {}
 } 
Sign up to request clarification or add additional context in comments.

2 Comments

This did the trick. The reason for a datatable is because each item on the list has a lot of data, so I want to limit what I am parsing to only what I need.
You code as it stands actually goes through each individual list item and converts to a datarow and adds those rows to a datatable. You're then selecting from the datatable. If you really want to improve performance AND limit the columns you need, leverage a CAML query with the $list.GetItems() method. You don't need the DataTable. In fact, it's working against you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.