public void addOptionRemoveRequest(int value, List <CrmOperation> crmOp) { DeleteOptionValueRequest deleteOptionValueRequest; if (optionMetadata.optionData.IsGlobal.Value) { deleteOptionValueRequest = new DeleteOptionValueRequest { OptionSetName = optionMetadata.optionData.Name, Value = value }; } else { deleteOptionValueRequest = new DeleteOptionValueRequest { AttributeLogicalName = optionMetadata.parentAttribute.LogicalName, EntityLogicalName = optionMetadata.parentAttribute.EntityLogicalName, Value = value }; } crmOp.Add(new CrmOperation(CrmOperation.CrmOperationType.create, CrmOperation.CrmOperationTarget.optionSet, deleteOptionValueRequest, string.Format("Delete OptionSet {0}", value))); }
public void DeleteOptionValue(bool globalOptionSet, string attributeName, string entityName, int optionValue) { if (globalOptionSet) { DeleteOptionValueRequest deleteOptionValueRequest = new DeleteOptionValueRequest { OptionSetName = attributeName, Value = optionValue }; service.Execute(deleteOptionValueRequest); } else { // Create a request. DeleteOptionValueRequest insertOptionValueRequest = new DeleteOptionValueRequest { AttributeLogicalName = attributeName, EntityLogicalName = entityName, Value = optionValue }; service.Execute(insertOptionValueRequest); } }
/// <summary> /// Get a request to add to the batch. /// </summary> /// <param name="option">The option for the request.</param> /// <param name="operation">The operation we are doing.</param> /// <returns>The request created.</returns> private static OrganizationRequest GetRequest(EntityItem option, Operation operation) { OrganizationRequest request = null; switch (operation) { case Operation.Insert: request = new InsertOptionValueRequest { OptionSetName = option.Parent.GlobalName, EntityLogicalName = option.Parent.Global ? null : option.Parent.Parent.LogicalName, AttributeLogicalName = option.Parent.Global ? null : option.Parent.LogicalName, Value = option.Value, Label = option.Label, Description = option.Description }; break; case Operation.Update: request = new UpdateOptionValueRequest { OptionSetName = option.Parent.GlobalName, EntityLogicalName = option.Parent.Global ? null : option.Parent.Parent.LogicalName, AttributeLogicalName = option.Parent.Global ? null : option.Parent.LogicalName, Value = option.Value, Label = option.Label, Description = option.Description }; break; case Operation.Delete: request = new DeleteOptionValueRequest { OptionSetName = option.GlobalName, EntityLogicalName = option.Global ? null : option.Parent.LogicalName, AttributeLogicalName = option.Global ? null : option.LogicalName, Value = option.Value }; break; default: break; } return(request); }
/// <summary> /// Global OptionSet의 Options 삭제 /// </summary> /// <param name="optionSetName"></param> /// <param name="optionValue"></param> public void DeleteGlobalOptions(string optionSetName, int optionValue) { try { DeleteOptionValueRequest deleteOptionValueRequest = new DeleteOptionValueRequest { OptionSetName = optionSetName, Value = optionValue }; _orgService.Execute(deleteOptionValueRequest); } catch (Exception) { throw; } }
/// <summary> /// Deletes/Reverts the record that was created/changed for this sample. /// <param name="prompt">Indicates whether to prompt the user to delete /// the records created in this sample.</param> /// </summary> public void DeleteRequiredRecords(bool prompt) { bool deleteRecords = true; if (prompt) { Console.WriteLine( "\nDo you want these entity records to be deleted? (y/n)"); String answer = Console.ReadLine(); deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y")); } if (deleteRecords) { #region How to delete attribute //<snippetWorkWithAttributes9> // Delete all attributes created for this sample. foreach (AttributeMetadata anAttribute in addedAttributes) { // Create the request object DeleteAttributeRequest deleteAttribute = new DeleteAttributeRequest { // Set the request properties EntityLogicalName = Contact.EntityLogicalName, LogicalName = anAttribute.SchemaName }; // Execute the request _serviceProxy.Execute(deleteAttribute); } //</snippetWorkWithAttributes9> #endregion How to delete attribute #region How to remove inserted status value //<snippetWorkWithAttributes10> // Delete the newly inserted status value. // Create the request object DeleteOptionValueRequest deleteRequest = new DeleteOptionValueRequest { AttributeLogicalName = "statuscode", EntityLogicalName = Contact.EntityLogicalName, Value = _insertedStatusValue }; // Execute the request _serviceProxy.Execute(deleteRequest); Console.WriteLine("Deleted all attributes created for this sample."); //</snippetWorkWithAttributes10> #endregion How to remove inserted status value #region Revert the changed state value //<snippetWorkWithAttributes11> // Revert the state value label from Open to Active. // Create the request. UpdateStateValueRequest revertStateValue = new UpdateStateValueRequest { AttributeLogicalName = "statecode", EntityLogicalName = Contact.EntityLogicalName, Value = 1, Label = new Label("Active", _languageCode) }; // Execute the request. _serviceProxy.Execute(revertStateValue); // NOTE: All customizations must be published before they can be used. _serviceProxy.Execute(new PublishAllXmlRequest()); Console.WriteLine( "Reverted {0} state attribute of {1} entity from 'Open' to '{2}'.", revertStateValue.AttributeLogicalName, revertStateValue.EntityLogicalName, revertStateValue.Label.LocalizedLabels[0].Label ); //</snippetWorkWithAttributes11> #endregion Revert the changed state value } }
public void CreateOrUpdateSharedOptionSet(string schemaName, string displayName, IEnumerable<KeyValuePair<int, string>> options) { if (SharedOptionSetExists(schemaName)) { var optionSetMetadata = GetSharedOptionSet(schemaName); optionSetMetadata.DisplayName = new Label(displayName, 1033); var updateOptionSetRequest = new UpdateOptionSetRequest { OptionSet = optionSetMetadata }; Execute(updateOptionSetRequest); if (options.Any()) { var existingOptions = OptionSetToKeyValues(optionSetMetadata.Options); var optionSet = options.ToArray(); foreach (var option in existingOptions) { if (!optionSet.Any(o => o.Key == option.Key)) { var request = new DeleteOptionValueRequest { OptionSetName = schemaName, Value = option.Key }; Execute(request); } else if (optionSet.Any(o => o.Key == option.Key && o.Value != option.Value)) { var newValue = optionSet.Single(o => o.Key == option.Key); var request = new UpdateOptionValueRequest { OptionSetName = schemaName, Value = option.Key, Label = new Label(newValue.Value, 1033) }; Execute(request); } } foreach (var option in optionSet) { if (!existingOptions.Any(o => o.Key == option.Key)) { var request = new InsertOptionValueRequest { OptionSetName = schemaName, Value = option.Key, Label = new Label(option.Value, 1033) }; Execute(request); } } } } else { var optionSetMetadata = new OptionSetMetadata(); optionSetMetadata.Name = schemaName; optionSetMetadata.DisplayName = new Label(displayName, 1033); optionSetMetadata.IsGlobal = true; optionSetMetadata.Options.AddRange( options.Select(o => new OptionMetadata(new Label(o.Value, 1033), o.Key)).ToList()); var request = new CreateOptionSetRequest {OptionSet = optionSetMetadata}; Execute(request); } RefreshSharedOptionValues(schemaName); }
public void UpdatePicklistOptions(string fieldName, string recordType, IEnumerable<KeyValuePair<int, string>> optionSet) { if (optionSet.Any()) { var existingOptions = GetPicklistKeyValues(recordType, fieldName); var itemUpdated = false; foreach (var option in existingOptions) { if (!optionSet.Any(o => o.Key == option.Key)) { var request = new DeleteOptionValueRequest { AttributeLogicalName = fieldName, EntityLogicalName = recordType, Value = option.Key }; Execute(request); itemUpdated = true; } else if (optionSet.Any(o => o.Key == option.Key && o.Value != option.Value)) { var newValue = optionSet.Single(o => o.Key == option.Key); var request = new UpdateOptionValueRequest { AttributeLogicalName = fieldName, EntityLogicalName = recordType, Value = option.Key, Label = new Label(newValue.Value, 1033) }; Execute(request); itemUpdated = true; } } foreach (var option in optionSet) { if (!existingOptions.Any(o => o.Key == option.Key)) { var request = new InsertOptionValueRequest { AttributeLogicalName = fieldName, EntityLogicalName = recordType, Value = option.Key, Label = new Label(option.Value, 1033) }; Execute(request); itemUpdated = true; } } if (itemUpdated) RefreshFieldMetadata(fieldName, recordType); } }
private void SaveGlobalOptionSet() { WorkAsync("Saving global optionset...", (w, e) => { var req = new RetrieveOptionSetRequest() { Name = ((Item)cboOptionSets.SelectedItem).Value, }; var resp = (RetrieveOptionSetResponse)Service.Execute(req); OptionSetMetadata md = (OptionSetMetadata)resp.OptionSetMetadata; //foreach (var option in md.Options) //{ // var delReq = new DeleteOptionValueRequest() // { // OptionSetName = md.Name, // Value = option.Value.Value // }; // Service.Execute(delReq); //} var data = (BindingList<Item>)dgvOptions.DataSource; var execMultiReq = new ExecuteMultipleRequest() { Settings = new Microsoft.Xrm.Sdk.ExecuteMultipleSettings() { ContinueOnError = true, ReturnResponses = false }, Requests = new Microsoft.Xrm.Sdk.OrganizationRequestCollection() }; foreach (var item in data) { var exists = (from o in md.Options where o.Value.Value == int.Parse(item.Value) select true).FirstOrDefault(); if (exists) { var upReq = new UpdateOptionValueRequest() { OptionSetName = md.Name, Label = new Microsoft.Xrm.Sdk.Label(item.Name, 1033), Value = int.Parse(item.Value) }; execMultiReq.Requests.Add(upReq); } else { var addReq = new InsertOptionValueRequest() { OptionSetName = md.Name, Label = new Microsoft.Xrm.Sdk.Label(item.Name, 1033), Value = int.Parse(item.Value) }; execMultiReq.Requests.Add(addReq); } } foreach (var item in md.Options) { var exists = (from d in data where int.Parse(d.Value) == item.Value.Value select true).FirstOrDefault(); if (!exists) { var delReq = new DeleteOptionValueRequest() { OptionSetName = md.Name, Value = item.Value.Value }; execMultiReq.Requests.Add(delReq); } } Service.Execute(execMultiReq); w.ReportProgress(50, "Publishing global optionset..."); PublishXmlRequest pxReq1 = new PublishXmlRequest { ParameterXml = String.Format("<importexportxml><optionsets><optionset>{0}</optionset></optionsets></importexportxml>", md.Name) }; Service.Execute(pxReq1); }, e => { }, e => { SetWorkingMessage(e.UserState.ToString()); } ); }
/// <summary> /// Deletes any entity records that were created for this sample. /// <param name="prompt">Indicates whether to prompt the user to /// delete the records created in this sample.</param> /// </summary> public void DeleteRequiredRecords(bool prompt) { bool deleteRecords = true; if (prompt) { Console.WriteLine("\nDo you want these entity records deleted? (y/n)"); String answer = Console.ReadLine(); deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y")); } if (deleteRecords) { #region How to delete a option from a option set // Use the DeleteOptionValueRequest message // to remove the newly inserted label. DeleteOptionValueRequest deleteOptionValueRequest = new DeleteOptionValueRequest { OptionSetName = _globalOptionSetName, Value = _insertedOptionValue }; // Execute the request. _serviceProxy.Execute(deleteOptionValueRequest); Console.WriteLine("Option Set option removed."); #endregion How to delete a option from a option set #region How to delete attribute // Create the request to see which components have a dependency on the // global option set. RetrieveDependentComponentsRequest dependencyRequest = new RetrieveDependentComponentsRequest { ObjectId = _optionSetId, ComponentType = (int)componenttype.OptionSet }; RetrieveDependentComponentsResponse dependencyResponse = (RetrieveDependentComponentsResponse)_serviceProxy.Execute( dependencyRequest); // Here you would check the dependencyResponse.EntityCollection property // and act as appropriate. However, we know there is exactly one // dependency so this example deals with it directly and deletes // the previously created attribute. DeleteAttributeRequest deleteAttributeRequest = new DeleteAttributeRequest { EntityLogicalName = Contact.EntityLogicalName, LogicalName = "sample_examplepicklist" }; _serviceProxy.Execute(deleteAttributeRequest); Console.WriteLine("Referring attribute deleted."); #endregion How to delete attribute #region How to delete global option set // Finally, delete the global option set. Attempting this before deleting // the picklist above will result in an exception being thrown. DeleteOptionSetRequest deleteRequest = new DeleteOptionSetRequest { Name = _globalOptionSetName }; _serviceProxy.Execute(deleteRequest); Console.WriteLine("Global Option Set deleted"); #endregion How to delete global option set } }
/// <summary> /// Deletes any entity records that were created for this sample. /// <param name="prompt">Indicates whether to prompt the user to /// delete the records created in this sample.</param> /// </summary> public void DeleteRequiredRecords(bool prompt) { bool deleteRecords = true; if (prompt) { Console.WriteLine("\nDo you want these entity records deleted? (y/n)"); String answer = Console.ReadLine(); deleteRecords = (answer.StartsWith("y") || answer.StartsWith("Y")); } if (deleteRecords) { #region How to delete a option from a option set //<snippetWorkwithGlobalOptionSets11> // Use the DeleteOptionValueRequest message // to remove the newly inserted label. DeleteOptionValueRequest deleteOptionValueRequest = new DeleteOptionValueRequest { OptionSetName = _globalOptionSetName, Value = _insertedOptionValue }; // Execute the request. _serviceProxy.Execute(deleteOptionValueRequest); //</snippetWorkwithGlobalOptionSets11> Console.WriteLine("Option Set option removed."); #endregion How to delete a option from a option set #region How to delete attribute //<snippetWorkwithGlobalOptionSets12> // Create the request to see which components have a dependency on the // global option set. RetrieveDependentComponentsRequest dependencyRequest = new RetrieveDependentComponentsRequest { ObjectId = _optionSetId, ComponentType = (int)componenttype.OptionSet }; RetrieveDependentComponentsResponse dependencyResponse = (RetrieveDependentComponentsResponse)_serviceProxy.Execute( dependencyRequest); // Here you would check the dependencyResponse.EntityCollection property // and act as appropriate. However, we know there is exactly one // dependency so this example deals with it directly and deletes // the previously created attribute. DeleteAttributeRequest deleteAttributeRequest = new DeleteAttributeRequest { EntityLogicalName = Contact.EntityLogicalName, LogicalName = "sample_examplepicklist" }; _serviceProxy.Execute(deleteAttributeRequest); Console.WriteLine("Referring attribute deleted."); #endregion How to delete attribute #region How to delete global option set // Finally, delete the global option set. Attempting this before deleting // the picklist above will result in an exception being thrown. //<snippetWorkwithGlobalOptionSets.DeleteOptionSetRequest> DeleteOptionSetRequest deleteRequest = new DeleteOptionSetRequest { Name = _globalOptionSetName }; _serviceProxy.Execute(deleteRequest); //</snippetWorkwithGlobalOptionSets.DeleteOptionSetRequest> //</snippetWorkwithGlobalOptionSets12> Console.WriteLine("Global Option Set deleted"); #endregion How to delete global option set } }