Using New-WebServiceProxy to get, modify, and add items to a list in SharePoint 2007

PowerShell Team

The motivation

Let’s say that you want to retrieve and modify a list in a SharePoint site, but you don’t have access to Microsoft.SharePoint.dll. One possible solution is to try to search online or ask someone to let you copy the dll. If you are able to get a hold of it, you will quickly realize that the Microsoft.SharePoint.dll has some dependencies and that you are stuck again. Well, I will show you how you can get, modify and add items to a list in SharePoint using PowerShell V2.

Pre requisites: First, you need a SharePoint site and credentials to modify that particular list. The illustration below shows the list (DemoList) that I want to modify.

Image 4606 DemoSite1 thumb 1

Get List Items

The New-WebServiceProxy Cmdlet creates a web service proxy object which lets you use and manage the web service.

# The uri refers to the path of the service description, e.g. the .asmx page            
$uri = "http://powershell/TeamSite/TestTeamSite/Demos/_vti_bin/lists.asmx?WSDL"             
            
# Create the service            
$service = New-WebServiceProxy -Uri $uri  -Namespace SpWs  -UseDefaultCredential            
            
# The name of the list             
$listName = "My Demo List"             
            
# Create xml query to retrieve list.             
$xmlDoc = new-object System.Xml.XmlDocument            
$query = $xmlDoc.CreateElement("Query")            
$viewFields = $xmlDoc.CreateElement("ViewFields")            
$queryOptions = $xmlDoc.CreateElement("QueryOptions")            
$query.set_InnerXml("FieldRef Name='Full Name'")             
$rowLimit = "10"            
            
$list = $null             
$service = $null              
            
try{            
    $service = New-WebServiceProxy -Uri $uri  -Namespace SpWs  -UseDefaultCredential              
}            
catch{             
    Write-Error $_ -ErrorAction:'SilentlyContinue'             
}

Now, we use the service object to retrieve the list.

if($service -ne $null){            
    try{                    
        $list = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $queryOptions, "")             
    }            
    catch{             
        Write-Error $_ -ErrorAction:'SilentlyContinue'            
    }            
}

Let’s take a look at the $list object.

$list

s           : uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882

dt          : uuid:C2F41010-65B3-11d1-A29F-00AA00C14882

rs          : urn:schemas-microsoft-com:rowset

z           : #RowsetSchema

#whitespace : {

,

}

data        : data

 

The items of the DemoList are located in $list.data.row.

$list.data.row

ows_ContentTypeId       : 0x0100F20992473D85F74DBAFD4159A55805FB

ows_Title               : Francisco

ows_Car                 : Impreza

ows_IceCream            : Chocolate

ows_Sport               : Basketball

ows_ID                  : 1

ows_ContentType         : Item

ows_Modified            : 2010-06-24 14:58:17

… (more data) …

For more information on getting list items visit http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx

Update List Items

We want to modify the sport field value from “basketball” to “soccer”. to do this, we use the $service and $list from the example above.

# Get name attribute values (guids) for list and view            
$ndlistview = $service.getlistandview($listname, "")            
$strlistid = $ndlistview.childnodes.item(0).name            
$strviewid = $ndlistview.childnodes.item(1).name            
            
                
# Create an xmldocument object and construct a batch element and its attributes.             
$xmldoc = new-object system.xml.xmldocument             
            
# note that an empty viewname parameter causes the method to use the default view               
$batchelement = $xmldoc.createelement("batch")            
$batchelement.setattribute("onerror", "continue")            
$batchelement.setattribute("listversion", "1")            
$batchelement.setattribute("viewname", $strviewid)            
            
            
# Specify methods for the batch post using caml. to update or delete, specify the id of the item,             
# and to update or add, specify the value to place in the specified column            
$id = 1            
$xml = ""            
            
# The row to be modified            
$rowId = $list.data.row.ows_id            
            
# New field value            
$newsport ="soccer"               
            
$xml += <method id='$id' cmd='Update'>" +            
        "<field name='ID'>$rowId</field>" +            
        "<field name='Sport'>$newsport</field>" +            
        "</method>"            
                    
# Set the xml content                    
$batchelement.innerxml = $xml            
            
$ndreturn = $null             
try {            
    $ndreturn = $service.updatelistitems($listname, $batchelement)             
}            
catch {             
    write-error $_ -erroraction:'SilentlyContinue'            
}

For more information on updating list items visit http://msdn.microsoft.com/en-us/library/ms440289.aspx.

Add New Items to the List

To add a new row to the list, everything is the same as in the update list items example above, the only difference is the xml content.

In this case, the new row will contain the following information:

Title: James

Car: A4

Sport: Swimming

Icecream: Strawberry Cheese Cake

$xml = "<method id='$id' cmd='New'>" +            
        "<field name='Title'>James</field>"+            
        "<field name='Car'>A4</field>"+            
        "<field name='Sport'>Swimming</field>" +            
        "<field name='IceCream'>Strawberry cheese Cake</field>" +            
        "</method>""

At the end, our SharePoint list looks like this.

Image 1145 DemoSite2 thumb

Cheers,

Francisco Gomez Gamino [MSFT]

0 comments

Discussion is closed.

Feedback usabilla icon