private static string CrmWhoAmI(CrmAuthenticationHeader authHeader, string url) { StringBuilder xml = new StringBuilder(); 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>"); XmlDocument xDoc = CrmExecuteSoap.ExecuteSoapRequest(authHeader, xml.ToString(), url); if (xDoc == null) { return(null); } XmlNodeList nodes = xDoc.GetElementsByTagName("b:KeyValuePairOfstringanyType"); foreach (XmlNode node in nodes) { if (node.FirstChild.InnerText == "UserId") { return(node.LastChild.InnerText); } } return(null); }
async partial void LoginClick(UIButton sender) { if (LoginButton.Title(UIControlState.Normal) == "Logout") { NSUserDefaults.StandardUserDefaults.RemoveObject("CrmUserId"); LoginButton.SetTitle("Login To CRM", UIControlState.Normal); Output("Cleared CRM user id"); return; } if (string.IsNullOrEmpty(CrmUrl.Text) || string.IsNullOrEmpty(CrmUsername.Text) || string.IsNullOrEmpty(CrmPassword.Text)) { return; } string url = CrmUrl.Text; if (!url.EndsWith("/")) { url += "/"; } CrmAuthenticationHeader authHeader = null; CrmAuth auth = new CrmAuth(); authHeader = url.Contains("dynamics.com") ? auth.GetHeaderOnline(CrmUsername.Text, CrmPassword.Text, url) : auth.GetHeaderOnPremise(CrmUsername.Text, CrmPassword.Text, url); if (authHeader == null) { Output("Unable to connect to CRM"); return; } string userid = CrmWhoAmI(authHeader, url); if (string.IsNullOrEmpty(userid)) { Output("Unable to retrieve user info from CRM"); return; } //Save the userid NSUserDefaults.StandardUserDefaults.SetString(userid, "CrmUserId"); //Register for notifications //Moved from AppDelegate.cs - FinishedLaunching var settings = UIUserNotificationSettings.GetSettingsForTypes( UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet()); UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); UIApplication.SharedApplication.RegisterForRemoteNotifications(); LoginButton.SetTitle("Logout", UIControlState.Normal); Output("Retrieved CRM user id"); }