Modify Sharepoint list items remotely using Powershell

I had a post on how to connect to a Sharepoint list using Powershell and webservice. So here is the new post on how to manipulate the data remotely can be found in this list.

If you are using PowerShell v1 you need the Lists.dll in your hand, and in PowerShell v2 you need a new-webserviceproxy to have an instance (list object).

So I follow the Powershell v1 possibility. You need to import the lists.dll first

[void][Reflection.Assembly]::LoadFrom("Lists.dll")
$lista = New-Object Lists
$lista.Credentials=[System.Net.CredentialCache]::DefaultCredentials

Let’s get the list items to a local variable:


$lista.url = "http://intranet/_vti_bin/lists.asmx"
$items = $lista.GetListItems("Name of the list", $null, $null, $null, $null, $null, $null)

What’s going here? What are these $null parameters, heh?
We need to ask for a hand from a CAMEL 🙂 so in the parameter list you can use CAML query to filter the query result.

So now we have all the items from our list. Let’s have a look at all the items by running this command:

$items.data.row | foreach {$_.ows_Title}

And one more Upps here… what’s that “ows_Title” grrr.. oh yeah you have to use such a reference on a clumn name. So for example if you have a column named “Asterix” you have to use $_.ows_Asterix as a reference.
But what if we have space in the name of a column? You have to have the next refernce: ows_Some_x0020_Thing Cool, isn’t it? 🙂 We can use the hexacodes for the given character with an underscore before and after it. LIKE.
OK, you can list all the columns:

$items.data.row

Let’s get back to the point.. how to manipulate a list item? Let’s do it with the item which has an ID=5.
First we need to create a query string:


[xml]$query = "5Laptop 2"

The bolded string is the filter. It will tell the sharepoint which item should be modified.
Then make the Sharepoint eat this update with the following code:


$lista.UpdateListItems("Name of the list", $query)

Ok. Let’s have a query which provides all the rows we would like to modify:


[xml]$query = "Pending"
$items = $lista.getlistitems("Name of the list", $null, $query, $null, $null, $null, $null)

Reference

http://www.u2u.info/Blogs/karine/Lists/Posts/Post.aspx?ID=23 -Karine Bosch

Advertisements

1 thought on “Modify Sharepoint list items remotely using Powershell”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s