Get Data From A SharePoint 2007 List Using PowerShell

Leave a comment
Share

I recently needed to get some data from a SharePoint 2007 List from a PowerShell script.  I assumed that Microsoft would have made it easy, but I have to admit that I found it rather difficult.  Most articles I’d read only got me a response from the server that said SERVER_ERROR , with no further explanation, and very few search results to help me.  Anyway, I ended up with the code below, which I hope will help someone.  It may even work for later versions of SharePoint, too.

$strUri = "http://yourservername/sites/sitename/_vti_bin/lists.asmx?WSDL"
$objService = New-WebServiceProxy -Uri $strUri -UseDefaultCredential
$strListName = "Your List Name"
$xmlDoc = New-Object System.XML.XmlDocument
$objQuery = $xmlDoc.CreateElement("Query")
$objViewFields = $xmlDoc.CreateElement("ViewFields")
$objQueryOptions = $xmlDoc.CreateElement("QueryOptions")
$strQueryText = @"
<Where>
  <Eq><FieldRef Name='YourFieldName' /><Value Type='Text'>YourSearchText</Value></Eq>
</Where>
"@
$objQuery.InnerXML = $strQueryText
$objList = $objService.GetListItems($strListName, "", $objQuery, $objViewFields, "", $objQueryOptions, "")
# Next line just dumps the data to the console
# You'll need to find the correct child node yourself
$objList.data.row




Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>