Last active
August 29, 2015 14:15
-
-
Save YARG/2a45c15d7fa724cae458 to your computer and use it in GitHub Desktop.
Simple SOAP call to retrieve a list of deleted Appointment IDs for a given Microsoft CRM user Id.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Creates a SOAP payload to retrieve a list of deleted Appointment Ids for a given user | |
/// </summary> | |
/// <param name="userId">The GUID crm User Id</param> | |
/// <param name="dateFrom">From date</param> | |
/// <returns></returns> | |
private string CreateSoapPayloadForDeletedAppointments(string userId, string dateFrom) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendLine("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"); | |
sb.AppendLine("<s:Body>"); | |
sb.AppendLine("<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"); | |
sb.AppendLine("<request i:type=\"a:RetrieveMultipleRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">"); | |
sb.AppendLine("<a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">"); | |
sb.AppendLine("<a:KeyValuePairOfstringanyType>"); | |
sb.AppendLine("<b:key>Query</b:key>"); | |
sb.AppendLine("<b:value i:type=\"a:QueryExpression\">"); | |
sb.AppendLine("<a:ColumnSet>"); | |
sb.AppendLine("<a:AllColumns>true</a:AllColumns>"); | |
sb.AppendLine("<a:Columns xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" />"); | |
sb.AppendLine("</a:ColumnSet>"); | |
sb.AppendLine("<a:Criteria>"); | |
sb.AppendLine("<a:Conditions>"); | |
sb.AppendLine(AddConditionExpression("Equal", "int", "operation", "3")); | |
sb.AppendLine(AddConditionExpression("Equal", "int", "objecttypecode", "4201")); | |
sb.AppendLine(AddConditionExpression("Equal", "string", "userid", userId)); | |
sb.AppendLine(AddConditionExpression("GreaterThan", "dateTime", "createdon", dateFrom)); | |
sb.AppendLine("</a:Conditions>"); | |
sb.AppendLine("<a:FilterOperator>And</a:FilterOperator>"); | |
sb.AppendLine("<a:Filters />"); | |
sb.AppendLine("</a:Criteria>"); | |
sb.AppendLine("<a:Distinct>false</a:Distinct>"); | |
sb.AppendLine("<a:EntityName>audit</a:EntityName>"); | |
sb.AppendLine("<a:LinkEntities />"); | |
sb.AppendLine("<a:Orders>"); | |
sb.AppendLine("<a:OrderExpression>"); | |
sb.AppendLine("<a:AttributeName>createdon</a:AttributeName>"); | |
sb.AppendLine("<a:OrderType>Descending</a:OrderType>"); | |
sb.AppendLine("</a:OrderExpression>"); | |
sb.AppendLine("</a:Orders>"); | |
sb.AppendLine("<a:PageInfo>"); | |
sb.AppendLine("<a:Count>0</a:Count>"); | |
sb.AppendLine("<a:PageNumber>0</a:PageNumber>"); | |
sb.AppendLine("<a:PagingCookie i:nil=\"true\" />"); | |
sb.AppendLine("<a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>"); | |
sb.AppendLine("</a:PageInfo>"); | |
sb.AppendLine("<a:NoLock>false</a:NoLock>"); | |
sb.AppendLine("</b:value>"); | |
sb.AppendLine("</a:KeyValuePairOfstringanyType>"); | |
sb.AppendLine("</a:Parameters>"); | |
sb.AppendLine("<a:RequestId i:nil=\"true\" />"); | |
sb.AppendLine("<a:RequestName>RetrieveMultiple</a:RequestName>"); | |
sb.AppendLine("</request>"); | |
sb.AppendLine("</Execute>"); | |
sb.AppendLine("</s:Body>"); | |
sb.AppendLine("</s:Envelope>"); | |
return sb.ToString(); | |
} | |
private string AddConditionExpression(string operation, string type, string attributeName, string attributeValue) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendLine("<a:ConditionExpression>"); | |
sb.AppendLine("<a:AttributeName>" + attributeName + "</a:AttributeName>"); | |
sb.AppendLine("<a:Operator>" + operation + "</a:Operator>"); | |
sb.AppendLine("<a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">"); | |
sb.AppendLine(" <c:anyType i:type=\"d:" + type + "\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + attributeValue + "</c:anyType>"); | |
sb.AppendLine("</a:Values>"); | |
sb.AppendLine("</a:ConditionExpression>"); | |
return sb.ToString(); | |
} | |
/// <summary> | |
/// Returns a list of deleted appointment Ids for a given user as Json | |
/// </summary> | |
/// <param name="userId">CRM User Id</param> | |
/// <param name="fromDateTime">From date</param> | |
/// <returns></returns> | |
public string GetDeletedAppointmentIdsForUserAsJson(string userId, DateTime fromDateTime) | |
{ | |
string deletedFromDate = fromDateTime.ToUniversalTime().ToString("s"); | |
List<string> appointmentIds = new List<string>(); | |
string serviceUrl = "<Your organizational url>/XRMServices/2011/Organization.svc/web"; | |
string userName = "USERNAME"; | |
string password = "PASSWORD"; | |
try | |
{ | |
using (HttpClientHandler handler = new HttpClientHandler()) | |
{ | |
handler.Credentials = new System.Net.NetworkCredential(userName, password); | |
using (HttpClient httpClient = new HttpClient(handler)) | |
{ | |
Uri uri = new Uri(serviceUrl); | |
httpClient.DefaultRequestHeaders.Accept.Clear(); | |
httpClient.DefaultRequestHeaders.Add("Accept", "application/xml, text/xml, */*"); | |
httpClient.DefaultRequestHeaders.Add("SOAPAction", | |
"http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute"); | |
StringContent content = | |
new StringContent(CreateSoapPayloadForDeletedAppointments(userId, deletedFromDate)); | |
content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml; charset=utf-8"); | |
var response | |
= httpClient.PostAsync(uri, content).Result; | |
// check to see if we have a valid response | |
if (response.StatusCode == HttpStatusCode.OK) | |
{ | |
// ok great we do, so let's load the soap response up and extract what we need | |
Stream resultsStream = response.Content.ReadAsStreamAsync().Result; | |
XDocument xDoc = XDocument.Load(resultsStream); | |
XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";//Envelope namespace s | |
XNamespace a = "http://schemas.microsoft.com/xrm/2011/Contracts";//a namespace | |
XmlNamespaceManager namespaces = new XmlNamespaceManager(new NameTable()); | |
XNamespace ns = xDoc.Root.GetDefaultNamespace(); | |
namespaces.AddNamespace("ns", ns.NamespaceName); | |
foreach (var entity in xDoc.Descendants(s + "Body").Descendants(a + "Entities").Descendants(a + "Entity")) | |
{ | |
// gett the appointment ID that was deleted | |
string id = entity.Descendants(a + "Id").First().Value; | |
appointmentIds.Add(id); | |
} | |
} | |
response.Dispose(); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
// log | |
System.Diagnostics.Debug.WriteLine(ex.Message); | |
throw; | |
} | |
// convert the results to json | |
return JsonConvert.SerializeObject(appointmentIds); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment