Connecting iOS (iPhone/iPad) apps to Dynamics CRM using SOAP endpoint
Non .Net clients can access Microsoft Dynamics CRM business data using SOAP end point or Web API. In this tutorial we will learn how to perform SOAP authentication to Microsoft Dynamics CRM from Swift, which is is a new programming language for iOS app development. We have made a sample application which sends WhoAmIRequest and CRM responds back with the logged in username.
Application
The app takes Dynamics CRM online URL, username and password and returns the full name of the logged in user. The final application looks like below
Note: The complete code of the application is hosted on GitHub.
Working
CRMAuth
This is the main class which does all the authentication process. The Authentication SOAP envelope is being created by the following code
func getAuthHeaderEnvelopOnline(url:String, username:String, password:String) -> String{
let urnAddress = GetUrnOnline(url)
var XML = [String]()
//Get Auth Header Request
XML.append("<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">")
XML.append("<s:Header>")
XML.append("<a:Action s:mustUnderstand=\"1\">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>")
XML.append("<a:MessageID>urn:uuid:\(CreateGUID()) </a:MessageID>")
XML.append("<a:ReplyTo>")
XML.append("<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>")
XML.append("</a:ReplyTo>")
XML.append("<a:To s:mustUnderstand=\"1\">https://login.microsoftonline.com/RST2.srf</a:To>")
XML.append("<o:Security s:mustUnderstand=\"1\" xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">")
XML.append("<u:Timestamp u:Id=\"_0\">")
XML.append("<u:Created>\(GetCurrentDateAndTime())</u:Created>")
XML.append("<u:Expires>\(Get1HourPlusDateAndTime())</u:Expires>")
XML.append("</u:Timestamp>")
XML.append("<o:UsernameToken u:Id=\"uuid-\(CreateGUID())-1\">")
XML.append("<o:Username>\(username)</o:Username>")
XML.append("<o:Password>\(password)</o:Password>")
XML.append("</o:UsernameToken>")
XML.append("</o:Security>")
XML.append("</s:Header>")
XML.append("<s:Body>")
XML.append("<trust:RequestSecurityToken xmlns:trust=\"http://schemas.xmlsoap.org/ws/2005/02/trust\">")
XML.append("<wsp:AppliesTo xmlns:wsp=\"http://schemas.xmlsoap.org/ws/2004/09/policy\">")
XML.append("<a:EndpointReference>")
XML.append("<a:Address>urn:\(urnAddress)</a:Address>")
XML.append("</a:EndpointReference>")
XML.append("</wsp:AppliesTo>")
XML.append("<trust:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</trust:RequestType>")
XML.append("</trust:RequestSecurityToken>")
XML.append("</s:Body>")
XML.append("</s:Envelope>")
return XML.joinWithSeparator("")
}
We are passing CRM Online URL, username and password and it return four values, which are:
- Expire time
- Key identifier
- Token0
- Token1
First value determines the expiry of authentication token. Second, third and fourth values are used for creating SOAP header.
ExecuteSOAP
This class creates and executes SOAP Request with the help of tokens and keyidentifier returned by CRMAuth class.
CRMSOAPBodies
This class holds the SOAP bodies which is used in SOAP request inside SOAP Envelope.
Below is an example of how SOAP bodies are made.
func WhoIAMBody() -> String{
var XML = [String]()
XML.append("<s:Body>")
XML.append("<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\">")
XML.append("<request i:type=\"c:WhoAmIRequest\" xmlns:b=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:c=\"http://schemas.microsoft.com/crm/2011/Contracts\">")
XML.append("<b:Parameters xmlns:d=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\"/>")
XML.append("<b:RequestId i:nil=\"true\"/>")
XML.append("<b:RequestName>WhoAmI</b:RequestName>")
XML.append("</request>")
XML.append("</Execute>")
XML.append("</s:Body>")
return XML.joinWithSeparator("")
}
Conclusion
The given sample is great starter app to perform authentication in iOS apps. It gives a good overview of how you can make connection with CRM from external apps not developed in .NET. You can freely use this sample in your apps for authentication purposes.

Like
Report

*This post is locked for comments