PowerShell to get all items in a SharePoint 2007 list via Web Services/SOAP

I wanted to get a list’s contents in a SharePoint 2007 site via PowerShell. I ran into only one issue – how to handle the pagination. When creating the Xml to include the next page, I was running into formatting issues because the text contained a “=”. This link suggested that I create the XML element first then add the innerText after. Worked after that!

TheBelow is my script to get the contents of a SharePoint 2007 list.

 

$listName = "List Name"
$xmlDoc = new-object System.Xml.XmlDocument
$query = $xmlDoc.CreateElement("Query")
$viewFields = $xmlDoc.CreateElement("ViewFields")
$queryOptions = $xmlDoc.CreateElement("QueryOptions")
$rowLimit = "50"
$service = New-WebServiceProxy -UseDefaultCredential -uri http://sharepoint2007.comapny.com/_vti_bin/lists.asmx?WSDL
$nextPage=$true
while($nextPage){
$list = $service.GetListItems($listName, "", $query, $viewFields, $rowLimit, $queryOptions, "")
$list.data.row | select ows_ID,ows_Created,ows_Title
if ($list.data.ListItemCollectionPositionNext){
$nextPage=@"
<Paging ListItemCollectionPositionNext="" />
"@
$queryOptions.set_InnerXml($nextPage)
$queryOptions.ChildNodes[0].Attributes["ListItemCollectionPositionNext"].InnerText = "$($list.data.ListItemCollectionPositionNext)"
}
else {
write-host "done"
$nextPage=$false
}
}

,

4 Responses to PowerShell to get all items in a SharePoint 2007 list via Web Services/SOAP

  1. Ben May 29, 2012 at 11:14 am #

    Hi, I was looking to use PS to extract data from a SharePoint 2007 list, and this has gotten me fairly close, however, I get the following message, looping after what I assume is the first page of data. I don’t think the pagination is working:-

    Unable to index into an object of type System.Xml.XmlChildNodes.
    At C:\ReadFromSPListViaSOAP.ps1:17 char:26
    + $queryOptions.ChildNodes[ <<<< 0].Attributes["ListItemCollectionPositionNext"].InnerText = "$($li
    st.data.ListItemCollectionPositionNext)"
    + CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

    Can you point me in the right direction with a fix? I've only started manipulation of XML with PS recently Thanks! Ben

  2. jbmurphy May 30, 2012 at 4:20 pm #

    I will have to take a look. I haven’t used that code all that much, I have been working more with 2010 and REST.

  3. Ben May 30, 2012 at 4:47 pm #

    Hi Jeff, thanks. I’ve gotten it working by just ignoring pagination and setting $rowLimit to a number greater than the number of expected records, but I’d still like to get my head around how it would work (this particular script is about 55% practical application 45% learning!).

  4. jbmurphy May 30, 2012 at 4:51 pm #

    Now that you mention it, that is what I did. I just just put the row limit more that my list had.