internal static void CheckStatusTransitions(EntityMetadata metadata, Entity newEntity, Entity prevEntity) { if (newEntity == null || prevEntity == null) { return; } if (!newEntity.Attributes.ContainsKey("statuscode") || !prevEntity.Attributes.ContainsKey("statuscode")) { return; } if (newEntity.LogicalName != prevEntity.LogicalName || newEntity.Id != prevEntity.Id) { return; } var newValue = newEntity["statuscode"] as OptionSetValue; var prevValue = prevEntity["statuscode"] as OptionSetValue; if (metadata.EnforceStateTransitions != true) { return; } OptionMetadataCollection optionsMeta = GetStatusOptionMetadata(metadata); if (!optionsMeta.Any(o => o.Value == newValue.Value)) { return; } var prevValueOptionMeta = optionsMeta.FirstOrDefault(o => o.Value == prevValue.Value) as StatusOptionMetadata; if (prevValueOptionMeta == null) { return; } var transitions = prevValueOptionMeta.TransitionData; if (transitions != null && transitions != "" && IsValidStatusTransition(transitions, newValue.Value)) { return; } throw new FaultException($"Trying to switch {newEntity.LogicalName} from status {prevValue.Value} to {newValue.Value}"); }
/// <summary> /// Remove optionSet values no longer existing in application /// </summary> /// <param name="optionSet">OptionSet to clean</param> /// <param name="omc">List of all optionsets</param> /// <param name="settings">Settings for optionSets sync</param> private void CleanOption(AttributeMetadata optionSet, OptionMetadataCollection omc, Settings settings) { var schemaName = settings.AllMetadata.First(e => e.LogicalName == optionSet.EntityLogicalName).SchemaName; var existingOptions = Service.RetrieveMultiple(new QueryExpression("gap_powerbioptionsetref") { NoLock = true, ColumnSet = new ColumnSet("gap_value"), Criteria = new FilterExpression { Conditions = { new ConditionExpression("gap_entityschemaname", ConditionOperator.Equal, schemaName), new ConditionExpression("gap_optionsetschemaname", ConditionOperator.Equal, optionSet.SchemaName), } } }).Entities; var bulkDeleteRequest = new ExecuteMultipleRequest { Settings = new ExecuteMultipleSettings { ContinueOnError = true, ReturnResponses = false }, Requests = new OrganizationRequestCollection() }; foreach (var record in existingOptions) { if (omc.Any(o => o.Value.HasValue && o.Value.Value == record.GetAttributeValue <int>("gap_value"))) { continue; } bulkDeleteRequest.Requests.Add(new DeleteRequest { Target = record.ToEntityReference() }); } Service.Execute(bulkDeleteRequest); }
private void BtnImport_OnClick(object sender, EventArgs args) { WorkAsync(new WorkAsyncInfo { Message = "Importing the option sets...", Work = (w, e) => { cmbOptionSet.Invoke((MethodInvoker) delegate { selectedAttributeLogicalName = (string)cmbOptionSet.SelectedValue; }); txtResult.Invoke((MethodInvoker) delegate { txtResult.Clear(); }); OptionMetadataCollection existingOptions = Helper.GetOptionSetEntries(Service, selectedEntityLogicalName, selectedAttributeLogicalName); ExecuteMultipleRequest multipleRequest = new ExecuteMultipleRequest(); multipleRequest.Settings = new ExecuteMultipleSettings(); multipleRequest.Settings.ContinueOnError = false; multipleRequest.Settings.ReturnResponses = true; multipleRequest.Requests = new OrganizationRequestCollection(); foreach (Model.ImportDataRow row in importData) { if (existingOptions.Any(_ => _.Value == row.Value)) { //merge List <KeyValuePair <int, string> > labels = new List <KeyValuePair <int, string> >(); OptionMetadata metadata = existingOptions.FirstOrDefault(_ => _.Value == row.Value); if (metadata.Label != null && metadata.Label.LocalizedLabels != null) { // initit labels collection with original labels foreach (LocalizedLabel item in metadata.Label.LocalizedLabels) { labels.Add(new KeyValuePair <int, string>(item.LanguageCode, item.Label)); } // override the existing labels if (!row.IsLabel1031Null() && labels.Any(_ => _.Key == 1031)) { labels.Remove(labels.First(_ => _.Key == 1031)); labels.Add(new KeyValuePair <int, string>(1031, row.Label1031)); }//if if (!row.IsLabel1033Null() && labels.Any(_ => _.Key == 1033)) { labels.Remove(labels.First(_ => _.Key == 1033)); labels.Add(new KeyValuePair <int, string>(1033, row.Label1033)); }//if if (!row.IsLabel1036Null() && labels.Any(_ => _.Key == 1036)) { labels.Remove(labels.First(_ => _.Key == 1036)); labels.Add(new KeyValuePair <int, string>(1036, row.Label1036)); }//if if (!row.IsLabel1040Null() && labels.Any(_ => _.Key == 1040)) { labels.Remove(labels.First(_ => _.Key == 1040)); labels.Add(new KeyValuePair <int, string>(1040, row.Label1040)); }//if if (!row.IsLabel3082Null() && labels.Any(_ => _.Key == 3082)) { labels.Remove(labels.First(_ => _.Key == 3082)); labels.Add(new KeyValuePair <int, string>(3082, row.Label3082)); }//if // create insert request multipleRequest.Requests.Add(Helper.CreateUpdateOptionValueRequest(Service, languageCodeOfUser, selectedEntityLogicalName, selectedAttributeLogicalName, labels, row.Value)); }//if } else { List <KeyValuePair <int, string> > labels = new List <KeyValuePair <int, string> >(); if (!row.IsLabel1031Null()) { labels.Add(new KeyValuePair <int, string>(1031, row.Label1031)); } if (!row.IsLabel1033Null()) { labels.Add(new KeyValuePair <int, string>(1033, row.Label1033)); } if (!row.IsLabel1036Null()) { labels.Add(new KeyValuePair <int, string>(1036, row.Label1036)); } if (!row.IsLabel1040Null()) { labels.Add(new KeyValuePair <int, string>(1040, row.Label1040)); } if (!row.IsLabel3082Null()) { labels.Add(new KeyValuePair <int, string>(3082, row.Label3082)); } // create insert request multipleRequest.Requests.Add(Helper.CreateInsertOptionValueRequest(Service, languageCodeOfUser, selectedEntityLogicalName, selectedAttributeLogicalName, labels, row.Value)); } }//foreach //multipleRequest.Requests.Add(new PublishAllXmlRequest()); ExecuteMultipleResponse response = (ExecuteMultipleResponse)Service.Execute(multipleRequest); txtResult.Invoke((MethodInvoker) delegate { if (!response.IsFaulted) { txtResult.Text = "Successfully performed the import - please publish the entity."; } else { foreach (ExecuteMultipleResponseItem responseItem in response.Responses) { txtResult.Text = responseItem.Fault.Message + Environment.NewLine + txtResult.Text; }//foreach } }); }, ProgressChanged = e => { }, PostWorkCallBack = e => { }, AsyncArgument = null, IsCancelable = true, MessageWidth = 340, MessageHeight = 150 }); }