Scenario: User wants to see audit trail of record in email for some particular account notifications.
Solution : Custom ribbon button calls plugin(I am only posting code relevant to retrieve Audit Logs) which has following functions:
//The below function returns single Audit Record detail
private static string DisplayAuditDetails(AuditDetail detail)
{
// Write out some of the change history information in the audit record.
Audit record = (Audit)detail.AuditRecord;
StringBuilder sb = new StringBuilder();
sb.Append(“\nAudit record created on: {0}” + record.CreatedOn.Value.ToLocalTime());
sb.Append(string.Format(“Entity: {0}, Action: {1}, Operation: {2}”,
record.ObjectId.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<String, object> 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();
sb.Append(string.Format(“Attribute: {0}, old value: {1}, new value: {2}”,
attribute.Key, oldValue, newValue));
}
foreach (KeyValuePair<String, object> 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();
sb.Append(string.Format(“Attribute: {0}, old value: {1}, new value: {2}”,
attribute.Key, oldValue, newValue));
}
}
}
sb.Append(“”);
return sb.ToString();
}
//The below function returns complete Audit Detail for a single record
private static string GetAuditDetails(string entityLogicalName, Guid entityId,IOrganizationService service)
{
RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest();
changeRequest.Target = new EntityReference(entityLogicalName, entityId);
RetrieveRecordChangeHistoryResponse changeResponse =
(RetrieveRecordChangeHistoryResponse)service.Execute(changeRequest);
AuditDetailCollection details = changeResponse.AuditDetailCollection;
StringBuilder final = new StringBuilder();
for (int i = 0; i < details.AuditDetails.Count; i++)
{
// Display some of the detail information in each audit record.
if (details.AuditDetails[i].GetType().ToString() == “Microsoft.Crm.Sdk.Messages.AttributeAuditDetail”)
final.Append(DisplayAuditDetails(details.AuditDetails[i]));
}
return final.ToString();
}
Function call :
string auditText = GetAuditDetails(“account”, new Guid(“A3627A42-5994-E411-80E1-C4346BACCF7C”), service);
//auditText can than be embedded in emails.
Hope it helps!
Hi Deepesh, Please share sample code for this solution