PowerShell: SharePoint - Consume SharePoint Web Service

Updated on 2/7/2015

The New-WebServiceProxy cmdlet is able to take a web service’s WSDL URL, and return an object that is able to execute the web service operations and queries. One thing to be taken into consideration when querying SharePoint web services are the credentials that need to be used. If the credentials of the person running the Windows PowerShell commands have permissions to the service, then the –UseDefaultCredential flag can be used when creating the web service proxy PowerShell object. If the script needs to specify credentials, this can be done by creating a credentials object. Alternatively, the script could query the user for credentials each time the script is run using the Get-Credentials cmdlet.

#create credentials
$username = "dmg\user1"
$password = ConvertTo-SecureString -String 'password' -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($username, $password)

#create web service proxy
$uri = "http://sharepoint:8080/_vti_bin/UserProfileChangeService.asmx"
$serviceProxy = New-WebServiceProxy -Uri $uri -Namespace UserProfileChangeService -Class Changes -Credential $credentials 

#query web service and output
$changes = $serviceProxy.GetAllChanges()
$changes.Changes | ft ID, UserAccountName, ChangeType, EventTime

Output


Id UserAccountName  ChangeType EventTime                        
-- ---------------  ---------- ---------                        
1  dmg\user1        Add        2/3/2014 9:05:39 PM              
2  dmg\user1        Metadata   2/3/2014 9:05:39 PM              
3  dmg\user1        Add        2/3/2014 9:05:39 PM