///// <summary> /// Re-Create Crm Service by using the current user /// </summary> /// <param name="CrmService">CrmService</param> /// <param name="pluginContext">IPluginExecutionContext</param> /// <param name="UserGuid">Guid of the system user</param> public static void RefreshService(this ICrmService CrmService, IPluginExecutionContext pluginContext, Guid UserGuid) { CrmService = pluginContext.CreateCrmService(UserGuid); }
/// <summary> /// Re-Create Crm Service with the current user id /// </summary> /// <param name="pluginContext"></param> /// <param name="userGuid">User Guid to use </param> /// <returns></returns> public static ICrmService RefreshService(this IPluginExecutionContext pluginContext, Guid userGuid) { ICrmService iservice = pluginContext.CreateCrmService(userGuid); return(iservice); }
/// <summary> /// Re-Create Crm Service by using the current user /// </summary> /// <param name="CrmService">Crm Service</param> /// <param name="pluginContext">Plugin Context</param> /// <param name="UseCurrentUser">True/False</param> public static void RefreshService(this ICrmService CrmService, IPluginExecutionContext pluginContext, bool UseCurrentUser) { CrmService = pluginContext.CreateCrmService(UseCurrentUser); }
public void Execute(IPluginExecutionContext context) { // Get a reference to the CRM Web Service & metadata service ICrmService crmService = context.CreateCrmService(true); IMetadataService metaData = context.CreateMetadataService(true); // Get the metadata about the current entity. var req = new RetrieveEntityRequest { EntityItems = EntityItems.IncludeAttributes, LogicalName = context.PrimaryEntityName }; var res = (RetrieveEntityResponse)metaData.Execute(req); // Create the record & set it to the audit entity. var auditRecord = new DynamicEntity { Name = _mAuditEntity }; // Set the Entity name, Message Type, Primary attribute value. auditRecord.Properties["custom_entity"] = context.PrimaryEntityName; auditRecord.Properties["custom_entity_label"] = res.EntityMetadata.DisplayName.UserLocLabel.Label; string primaryAttribute = GetValue(context, res.EntityMetadata.PrimaryField); if (string.IsNullOrEmpty(primaryAttribute)) { if (context.PostEntityImages != null) { if (context.PostEntityImages.Contains("Target")) { var de = (DynamicEntity)context.PostEntityImages["Target"]; if (de.Properties.Contains(res.EntityMetadata.PrimaryField)) { primaryAttribute = de.Properties[res.EntityMetadata.PrimaryField].ToString(); } } } // attribute was not in post image if (string.IsNullOrEmpty(primaryAttribute)) { if (context.PreEntityImages != null) { if (context.PreEntityImages.Contains("Target")) { var de = (DynamicEntity)context.PreEntityImages["Target"]; if (de.Properties.Contains(res.EntityMetadata.PrimaryField)) { primaryAttribute = de.Properties[res.EntityMetadata.PrimaryField].ToString(); } } } } } if (!string.IsNullOrEmpty(primaryAttribute)) { auditRecord.Properties["custom_primary_attribute"] = primaryAttribute; } //auditRecord.Properties["custom_primary_attribute"] = this.GetValue(context, res.EntityMetadata.PrimaryField); auditRecord.Properties["custom_message"] = context.MessageName; // Get the current object's id and save it for audit. Guid entityId = GetKeyValue(context, res.EntityMetadata.PrimaryKey); if (entityId != Guid.Empty) auditRecord.Properties["custom_entityid"] = entityId.ToString("D"); // If the Audit entity has a relationship to the Primary entity, then populate the lookup key. if (!string.IsNullOrEmpty(_mLookupKey) && entityId != Guid.Empty) auditRecord.Properties[_mLookupKey] = new Lookup(context.PrimaryEntityName, entityId); // Create the audit record. Guid auditId = crmService.Create(auditRecord); // Process the modified records if (context.PostEntityImages.Contains(ParameterName.Target)) { // Get a reference to the images. DynamicEntity pre; var post = (DynamicEntity)context.PostEntityImages[ParameterName.Target]; // If the message is "Create" then dummy up a pre-image. if (context.MessageName == MessageName.Create) { pre = new DynamicEntity(); } else { pre = (DynamicEntity)context.PreEntityImages[ParameterName.Target]; } // Iterate through the entity's attributes to determine change. foreach (AttributeMetadata metaAttribute in res.EntityMetadata.Attributes) { // Ensure the attribute is a parent level attribute. if (metaAttribute.AttributeOf == null) { // Get the property objects object preProp = GetObjectFromProperty(pre.Properties, metaAttribute.LogicalName); object postProp = GetObjectFromProperty(post.Properties, metaAttribute.LogicalName); // Ensure we have at least a pre/post property. bool bProcess = (preProp != null); if (!bProcess) bProcess = (postProp != null); if (bProcess) { // Get the value of the properties string preVal = CustomDynamicEntity.GetAttributeValue(preProp, true); string postVal = CustomDynamicEntity.GetAttributeValue(postProp, true); // Has the property changed? if (preVal != postVal) { // Create an object for saving the changed detail. var auditDetail = new DynamicEntity { Name = _mAuditDetailEntity }; auditDetail.Properties["custom_auditid"] = new Lookup(_mAuditEntity, auditId); string displayName = metaAttribute.DisplayName.UserLocLabel != null ? metaAttribute.DisplayName.UserLocLabel.Label : metaAttribute.LogicalName; auditDetail.Properties["custom_display_name"] = displayName; auditDetail.Properties["custom_schema_name"] = metaAttribute.LogicalName; // Determine whether a value is too big for the value field. bool bPreValueTooBig = false; bool bPostValueTooBig = false; // Evaluate too big for Pre Value. if (preVal != null) { if (preVal.Length > _mMaxLength) { bPreValueTooBig = true; auditDetail.Properties["custom_original_value"] = _mMsgTooBig; } else auditDetail.Properties["custom_original_value"] = preVal; } // Evaluate too big for Post Value. if (postVal != null) { if (postVal.Length > _mMaxLength) { bPostValueTooBig = true; auditDetail.Properties["custom_new_value"] = _mMsgTooBig; } else auditDetail.Properties["custom_new_value"] = postVal; } // Create the audit detail record. Guid auditDetailId = crmService.Create(auditDetail); // If the Post Value is too big, save it as a note. if (bPostValueTooBig) { const string cPostNoteToobigSubject = "Modified Value"; CreateNote(crmService, _mAuditDetailEntity, auditDetailId, cPostNoteToobigSubject, postVal); } // If the Pre Value is too big, save it as a note. if (bPreValueTooBig) { const string cPreNoteToobigSubject = "Original Value"; CreateNote(crmService, _mAuditDetailEntity, auditDetailId, cPreNoteToobigSubject, preVal); } } } } } } }