FredBainbridge

Get SharePoint list items using Get-WebService

This is an example of how to get SharePoint list items using Get-WebService. This does not require any SharePoint modules or special dlls to be loaded. This particular example shows how to get back a particular set of items. You pass in a Query Column and Query Value and a CAML query is created. i.e. QueryColumn = Title and QueryValue = Item1. All matching items are returned. This is a simple example, you can get way more precise if needed.

BEGIN

<#
.Synopsis
    Get a sharepoint list object
.DESCRIPTION
    Using the name of of a field in a list and a value for that field, this will return all items that match the critiria.  \
    This returned object is XML
    SharePoint is case sensitive!
    Column names returned in the output are ows_[name], but when you pass them in they are just [name]
.PARAMETER URI
    The URL to the Sharepoint WSDL
.PARAMETER SharePointListName
    This name of the SharePoint list you are querying
.PARAMTER ColumnNames
    The array should contains the name of columns you want returned in your dataset from the SharePoint list.  
.PARAMETER QueryColumn
    This is column of the list you are querying with
.PARAMETER QueryValue
    This is the value of the column you querying with 
.EXAMPLE
    Get-SPList -URI "http://sharepointsite/sitename/_vti_bin/Lists.asmx?wsdl -SharePointListName "MySharePointList" -ColumnName "ID","Title","Vendor" -QueryColumn "Color" -QueryValue "Red"
#>
function Get-SPList
{
    
    [CmdletBinding()]
    [OutputType([Object[]])]
    Param
    (
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$false,
                   Position=0)]
        [string] $URI = "http://sharepointsite/sitename/_vti_bin/Lists.asmx?wsdl",
        [string] $SharePointListName = "List Name",
        [string[]] $ColumnNames = [string[]]("ID","Title","Vendor"),  #examples
        [string] $QueryColumn = "FieldName",
        [string] $QueryValue = "FieldValue"
    )

    Begin
    {
        #Maybe put some logging in here
    }
    Process
    {
        #Prepare the ViewFieldsXML
        $ColumnNames | ForEach-Object {
            $ViewFieldsXML = $ViewFieldsXML + ""
        }
        #get sharepoint web service connection
        $service = New-WebServiceProxy -Uri $URI  -Namespace SpWs -UseDefaultCredential
        
        #the status field is ows_Demand_x0020_Status
        #This sets up the XML to use the webservice.
        $xmlDoc = new-object System.Xml.XmlDocument            
        $query = $xmlDoc.CreateElement("Query")            
        $viewFields = $xmlDoc.CreateElement("ViewFields") 

        #define your view fields with the name of SharePoint columns.  
        $viewFields.set_InnerXML($ViewFieldsXML)      
        $queryOptions = $xmlDoc.CreateElement("QueryOptions")            
        $query.set_InnerXml("$QueryValue")             
        $rowLimit = "100"
        
        $ListItems = $service.GetListItems($SharePointListName, "", $query, $viewfields, $rowLimit, $queryOptions, "") 
        
        Write-Output $ListItems.Data.Row
    }
    End
    {
        #some logging here
    }
}

Share