public static string SelectMessagePropertyName(CrmMessage message) { if (null == message) { throw new ArgumentNullException("message"); } // Handle the basic cases if (0 == message.ImageMessagePropertyNames.Count) { return(null); } else if (1 == message.ImageMessagePropertyNames.Count) { return(message.ImageMessagePropertyNames[0].Name); } // For multiple properties, display the selection form. using (MessagePropertyNameForm selectPropertyForm = new MessagePropertyNameForm(message)) { if (selectPropertyForm.ShowDialog() == DialogResult.OK && null != selectPropertyForm.SelectedMessagePropertyName) { return(selectPropertyForm.SelectedMessagePropertyName.Name); } } return(null); }
public void MessageXmlSerializer_SerializerString_UnitTest() { IMessageSerializer testObject = new MessageXmlSerializer(); var testData = new CrmMessage() { CustomId = "1433" }; var testResult = testObject.SerializerXmlString(testData); Assert.IsTrue(testResult.Length > 0); }
private void LoadEntities() { if (m_messageLoaded == Message) { return; } else { m_messageLoaded = Message; } Control activeControl = ActiveControl; if (Message == null) { txtPrimaryEntity.AutoCompleteCustomSource = new AutoCompleteStringCollection(); txtSecondaryEntity.AutoCompleteCustomSource = new AutoCompleteStringCollection(); if (activeControl != null) { activeControl.Focus(); } return; } else { AutoCompleteStringCollection primaryList = new AutoCompleteStringCollection(); AutoCompleteStringCollection secondaryList = new AutoCompleteStringCollection(); foreach (CrmMessageEntity entity in Message.FindMessageEntities(null, null)) { if (!string.IsNullOrEmpty(entity.PrimaryEntity)) { primaryList.Add(entity.PrimaryEntity); } if (!string.IsNullOrEmpty(entity.SecondaryEntity)) { secondaryList.Add(entity.SecondaryEntity); } } txtPrimaryEntity.AutoCompleteCustomSource = primaryList; txtSecondaryEntity.AutoCompleteCustomSource = secondaryList; if (activeControl != null) { activeControl.Focus(); } } }
private MessagePropertyNameForm(CrmMessage message) { if (null == message) { throw new ArgumentNullException("message"); } InitializeComponent(); _message = message; grpMessageProperties.Text = string.Format(CultureInfo.InvariantCulture, grpMessageProperties.Text, message.Name); //Create the list _buttonList = new RadioButton[message.ImageMessagePropertyNames.Count]; //Add the radio buttons to the list int radioSpace = radTemplateItem.Margin.Top + radTemplateItem.Height; int radioTop = radTemplateItem.Top + radioSpace; for (int i = 0; i < message.ImageMessagePropertyNames.Count; i++) { RadioButton button; if (0 == i) { button = radTemplateItem; } else { button = new RadioButton { Left = radTemplateItem.Left, Top = radioTop }; grpMessageProperties.Controls.Add(button); radioTop += radioSpace; } ImageMessagePropertyName property = message.ImageMessagePropertyNames[i]; button.Text = property.Label; button.Tag = property; button.CheckedChanged += new EventHandler(ButtonCheckedChanged); _buttonList[i] = button; } radTemplateItem.Visible = true; radTemplateItem.Checked = true; }
public void MessageXmlSerializer_Deserialize_UnitTest() { IMessageSerializer testObject = new MessageXmlSerializer(); var testData = new CrmMessage() { CustomId = "1433", }; var bytes = testObject.SerializerBytes(testData); var testResult = testObject.Deserialize <CrmMessage>(bytes); Assert.AreEqual(testResult.CustomId, "1433"); }
public static bool UpdateStep(CrmOrganization org, CrmPluginStep step, Guid?origSecureConfigId, IList <CrmPluginImage> updateImages) { if (org == null) { throw new ArgumentNullException("org"); } else if (step == null) { throw new ArgumentNullException("step"); } Dictionary <string, object> entityList = step.GenerateCrmEntities(); SdkMessageProcessingStep sdkStep = (SdkMessageProcessingStep)entityList[SdkMessageProcessingStep.EntityLogicalName]; // Loop through each image and set the new message property List <SdkMessageProcessingStepImage> sdkImages = null; if (null != updateImages) { // Ensure that the given message supports images CrmMessage message = org.Messages[step.MessageId]; if (0 == message.ImageMessagePropertyNames.Count && step.Images.Count > 0) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The step has images registered, but the \"{0}\" message doesn't support images.{1}In order to change the message to \"{0}\", delete the existing images.", message.Name, Environment.NewLine)); } // Loop through the existing images and update their message property name values sdkImages = new List <SdkMessageProcessingStepImage>(updateImages.Count); foreach (CrmPluginImage image in updateImages) { // Set the message property name for each of the images string propertyName = MessagePropertyNameForm.SelectMessagePropertyName(message); if (string.IsNullOrWhiteSpace(propertyName)) { return(false); } else if (string.Equals(image.MessagePropertyName, propertyName, StringComparison.Ordinal)) { continue; } // Create the entity to update the value SdkMessageProcessingStepImage sdkImage = new SdkMessageProcessingStepImage(); sdkImage.Id = image.ImageId; sdkImage.MessagePropertyName = propertyName; sdkImage.EntityState = EntityState.Changed; sdkImages.Add(sdkImage); } } //This is a sanity check. The UI won't allow a user to set the secure configuration. if (org.SecureConfigurationPermissionDenied) { sdkStep.Attributes.Remove("sdkmessageprocessingstepsecureconfigid"); sdkStep.RelatedEntities.Clear(); origSecureConfigId = null; } else if (entityList.ContainsKey(Entities.SdkMessageProcessingStepSecureConfig.EntityLogicalName)) { if (null == origSecureConfigId) { entityList.Remove(Entities.SdkMessageProcessingStepSecureConfig.EntityLogicalName); } else { SdkMessageProcessingStepSecureConfig sdkSecureConfig = (SdkMessageProcessingStepSecureConfig)entityList[SdkMessageProcessingStepSecureConfig.EntityLogicalName]; Guid secureConfigId; EntityState secureConfigState; if (step.SecureConfigurationId == origSecureConfigId && origSecureConfigId.GetValueOrDefault() != Guid.Empty) { //Set the ID of the secure configuration to be the secureConfigId = origSecureConfigId.GetValueOrDefault(); secureConfigState = EntityState.Changed; //Set the original secure config id so that the current id is not deleted sdkStep.SdkMessageProcessingStepSecureConfigId = new EntityReference(SdkMessageProcessingStepSecureConfig.EntityLogicalName, secureConfigId); } else { secureConfigId = Guid.NewGuid(); secureConfigState = EntityState.Created; } //Set the configuration id for the step step.SecureConfigurationId = secureConfigId; //Populate the secure configuration object and add it to the related entities sdkSecureConfig.SdkMessageProcessingStepSecureConfigId = secureConfigId; sdkSecureConfig.EntityState = secureConfigState; //Update the related entities sdkStep.sdkmessageprocessingstepsecureconfigid_sdkmessageprocessingstep = sdkSecureConfig; //Reset the original secure configuration origSecureConfigId = null; } } else if (null != origSecureConfigId) { // To null out if it was set before sdkStep.SdkMessageProcessingStepSecureConfigId = null; step.SecureConfigurationId = Guid.Empty; if (Guid.Empty == origSecureConfigId) { origSecureConfigId = null; } } // If the images need to be updated, there are two possible scenarios: // 1) The step is profiled -- The parent of the image is not the step that is currently being updated // but rather the profiler step itself. In order to avoid changing this, there will have to be two SDK // operations (update the step and update the images). Otherwise, the next time the profiled step executes, // the images won't be present. // 2) The step is not profiled -- The image can be added to the related entities because the parent step is the // current step being updated. if (null != sdkImages && sdkImages.Count > 0) { if (!step.IsProfiled || step.ProfilerStepId == step.StepId) { sdkStep.sdkmessageprocessingstepid_sdkmessageprocessingstepimage = sdkImages; } } //Update the step org.OrganizationService.Update(sdkStep); if (step.IsProfiled && null != sdkImages && sdkImages.Count > 0) { // Update the Profiler step with the new property values as a single transaction to minimize the // possibility of data corruption. SdkMessageProcessingStep profilerStep = new SdkMessageProcessingStep(); profilerStep.Id = step.ProfilerStepId.GetValueOrDefault(); profilerStep.sdkmessageprocessingstepid_sdkmessageprocessingstepimage = sdkImages; org.OrganizationService.Update(profilerStep); } // Refresh the objects so that the caller has up-to-date data OrganizationHelper.RefreshStep(org, step); if (null != updateImages) { foreach (CrmPluginImage image in updateImages) { OrganizationHelper.RefreshImage(org, image, step); } } // Delete the orphaned Secure config when nulling out the value on the step if (null != origSecureConfigId) { org.OrganizationService.Delete(SdkMessageProcessingStepSecureConfig.EntityLogicalName, origSecureConfigId.GetValueOrDefault()); } return(true); }
private void btnRegister_Click(object sender, EventArgs e) { CrmPluginImage image = new CrmPluginImage(m_org); #region Extract Information if (trvPlugins.SelectedNode == null || (m_currentImage == null && trvPlugins.SelectedNode.NodeType != CrmTreeNodeType.Step)) { MessageBox.Show("A Step must be selected when registering an image", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else if (!chkImageTypePost.Checked && !chkImageTypePre.Checked) { MessageBox.Show("At least one Image Type must be specified. This field is required.", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else if (txtEntityAlias.TextLength == 0 || txtEntityAlias.Text.Trim().Length == 0) { MessageBox.Show("You must enter an alias for this Image. This is a required field.", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else if (!crmParameters.HasAttributes) { MessageBox.Show("You must specify at least one attribute. This is a required field", "Registration", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //Retrieve the step for this image CrmPluginStep step; if (m_currentImage == null) { step = (CrmPluginStep)trvPlugins.SelectedNode; //Verify that the step is not a system item if (step.IsSystemCrmEntity) { m_orgControl.ShowSystemItemError("Cannot register image on this step"); return; } } else { step = m_org[m_currentImage.AssemblyId][m_currentImage.PluginId][m_currentImage.StepId]; } //Retrieve the message CrmMessage message = m_org.Messages[step.MessageId]; //Verify that this step can have images if (0 == message.ImageMessagePropertyNames.Count) { //Create a list of the messages that can have images StringBuilder sb = new StringBuilder(); sb.AppendLine("Only steps registered on the following messages can have images:"); foreach (CrmMessage item in m_org.Messages.Values) { if (0 != item.ImageMessagePropertyNames.Count) { sb.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "- {0}{1}", item.Name, Environment.NewLine); } } sb.AppendLine(); sb.Append("Please select a different step and try again."); MessageBox.Show(sb.ToString(), "Invalid Step Selected", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //Start populating the image that will be used image.AssemblyId = step.AssemblyId; image.PluginId = step.PluginId; image.StepId = step.StepId; image.Attributes = crmParameters.Attributes; image.EntityAlias = txtEntityAlias.Text.Trim(); image.Name = txtName.Text.Trim(); image.MessagePropertyName = MessagePropertyNameForm.SelectMessagePropertyName(message); if (null == image.MessagePropertyName) { return; } if (chkImageTypePre.Checked && chkImageTypePost.Checked) { image.ImageType = CrmPluginImageType.Both; } else if (chkImageTypePre.Checked) { image.ImageType = CrmPluginImageType.PreImage; } else { image.ImageType = CrmPluginImageType.PostImage; } #endregion Extract Information #region Register the Image try { if (m_currentImage == null) { image.ImageId = RegistrationHelper.RegisterImage(m_org, image); List <ICrmEntity> entityList = new List <ICrmEntity>(new ICrmEntity[] { image }); OrganizationHelper.UpdateDates(m_org, entityList); step.AddImage(image); m_orgControl.AddImage(image); } else { image.ImageId = m_currentImage.ImageId; RegistrationHelper.UpdateImage(m_org, image); m_currentImage.AssemblyId = step.AssemblyId; m_currentImage.PluginId = step.PluginId; m_currentImage.StepId = step.StepId; m_currentImage.ImageId = image.ImageId; m_currentImage.Attributes = image.Attributes; m_currentImage.EntityAlias = image.EntityAlias; m_currentImage.MessagePropertyName = image.MessagePropertyName; m_currentImage.ImageType = image.ImageType; image = m_currentImage; List <ICrmEntity> entityList = new List <ICrmEntity>(new ICrmEntity[] { image }); OrganizationHelper.UpdateDates(m_org, entityList); m_orgControl.RefreshImage(m_currentImage); } } catch (Exception ex) { ErrorMessageForm.ShowErrorMessageBox(this, "Unable to register the Image due to an error.", "Registration", ex); return; } #endregion Register the Image DialogResult = DialogResult.OK; Close(); }