/** Code for retrieving Audit History in Microsoft Dynamics CRM 2011 **/
For retrieving from the Audit history we need to use “ RetrieveRecordChangeHistoryRes ponse”.
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
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 Methodprivate static void ExtractFromAudit(DisplayAuditDetails (found in CRM SDK and modified a bit)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. RetrieveRecordChangeHistoryReq uest changeRequest = new RetrieveRecordChangeHistoryReq uest(); changeRequest.Target = new EntityReference(Contact. EntityLogicalName, entityId); RetrieveRecordChangeHistoryRes ponse changeResponse = ( RetrieveRecordChangeHistoryRes ponse)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); } }
///
/// 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 (KeyValuePair attribute 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();
}
Result:
