Interacting with classic Web Services (asmx) in PowerShell

[A versão em Português deste post pode ser encontrada aqui]

Here are some examples of how to call classic Web Services in PowerShell. I’ve made these examples a while back and since then I’ve shared it with many working peers throughout the years:

  1. Simple Web Service call, using the current user’s credential – default credential:
    # Creates the Web Service Proxy
    $ws = New-WebServiceProxy -Uri "http://spserver/_vti_bin/lists.asmx?wsdl" -UseDefaultCredential
    # Calls the web method
    $ws.GetListCollection()
    
  2. Simple Web Service call, using a specific credential:
    $credentials = New-Object System.Management.Automation.PSCredential("domain\myuser", (ConvertTo-SecureString -String "mypassword" -AsPlainText -Force));
    # Creates the Web Service Proxy
    $ws = New-WebServiceProxy -Uri "http://spserver/_vti_bin/lists.asmx?wsdl" -Credential $credentials;
    # Calls the web method
    $ws.GetListCollection();
    
  3. Calling a Web Service in a SharePoint environment configured for Forms Based Authentication (FBA):
    # Creates the Web Service Proxy to the authentication Web Service
    $wsAuth = New-WebServiceProxy -Uri "http://spserver/_vti_bin/authentication.asmx?wsdl" -UseDefaultCredential
    # Creates the Cookie Container in order to persist the authentication data returned by the authentication method.
    $wsAuth.CookieContainer = new-object System.Net.CookieContainer;
    # At this moment, $wsAuth.CookieContainer.Count == 0
    $wsAuth.Login("myuser", "mypassword");
    # At this moment, $wsAuth.CookieContainer.Count == 1
    # Creates the Web Service Proxy
    $ws = New-WebServiceProxy -Uri "http://spserver/_vti_bin/lists.asmx?wsdl" -Credential $credentials;
    # Transfer the cookie container to the current web service proxy.
    $ws.CookieContainer = $wsAuth.CookieContainer;
    # Calls the web method
    $ws.GetListCollection();
    
  4. Calling the same Web Service “the hard way”: assembling the raw SOAP message and using the object System.Net.HttpRequest to make the request:
    $soapMessageBytes = [System.Text.UTF8Encoding]::UTF8.GetBytes(@"
    <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <soap12:Body>
    <GetListCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/" />
    </soap12:Body>
    </soap12:Envelope>
    "@);
    $httpWebRequest = [System.Net.HttpWebRequest][System.Net.HttpWebRequest]::Create("http://spserver/_vti_bin/lists.asmx");
    $httpWebRequest.UseDefaultCredentials = $true;
    $httpWebRequest.Headers.Add("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListCollection");
    $httpWebRequest.ContentType = "text/xml;charset=`"utf-8`"";
    $httpWebRequest.ContentLength = $soapMessageBytes.Length;
    $httpWebRequest.Accept = "text/xml";
    $httpWebRequest.Method = "POST";
    $requestStream = $httpWebRequest.GetRequestStream();
    $requestStream.Write($soapMessageBytes, 0, $soapMessageBytes.Length);
    $requestStream.Flush();
    $response = $httpWebRequest.GetResponse();
    $resultStream = New-Object System.IO.StreamReader($response.GetResponseStream());
    $resultCall = $resultStream.ReadToEnd();
    $resultCall;
    
Advertisements

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s