The following sample (which I partially found something similar in the SDK) retrieves the information of the Audit history for any object in CRM
Create Organization Service Proxy
static void Main(string[] args) { try { //Grab the organization service url by navigating to // Settings -> Customizations - > Developer Resources // CrmInstance.CrmServer URL for your CRM //CrmInstance.cc create a new ClientCredentials and pass your user name/password and domain using (var XrmProxy = new OrganizationServiceProxy(new Uri(CrmInstance.CrmServer + "/XRMServices/2011/Organization.svc"), null, CrmInstance.cc, null)) { ExtractFromAudit(XrmProxy); Console.WriteLine("Thank you for participating in this amazing experiment! muwhaha!"); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine(ex); Console.ReadKey(true); } }ExtractFromAudit Method
private static void ExtractFromAudit(OrganizationServiceProxy XrmProxy) { // The GUID of the object you want to retirve in this case i am passing contactid var entityId = new Guid("4DC98353-CF88-E011-87E1-005056B30007"); Console.WriteLine("Retrieving the change history.\n"); // Retrieve the audit history for the account and display it. RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest(); changeRequest.Target = new EntityReference(Contact.EntityLogicalName, entityId); RetrieveRecordChangeHistoryResponse changeResponse = (RetrieveRecordChangeHistoryResponse)XrmProxy.Execute(changeRequest); AuditDetailCollection details = changeResponse.AuditDetailCollection; foreach (AttributeAuditDetail detail in details.AuditDetails) { // Display some of the detail information in each audit record. DisplayAuditDetails(detail); } }DisplayAuditDetails (found in CRM SDK and modified a bit)
///Sample Results:/// Displays audit change history details on the console. /// /// private static void DisplayAuditDetails(AuditDetail detail) { // Write out some of the change history information in the audit record. Entity record = detail.AuditRecord; Console.WriteLine("\nAudit record created on: {0}", record["createdon"]); Console.WriteLine("Entity: {0}, Action: {1}, Operation: {2}", record.LogicalName ,record.FormattedValues["action"], record.FormattedValues["operation"]); // Show additional details for certain AuditDetail sub-types. var detailType = detail.GetType(); if (detailType == typeof(AttributeAuditDetail)) { var attributeDetail = (AttributeAuditDetail)detail; // Display the old and new attribute values. foreach (KeyValuePairattribute in attributeDetail.NewValue.Attributes) { String oldValue = "(no value)", newValue = "(no value)"; //TODO Display the lookup values of those attributes that do not contain strings. if (attributeDetail.OldValue.Contains(attribute.Key)) oldValue = attributeDetail.OldValue[attribute.Key].ToString(); newValue = attributeDetail.NewValue[attribute.Key].ToString(); Console.WriteLine("Attribute: {0}, old value: {1}, new value: {2}", attribute.Key, oldValue, newValue); } foreach (KeyValuePair attribute in attributeDetail.OldValue.Attributes) { if (!attributeDetail.NewValue.Contains(attribute.Key)) { String newValue = "(no value)"; //TODO Display the lookup values of those attributes that do not contain strings. String oldValue = attributeDetail.OldValue[attribute.Key].ToString(); Console.WriteLine("Attribute: {0}, old value: {1}, new value: {2}", attribute.Key, oldValue, newValue); } } } Console.WriteLine(); }
What do I put this code into to make this work? I have Visual Studio 2010 and CRM SDK. Sorry I am a beginning developer here :/
ReplyDeleteHi Matt,simply create a c# console app in visual studio and reference dlls from SDK's bin folder. There are good samples in the SDK which can help you get started.
ReplyDeleteforeach (KeyValuePair
ReplyDeletestring and object between it
Delete