/// <summary> /// Method to retrieve the SDK Message Identifier. /// </summary> /// <param name="step">The Step Object.</param> /// <param name="service">The XRM Service.</param> /// <returns>The SDK Message Identifier.</returns> private Guid GetSdkMessageId(PluginStep step, XrmService service) { try { SdkMessageId = new Guid(); QuerySdkMessage = new QueryByAttribute(SdkMessage.EntityLogicalName); QuerySdkMessage.AddAttributeValue("name", step.PluginMessage); SdkMessageEntityCollection = service.RetrieveMultiple(QuerySdkMessage); foreach (Entity entity in SdkMessageEntityCollection.Entities) { SdkMessage = (SdkMessage)entity; if (SdkMessage.SdkMessageId.HasValue) { SdkMessageId = SdkMessage.SdkMessageId.Value; } else { throw new ArgumentException("The message could not be found!"); } } return(SdkMessageId); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to validate the Image before Registration. /// In certain circumstances, Dynamics CRM will not allow the Registration of an Image. /// </summary> /// <param name="step">The Step Object.</param> /// <param name="image">The Image Object.</param> private void ValidateImage(PluginStep step, Image image) { try { if (image.ImageType == XrmImageType.PreImage) { if (step.PluginMessage == "Create") { if (step.Stage == XrmPluginStepStage.PreValidation || step.Stage == XrmPluginStepStage.PreOperation) { throw new ArgumentException(GetImageExceptionErrorMessage(step, image)); } } } else { if (step.Stage == XrmPluginStepStage.PreValidation || step.Stage == XrmPluginStepStage.PreOperation) { throw new ArgumentException(GetImageExceptionErrorMessage(step, image)); } if (step.Stage == XrmPluginStepStage.PostOperationDeprecated || step.Stage == XrmPluginStepStage.PostOperation) { if (step.PluginMessage == "Delete") { throw new ArgumentException(GetImageExceptionErrorMessage(step, image)); } } } } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to retrieve the Impersonating User Identifier. /// </summary> /// <param name="step">The Step Object containing the user account details.</param> /// <param name="service">The XRM Service.</param> /// <returns>The Impersonating User Identifier.</returns> private Guid GetImpersonatingUserId(PluginStep step, XrmService service) { try { ImpersonatingUserId = new Guid(); QuerySystemUser = new QueryByAttribute(SystemUser.EntityLogicalName); QuerySystemUser.AddAttributeValue("domainname", step.ImpersonatingUserDomainName); SystemUserEntityCollection = service.RetrieveMultiple(QuerySystemUser); foreach (Entity entity in SystemUserEntityCollection.Entities) { User = (SystemUser)entity; if (User.SystemUserId.HasValue) { ImpersonatingUserId = User.SystemUserId.Value; } else { throw new ArgumentException("The user could not be found!"); } } return(ImpersonatingUserId); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to retrieve the Secure Configuration Identifier. /// </summary> /// <param name="step">The XRM Step Object.</param> /// <param name="service">The XRM Service.</param> /// <param name="secureConfigurationId">The Secure Configuration Identifier.</param> /// <param name="sdkMessageProcessingStep">The Dynamics CRM Step Object.</param> /// <returns>The Secure Configuration Identifier.</returns> private Guid?GetSecureConfigId(PluginStep step, XrmService service, Guid?secureConfigurationId, SdkMessageProcessingStep sdkMessageProcessingStep) { try { if (!string.IsNullOrEmpty(step.SecureConfiguration)) { if (sdkMessageProcessingStep.SdkMessageProcessingStepSecureConfigId != null) { SecureConfigurationId = sdkMessageProcessingStep.SdkMessageProcessingStepSecureConfigId.Id; } else { SecureConfigurationId = Guid.NewGuid(); } } else { if (sdkMessageProcessingStep.SdkMessageProcessingStepSecureConfigId != null) { service.Delete(SdkMessageProcessingStepSecureConfig.EntityLogicalName, sdkMessageProcessingStep.SdkMessageProcessingStepSecureConfigId.Id); } } secureConfigurationId = SecureConfigurationId; return(secureConfigurationId); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to retrieve the SDK Message Filter Identifier. /// </summary> /// <param name="registration">The Registration Object.</param> /// <param name="step">The Step Object</param> /// <param name="service">The XRM Service.</param> /// <returns>The SDK Message Filter Identifier.</returns> private Guid GetSdkMessageEntityId(string xrmServerDetails, PluginStep step, XrmService service) { try { SdkMessageEntityId = new Guid(); XrmMetadata = new XrmMetadata(xrmServerDetails); PrimaryEntityMetadata = XrmMetadata.RetrieveEntity(step.PrimaryEntity); SecondaryEntityMetadata = new EntityMetadata(); if (!string.IsNullOrEmpty(step.SecondaryEntity)) { SecondaryEntityMetadata = XrmMetadata.RetrieveEntity(step.SecondaryEntity); } if (PrimaryEntityMetadata.MetadataId.HasValue) { QuerySdkMessageFilter = new QueryExpression { EntityName = SdkMessageFilter.EntityLogicalName, ColumnSet = new ColumnSet("sdkmessagefilterid"), Criteria = new FilterExpression() }; QuerySdkMessageFilter.Criteria.AddCondition("sdkmessageidname", ConditionOperator.Equal, step.PluginMessage); QuerySdkMessageFilter.Criteria.AddCondition("primaryobjecttypecode", ConditionOperator.Equal, PrimaryEntityMetadata.ObjectTypeCode); if (SecondaryEntityMetadata.MetadataId.HasValue) { QuerySdkMessageFilter.Criteria.AddCondition("secondaryobjecttypecode", ConditionOperator.Equal, SecondaryEntityMetadata.ObjectTypeCode); } else { if (!string.IsNullOrEmpty(step.SecondaryEntity)) { throw new ArgumentException("The secondary entity could not be found!"); } } SdkMessageFilterEntityCollection = service.RetrieveMultiple(QuerySdkMessageFilter); foreach (Entity entity in SdkMessageFilterEntityCollection.Entities) { SdkMessageFilter = (SdkMessageFilter)entity; if (SdkMessageFilter.SdkMessageFilterId.HasValue) { SdkMessageEntityId = SdkMessageFilter.SdkMessageFilterId.Value; } else { throw new ArgumentException("The message filter could not be found!"); } } } else { throw new ArgumentException("The primary entity could not be found!"); } return(SdkMessageEntityId); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method that returns an Exception Message. /// </summary> /// <param name="step">The Step Object.</param> /// <param name="image">The Image Object.</param> /// <returns>The Error Message.</returns> private string GetImageExceptionErrorMessage(PluginStep step, Image image) { try { this.ImageExceptionErrorMessage = "Cannot create an image for " + step.PluginMessage + " for a " + step.Stage + " step for a " + image.ImageType + " image."; return(this.ImageExceptionErrorMessage); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to verify if a Step is already Registered in Dynamics CRM. /// </summary> /// <param name="step">The Step to verify.</param> /// <param name="xrmPluginStep">The Step to populate.</param> /// <param name="service">The XRM Service.</param> /// <returns>An object containing the details of the verification, including a new Step Identifier if the Step isn't Registered.</returns> private ItemExists GetStepId(PluginStep step, XrmPluginStep xrmPluginStep, XrmService service) { try { ItemExists stepDetails = new ItemExists(); stepDetails.SecureConfigId = new Guid?(); stepDetails.ItemId = new Guid(); stepDetails.Exists = false; QueryByAttribute querySdkProcessingStepMessage = new QueryByAttribute(SdkMessageProcessingStep.EntityLogicalName); querySdkProcessingStepMessage.ColumnSet = new ColumnSet(true); querySdkProcessingStepMessage.AddAttributeValue("name", xrmPluginStep.Name); EntityCollection sdkMessageProcessingStepEntityCollection = service.RetrieveMultiple(querySdkProcessingStepMessage); if (sdkMessageProcessingStepEntityCollection.Entities.Count != 0) { foreach (Entity entity in sdkMessageProcessingStepEntityCollection.Entities) { SdkMessageProcessingStep sdkMessageProcessingStep = (SdkMessageProcessingStep)entity; if (sdkMessageProcessingStep.SdkMessageProcessingStepId.HasValue) { stepDetails.ItemId = sdkMessageProcessingStep.SdkMessageProcessingStepId.Value; stepDetails.Exists = true; } else { stepDetails.ItemId = Guid.NewGuid(); stepDetails.Exists = false; } stepDetails.SecureConfigId = GetSecureConfigId(step, service, stepDetails.SecureConfigId, sdkMessageProcessingStep); } } else { stepDetails.ItemId = Guid.NewGuid(); stepDetails.Exists = false; if (!string.IsNullOrEmpty(step.SecureConfiguration)) { stepDetails.SecureConfigId = Guid.NewGuid(); } } return(stepDetails); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
/// <summary> /// Method to retrieve the list of Images specified in the Registration Object linked to the Step object and populate the XRM Plugin Step accordingly. /// </summary> /// <param name="registration">The Registration Object.</param> /// <param name="step">The Step Object.</param> /// <param name="xrmPluginStep">The XRM Plugin Step Object.</param> internal XrmPluginStep GetRegistrationImages(string xrmServerDetails, PluginStep step, XrmPluginStep xrmPluginStep) { try { foreach (Image image in step.Images) { ValidateImage(step, image); XrmPluginImage xrmPluginImage = new XrmPluginImage(); using (xrmService = RegistrationService.GetService(xrmServerDetails)) { string attributeCSV = GetAttributeCSV(new XrmMetadata(xrmServerDetails), image, step.PrimaryEntity); ItemExists imageDetails = GetImageId(xrmPluginStep, image, xrmService, attributeCSV); xrmPluginImage.ImageId = imageDetails.ItemId; xrmPluginImage.Exists = imageDetails.Exists; xrmPluginImage.StepId = xrmPluginStep.StepId; xrmPluginImage.ImageType = image.ImageType; if (!string.IsNullOrEmpty(attributeCSV)) { xrmPluginImage.Attributes = attributeCSV; } xrmPluginImage.EntityAlias = image.EntityAlias; xrmPluginImage.MessageProperty = image.MessageProperty; if (image.Merge.HasValue) { xrmPluginImage.MessagePropertyName = GetMessagePropertyName(step.PluginMessage, image.Merge.Value); } else { xrmPluginImage.MessagePropertyName = GetMessagePropertyName(step.PluginMessage); } xrmPluginStep.Images.Add(xrmPluginImage); } } return(xrmPluginStep); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }