ARTICLE

Export List Items Using SharePoint 2010 Web Service in Powershell

Posted by Vijai Anand Articles | SharePoint 2010 January 05, 2012
In this article you will see how to get all the items from a particular list using SharePoint 2010 web service in PowerShell.
Reader Level:

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.
 

Login to add your contents and source code to this article
post comment
     

If I leave the $viewName empty, the default view is not used. In fact, if I put in the guid, I don't get the expected columns. Any suggestions?

Posted by Shirley Feb 15, 2012

Nevermind. I figured it out.

Posted by Brenda Hartson Jan 29, 2012

PS C:\exports> c:\exports\exporttestbh.ps1 Please pass the credentials that have access to the site: https://lbrmain.lbrealty.com/intranet cmdlet Get-Credential at command pipeline position 1 Supply values for the following parameters: Credential Exception calling "GetListItems" with "7" argument(s): "Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerExcepti on' was thrown." At C:\exports\exporttestbh.ps1:41 char:82 + [System.Xml.XmlNode]$nodeListItems =$listsWebServiceReference.GetListItems <<<< ($listName, $viewName, $query, $vie wFields, $rowLimit, $queryOptions, $null) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException New-Object : Exception calling ".ctor" with "2" argument(s): "Access to the path 'c:\exports\ArmedForcesMembers.xml' is deni ed." At C:\exports\exporttestbh.ps1:42 char:28 + $output = New-Object <<<< -TypeName System.IO.StreamWriter -ArgumentList $outputXmlFilePath, $false + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand You cannot call a method on a null-valued expression. At C:\exports\exporttestbh.ps1:43 char:25 + $output.WriteLine <<<< ($nodeListItems.Outerxml) + CategoryInfo : InvalidOperation: (WriteLine:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At C:\exports\exporttestbh.ps1:44 char:25 + $output.WriteLine <<<< () + CategoryInfo : InvalidOperation: (WriteLine:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At C:\exports\exporttestbh.ps1:45 char:23 + $output.Dispose <<<< () + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull Output file is generated in the path: c:\exports\ArmedForcesMembers.xml PS C:\exports>

Posted by Brenda Hartson Jan 27, 2012

Thanks

Posted by Arjun Panwar Jan 07, 2012

As a big sharepoint fan i really like this article and appreciate your efforts

Posted by Akash Ahlawat Jan 06, 2012
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.