public IFieldDesc GetFieldDesc(string key, CrmAttrbite attr, Entity entity) { switch (attr.AttributeTypeName) { case PartyListType: return(new PartyListFieldValue(_log)); case DateTimeType: return(new DatetimeFieldValue(_log)); case LookupType: case OwnerType: case CustomerType: return(new LoopkupFieldValue(_log)); case ImageType: return(null); case PicklistType: case StateType: case StatusType: return(new PicklistFieldValue(_log)); case MoneyType: return(new MoneyFieldValue(_log)); default: return(new SimpleFieldValue(_log)); } }
public IEnumerable <CrmAttrbite> GetEntityFields(IOrganizationService service, Action <string> log, string entity, int languageCode) { if (crmMetaData.ContainsKey(entity)) { return(crmMetaData[entity]); } List <CrmAttrbite> temp = new List <CrmAttrbite>(); RetrieveEntityRequest retrieveBankAccountEntityRequest = new RetrieveEntityRequest { EntityFilters = EntityFilters.Attributes, LogicalName = entity }; RetrieveEntityResponse retrieveBankAccountEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveBankAccountEntityRequest); foreach (var attEntity in retrieveBankAccountEntityResponse.EntityMetadata.Attributes) { CrmAttrbite newAttr = new CrmAttrbite(); newAttr.AttributeTypeName = attEntity.AttributeTypeName.Value; newAttr.FieldName = attEntity.LogicalName; newAttr.DisplayName = attEntity.DisplayName.LocalizedLabels.Where(d => d.LanguageCode == languageCode).Select(s => s.Label).FirstOrDefault(); temp.Add(newAttr); } crmMetaData.Add(entity, temp); return(crmMetaData[entity]); }
private void AddToListItems(IOrganizationService service, string entityName, int langCode, AuditDetail detail, List <DataModel.AuditLogModel> items, AuditLogModelBase auditLogModelBase) { var crmAttrbites = _schema.GetEntityFields(service, _log, entityName, langCode); AuditLogModel auditLogModel; IFieldDesc fieldDesc = null; CrmAttrbite attr = null; CrmValueAttrbite crmValueAttrbite = null; Entity record = (Entity)detail.AuditRecord; // Show additional details for certain AuditDetail sub-types. var detailType = detail.GetType(); if (detailType == typeof(AttributeAuditDetail)) { var attributeDetail = (AttributeAuditDetail)detail; if (attributeDetail.NewValue != null && attributeDetail.NewValue.Attributes.Any()) { // Display the old and new attribute values. foreach (KeyValuePair <String, object> attribute in attributeDetail.NewValue.Attributes) { fieldDesc = null; crmValueAttrbite = null; attr = crmAttrbites.Where(a => a.FieldName == attribute.Key).FirstOrDefault(); if (attr == null) { continue; } auditLogModel = new AuditLogModel(); auditLogModel.AuditLogId = Guid.NewGuid(); auditLogModelBase.CopyTo(auditLogModel); auditLogModel.FieldDesc = attr.DisplayName; auditLogModel.FieldSchemaName = attr.FieldName; if (attributeDetail.OldValue.Contains(attribute.Key)) { fieldDesc = _factoryFieldsValue.GetFieldDesc(attribute.Key, attr, attributeDetail.OldValue); if (fieldDesc != null) { crmValueAttrbite = fieldDesc.GetValue(attribute.Key.ToString(), attributeDetail.OldValue); auditLogModel.OldValue = crmValueAttrbite.FieldValue; } } fieldDesc = _factoryFieldsValue.GetFieldDesc(attribute.Key, attr, attributeDetail.NewValue); if (fieldDesc != null) { crmValueAttrbite = fieldDesc.GetValue(attribute.Key.ToString(), attributeDetail.NewValue); auditLogModel.NewValue = crmValueAttrbite.FieldValue; } items.Add(auditLogModel); } } // for data who clear (the old value was data and new value was empty) if (attributeDetail.OldValue != null && attributeDetail.OldValue.Attributes.Any()) { foreach (KeyValuePair <String, object> attribute in attributeDetail.OldValue.Attributes) { if (attributeDetail.NewValue != null && !attributeDetail.NewValue.Contains(attribute.Key)) { fieldDesc = null; crmValueAttrbite = null; attr = crmAttrbites.Where(a => a.FieldName == attribute.Key).FirstOrDefault(); auditLogModel = new AuditLogModel(); auditLogModel.AuditLogId = Guid.NewGuid(); auditLogModelBase.CopyTo(auditLogModel); auditLogModel.FieldDesc = attr.DisplayName; auditLogModel.FieldSchemaName = attr.FieldName; fieldDesc = _factoryFieldsValue.GetFieldDesc(attribute.Key, attr, attributeDetail.OldValue); if (fieldDesc != null) { crmValueAttrbite = fieldDesc.GetValue(attribute.Key.ToString(), attributeDetail.OldValue); auditLogModel.OldValue = crmValueAttrbite.FieldValue; auditLogModel.NewValue = ""; } items.Add(auditLogModel); } } } } }