Beispiel #1
0
        /// <summary>
        /// Retrieves EntityId from the Context
        /// Create,Update,Delete,SetState,Assign,DeliverIncoming
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static Guid GetEntityId(IPluginExecutionContext context)
        {
            switch (context.MessageName)
            {
            case MessageName.Create:
            case MessageName.DeliverIncoming:
                if (context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction)
                {
                    //throw new InvalidPluginExecutionException("EntityId is not available in PreCreate");
                    return(Guid.Empty);
                }
                else
                {
                    //CreateResponse r;
                    //r.id;
                    if (context.OutputParameters.Contains(ParameterName.Id))
                    {
                        return((Guid)context.OutputParameters[ParameterName.Id]);
                    }

                    //DeliverIncomingEmailResponse r;
                    //r.EmailId;
                    if (context.OutputParameters.Contains(ParameterName.EmailId))
                    {
                        return((Guid)context.OutputParameters[ParameterName.EmailId]);
                    }
                }
                break;

            case MessageName.Update:
                //context.InputParameters.Contains(ParameterName.Target)
                IMetadataService      metadataService = context.CreateMetadataService(false);
                RetrieveEntityRequest rar             = new RetrieveEntityRequest();
                rar.LogicalName = context.PrimaryEntityName;
                rar.EntityItems = EntityItems.IncludeAttributes;
                RetrieveEntityResponse resp = (RetrieveEntityResponse)metadataService.Execute(rar);
                string keyName = resp.EntityMetadata.PrimaryKey;

                //UpdateRequest u;
                //TargetUpdateAccount a;
                //a.Account; // This s Dynamic entity
                //u.Target = a;

                // Update
                if (context.InputParameters[ParameterName.Target] is DynamicEntity)
                {
                    Key key = (Key)((DynamicEntity)context.InputParameters[ParameterName.Target]).Properties[keyName];
                    return(key.Value);
                }
                break;

            case MessageName.Delete:
            case MessageName.Assign:
            case MessageName.GrantAccess:
            case MessageName.Handle:
                if (context.InputParameters[ParameterName.Target] is Moniker)
                {
                    Moniker monikerId = (Moniker)context.InputParameters[ParameterName.Target];
                    return(monikerId.Id);
                }
                break;

            case MessageName.SetState:
            case MessageName.SetStateDynamicEntity:
                //SetStateAccountRequest r;
                //r.EntityId; // Guid === Moniker
                //r.AccountState; // State
                //r.AccountStatus; // Status
                return(((Moniker)context.InputParameters.Properties[ParameterName.EntityMoniker]).Id);;

            default:
                if (context.InputParameters.Contains(ParameterName.Target) &&
                    (context.InputParameters[ParameterName.Target] is Moniker))
                {
                    Moniker monikerId = (Moniker)context.InputParameters[ParameterName.Target];
                    return(monikerId.Id);
                }
                //Try by best route else fail
                //throw new InvalidPluginExecutionException("GetEntityId could not extract the Guid from Context");
                return(Guid.Empty);
            }
            //throw new InvalidPluginExecutionException("GetEntityId could not extract the Guid from Context");
            return(Guid.Empty);
        }
Beispiel #2
0
        public Guid GetEntityId(IPluginExecutionContext context)
        {
            IMetadataService metaData = context.CreateMetadataService(true);

            return this.GetEntityId(context, metaData);
        }
Beispiel #3
0
        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);
                                }
                            }
                        }
                    }
                }
            }
        }