Skip to content

Instantly share code, notes, and snippets.

@YARG
Last active August 29, 2015 14:15
Show Gist options
  • Save YARG/984bab9f61d8d97e8e54 to your computer and use it in GitHub Desktop.
Save YARG/984bab9f61d8d97e8e54 to your computer and use it in GitHub Desktop.
Simple SOAP call to update the status of a Microsoft CRM 2011 entity. The organization service Url can be found by going into Microsoft CRM -> Customizations -> Developer Resources
public void MakeTestAppointmentUpdate()
{
string serviceUrl = "<your organization service endpoint>/web";
string userName = "username";
string password = "password";
string logicalEntityName = "appointment";
string activityId = "ba9f21c5-82ac-e411-95dc-005056be7c4c";
int stateCode = 1;
int statusCode = 3;
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(CreateSoapPayload(logicalEntityName, activityId, stateCode, statusCode));
content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml; charset=utf-8");
var response = httpClient.PostAsync(uri, content).Result;
System.Diagnostics.Debug.WriteLine(response.StatusCode);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
throw;
}
}
/// <summary>
/// Creates a Soap Payload for updating a given entity state
/// <note>
/// The state and status code go hand in hand. Please see the link below for the different codes
/// http://mostlymscrm.blogspot.co.uk/2012/06/entity-statecodes-and-statuscodes.html#!/2012/06/entity-statecodes-and-statuscodes.html
/// </note>
/// </summary>
/// <param name="entityLogicalName">The logical name - i.e. appointment</param>
/// <param name="activityId">The GUID activity Id (which we're keeping as a string)</param>
/// <param name="stateCode">The state code</param>
/// <param name="statusCode">The status code</param>
/// <returns></returns>
private string CreateSoapPayload(string entityLogicalName, string activityId, int stateCode, int statusCode)
{
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=\"b:SetStateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">");
sb.AppendLine("<a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">");
sb.AppendLine("<a:KeyValuePairOfstringanyType>");
sb.AppendLine("<c:key>EntityMoniker</c:key>");
sb.AppendLine("<c:value i:type=\"a:EntityReference\">");
sb.AppendLine("<a:Id>" + activityId + "</a:Id>");
sb.AppendLine("<a:LogicalName>" + entityLogicalName + "</a:LogicalName>"); // appointment //
sb.AppendLine("<a:Name i:nil=\"true\" />");
sb.AppendLine("</c:value>");
sb.AppendLine("</a:KeyValuePairOfstringanyType>");
sb.AppendLine("<a:KeyValuePairOfstringanyType>");
sb.AppendLine("<c:key>State</c:key>");
sb.AppendLine("<c:value i:type=\"a:OptionSetValue\">");
sb.AppendLine("<a:Value>" + stateCode + "</a:Value>");
sb.AppendLine("</c:value>");
sb.AppendLine("</a:KeyValuePairOfstringanyType>");
sb.AppendLine("<a:KeyValuePairOfstringanyType>");
sb.AppendLine("<c:key>Status</c:key>");
sb.AppendLine("<c:value i:type=\"a:OptionSetValue\">");
sb.AppendLine("<a:Value>" + statusCode + "</a:Value>");
sb.AppendLine("</c:value>");
sb.AppendLine("</a:KeyValuePairOfstringanyType>");
sb.AppendLine("</a:Parameters>");
sb.AppendLine("<a:RequestId i:nil=\"true\" />");
sb.AppendLine("<a:RequestName>SetState</a:RequestName>");
sb.AppendLine("</request>");
sb.AppendLine("</Execute>");
sb.AppendLine("</s:Body>");
sb.AppendLine("</s:Envelope>");
return sb.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment