Export List Items Using SharePoint 2010 Web Service in Powershell


In this article you will see how to get all the items from a particular list using SharePoint 2010 web service in PowerShell.

List Items

I have a list named "List" which contains the following items

Share1.gif

In this you will see how to get all the items from the list "List" using SharePoint 2010 web service in PowerShell.


Steps Involved

  1. Open SharePoint 2010 Management Shell by going to Start | All Programs | SharePoint | Microsoft SharePoint 2010 Products | SharePoint 2010 Management Shell (Run as Administrator).
  2. Run the following script.

PowerShell Script

##==============================================================================
## Automation  : Get all the list items for a particular list using SharePoint 2010 Web Service in PowerShell 
## Note        : Need to modify the input parameters
## Author      : Vijai Anand.R
## Date        : 05-January-2012
##================================================================================

#------------- Input Parameters --------------

## Specify the site URL from where you need to get all the list items
$webURL="http://serverName:46563/sites/MMS"
## Specify the list name from where you need to get the list items
[string]$listName = "List"
## Specify the location where the output xml file GetListItems.xml file has to be generated
$outputXmlFilePath="D:\VijaiPOC\GetListItems.xml"

## $viewName is a string that contains the GUID of the view. If you give empty value it will take the values from default view
[string]$viewName = ""
## $rowLimit is a string that contains number of items to be retreived from the list
[string]$rowLimit = "50"

[String]$viewFieldsValue="<FieldRef Name='Title' />"
[String]$queryValue="<Where><Gt><FieldRef Name='ID'/><Value Type='Number'>3</Value></Gt></Where>"
[String]$queryOptionsValue=""

#--------- Get List Items Function ----------

Function GetListItems()
{
     
Write-Host -ForegroundColor Green "Please pass the credentials that have access to the site: "$webURL
      $credential=Get-Credential
      $uri=$webURL+"/_vti_bin/Lists.asmx?wsdl"
      $listsWebServiceReference = New-WebServiceProxy -Uri $uri -Credential $credential
      [System.Xml.XmlDocument]$xmlDoc=New-Object -TypeName System.Xml.XmlDocument
      [System.Xml.XmlElement]$query = $xmlDoc.CreateElement("Query")
      [
System.Xml.XmlElement]$viewFields =$xmlDoc.CreateElement("ViewFields")
      [
System.Xml.XmlElement]$queryOptions =$xmlDoc.CreateElement("QueryOptions")
     
$viewFields.InnerXml = $viewFieldsValue
      $query.InnerXml = $queryValue
      $queryOptions.InnerXml = $queryOptionsValue
      [System.Xml.XmlNode]$nodeListItems =$listsWebServiceReference.GetListItems($listName, $viewName, $query, $viewFields, $rowLimit, $queryOptions, $null)
     
$output = New-Object -TypeName System.IO.StreamWriter -ArgumentList $outputXmlFilePath, $false
      $output.WriteLine($nodeListItems.Outerxml)
     
$output.WriteLine()
     
$output.Dispose()
     
Write-Host -ForegroundColor Green "Output file is generated in the path: "$outputXmlFilePath

}

#--------- Calling the Function -----------

GetListItems


Output

Output file is generated in the path specified in the $outputXmlFilePath.

Note: You will get only two items because in the query I have given a condition ID>3.

Share2.gif

Summary

Thus in this article you have seen how to get all the items from a particular list using SharePoint 2010 web service.