protected virtual void RemoveTestEntity() { using (var orgService = (OrganizationServiceContext)this.GetCrmServiceProvider().GetOrganisationService()) { // Check for test entity - if it doesn't exist then create it. var retrieveEntity = new RetrieveEntityRequest(); retrieveEntity.RetrieveAsIfPublished = true; retrieveEntity.LogicalName = TestDynamicsCrmServerSyncProvider.TestEntityName; RetrieveEntityResponse retrieveEntityResponse = null; try { var entity = GetTestEntityMetadata(); } catch (Exception e) { if (e.Message.ToLower().StartsWith("could not find")) { // this means no need to remove the entity as it doesn't exist. return; } else { // This means some other error happened. // Write the error, but proceed so we can atleast still attempt to remove the entity. Console.WriteLine("error checking whether entity exists, but will still try and remove it. Message: " + e.Message); } } var request = new DeleteEntityRequest(); request.LogicalName = TestDynamicsCrmServerSyncProvider.TestEntityName; try { var response = (DeleteEntityResponse)orgService.Execute(request); if (response == null) { throw new Exception("Expected response."); } } catch (Exception e) { Console.WriteLine("Error deleting test entity metadata. " + e.Message); RetrieveDependenciesForDeleteRequest req = new RetrieveDependenciesForDeleteRequest(); req.ComponentType = (int)SolutionComponentType.Entity; //use the metadata browser or a retrieveentity request to get the MetadataId for the entity req.ObjectId = retrieveEntityResponse.EntityMetadata.MetadataId.Value; var dependenciesResponse = (RetrieveDependenciesForDeleteResponse)orgService.Execute(req); foreach (Entity item in dependenciesResponse.EntityCollection.Entities) { Console.WriteLine("Could not remove the entity because of a dependency: " + item.LogicalName + " id: " + item.Id); } } } }
internal bool Unregister(string xrmServerDetails, string entityLogicalName, Guid id, Collection <string> errors, SolutionComponentType solutionComponentType) { try { Result = false; using (xrmService = GetService(xrmServerDetails)) { try { if (solutionComponentType != SolutionComponentType.SDKMessageProcessingStepSecureConfig) { RetrieveDependenciesForDeleteRequest retrieveDependenciesForDeleteRequest = new RetrieveDependenciesForDeleteRequest(); retrieveDependenciesForDeleteRequest.ComponentType = (int)solutionComponentType; retrieveDependenciesForDeleteRequest.ObjectId = id; RetrieveDependenciesForDeleteResponse retrieveDependenciesForDeleteResponse = (RetrieveDependenciesForDeleteResponse)xrmService.Execute(retrieveDependenciesForDeleteRequest); if (retrieveDependenciesForDeleteResponse.EntityCollection.Entities.Count == 0) { xrmService.Retrieve(entityLogicalName, id, new ColumnSet(true)); xrmService.Delete(entityLogicalName, id); } else { errors.Add(solutionComponentType.ToString() + " with id = " + id.ToString() + "exists!"); } } else { xrmService.Retrieve(entityLogicalName, id, new ColumnSet(true)); xrmService.Delete(entityLogicalName, id); } } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> e) { IdString = id.ToString().Trim(); IdString = IdString.Replace("{", string.Empty); IdString = IdString.Replace("}", string.Empty); if (e.Message == entityLogicalName + " With Id = " + id.ToString().Trim() + " Does Not Exist") { errors.Add(e.Message); } else { throw; } } } Result = true; return(Result); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } }
internal bool HasDependencies(Guid webresourceId) { var request = new RetrieveDependenciesForDeleteRequest { ComponentType = SolutionComponentType.WebResource, ObjectId = webresourceId }; var response = (RetrieveDependenciesForDeleteResponse)innerService.Execute(request); return(response.EntityCollection.Entities.Count != 0); }
public static IEnumerable <Dependency> GetDependencies(IOrganizationService orgSvc, Guid globalOptionSetId) { var retrieveDependenciesForDeleteRequest = new RetrieveDependenciesForDeleteRequest { ComponentType = (int)componenttype.OptionSet, ObjectId = globalOptionSetId }; var retrieveDependenciesForDeleteResponse = (RetrieveDependenciesForDeleteResponse)orgSvc.Execute(retrieveDependenciesForDeleteRequest); return(retrieveDependenciesForDeleteResponse.EntityCollection.Entities.Select(e => e.ToEntity <Dependency>())); }
private List <Dependency> GetDependenciesForDelete(int componentType, Guid metadataId) { var request = new RetrieveDependenciesForDeleteRequest { ComponentType = componentType, ObjectId = metadataId }; var response = (RetrieveDependenciesForDeleteResponse)_service.Execute(request); var listComponent = response.EntityCollection.Entities.Select(e => e.ToEntity <Dependency>()).ToList(); return(listComponent); }
private static IEnumerable <Dependency> RetrieveSdkStepsByPluginType(IOrganizationService service, Entity pluginType) { var retrieveDependenciesForDeleteRequest = new RetrieveDependenciesForDeleteRequest { ComponentType = (int)SolutionComponent.OptionSet.ComponentType.PluginType, ObjectId = pluginType.Id }; var response = (RetrieveDependenciesForDeleteResponse)service.Execute(retrieveDependenciesForDeleteRequest); return(response.EntityCollection.Entities.Select(e => e.ToEntity <Dependency>())); }
public void DeleteDependenciesAndParent(Guid objectId, string entityLogicalName, int dependencieComponentType) { RetrieveDependenciesForDeleteRequest retrieveDependenciesForDeleteRequest = new RetrieveDependenciesForDeleteRequest() { ComponentType = dependencieComponentType, ObjectId = objectId }; var objectsToDelete = OrganizationService.Execute(retrieveDependenciesForDeleteRequest); foreach (var objectToDelete in ((EntityCollection)objectsToDelete.Results.Values.First()).Entities) { OrganizationService.Delete(SdkMessageProcessingStep.EntityLogicalName, objectToDelete.GetAttributeValue <Guid>("dependentcomponentobjectid")); } OrganizationService.Delete(entityLogicalName, objectId); }
private void DeletePluginStepsAndAssembly(Guid pluginAssemblyId) { RetrieveDependenciesForDeleteRequest retrieveDependenciesForDeleteRequest = new RetrieveDependenciesForDeleteRequest() { ComponentType = 91, ObjectId = pluginAssemblyId }; var objectsToDelete = OrganizationService.Execute(retrieveDependenciesForDeleteRequest); foreach (var objectToDelete in ((EntityCollection)objectsToDelete.Results.Values.First()).Entities) { OrganizationService.Delete(SdkMessageProcessingStep.EntityLogicalName, objectToDelete.GetAttributeValue <Guid>("dependentcomponentobjectid")); } OrganizationService.Delete(PluginAssembly.EntityLogicalName, pluginAssemblyId); }
public bool CanBeDeleted(Guid webResourceId, string name) { if (webResourceId == Guid.Empty) { throw new ArgumentNullException(nameof(webResourceId)); } var retrieveDependenciesForDeleteRequest = new RetrieveDependenciesForDeleteRequest { ComponentType = 61, // Web Resource ObjectId = webResourceId }; var retrieveDependenciesForDeleteResponse = (RetrieveDependenciesForDeleteResponse)_service.Execute(retrieveDependenciesForDeleteRequest); if (retrieveDependenciesForDeleteResponse.EntityCollection.Entities.Count > 0) { _logger.Warn($"Web Resource {name} has {retrieveDependenciesForDeleteResponse.EntityCollection.Entities.Count} dependencies and cannot be deleted. Skipping."); return(false); } return(true); }
[STAThread] // Added to support UX static void Main(string[] args) { CrmServiceClient service = null; try { service = SampleHelpers.Connect("Connect"); if (service.IsReady) { #region Sample Code #region Set up SetUpSample(service); #endregion Set up #region Demonstrate // Grab all Solution Components for a solution. QueryByAttribute componentQuery = new QueryByAttribute { EntityName = SolutionComponent.EntityLogicalName, ColumnSet = new ColumnSet("componenttype", "objectid", "solutioncomponentid", "solutionid"), Attributes = { "solutionid" }, // In your code, this value would probably come from another query. Values = { _primarySolutionId } }; IEnumerable <SolutionComponent> allComponents = service.RetrieveMultiple(componentQuery).Entities.Cast <SolutionComponent>(); foreach (SolutionComponent component in allComponents) { // For each solution component, retrieve all dependencies for the component. RetrieveDependentComponentsRequest dependentComponentsRequest = new RetrieveDependentComponentsRequest { ComponentType = component.ComponentType.Value, ObjectId = component.ObjectId.Value }; RetrieveDependentComponentsResponse dependentComponentsResponse = (RetrieveDependentComponentsResponse)service.Execute(dependentComponentsRequest); // If there are no dependent components, we can ignore this component. if (dependentComponentsResponse.EntityCollection.Entities.Any() == false) { continue; } // If there are dependencies upon this solution component, and the solution // itself is managed, then you will be unable to delete the solution. Console.WriteLine("Found {0} dependencies for Component {1} of type {2}", dependentComponentsResponse.EntityCollection.Entities.Count, component.ObjectId.Value, component.ComponentType.Value ); //A more complete report requires more code foreach (Dependency d in dependentComponentsResponse.EntityCollection.Entities) { DependencyReport(service, d); } } //Find out if any dependencies on a specific global option set would prevent it from being deleted // Use the RetrieveOptionSetRequest message to retrieve // a global option set by it's name. RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = _globalOptionSetName }; // Execute the request. RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)service.Execute( retrieveOptionSetRequest); _globalOptionSetId = retrieveOptionSetResponse.OptionSetMetadata.MetadataId; if (_globalOptionSetId != null) { //Use the global OptionSet MetadataId with the appropriate componenttype // to call RetrieveDependenciesForDeleteRequest RetrieveDependenciesForDeleteRequest retrieveDependenciesForDeleteRequest = new RetrieveDependenciesForDeleteRequest { ComponentType = (int)componenttype.OptionSet, ObjectId = (Guid)_globalOptionSetId }; RetrieveDependenciesForDeleteResponse retrieveDependenciesForDeleteResponse = (RetrieveDependenciesForDeleteResponse)service.Execute(retrieveDependenciesForDeleteRequest); Console.WriteLine(""); foreach (Dependency d in retrieveDependenciesForDeleteResponse.EntityCollection.Entities) { if (d.DependentComponentType.Value == 2)//Just testing for Attributes { String attributeLabel = ""; RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest { MetadataId = (Guid)d.DependentComponentObjectId }; RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)service.Execute(retrieveAttributeRequest); AttributeMetadata attmet = retrieveAttributeResponse.AttributeMetadata; attributeLabel = attmet.DisplayName.UserLocalizedLabel.Label; Console.WriteLine("An {0} named {1} will prevent deleting the {2} global option set.", (componenttype)d.DependentComponentType.Value, attributeLabel, _globalOptionSetName); } } } #region Clean up CleanUpSample(service); #endregion Clen up //DeleteRequiredRecords(promptForDelete); } #endregion Demonstrate #endregion Sample Code else { const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Dynamics CRM"; if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR)) { Console.WriteLine("Check the connection string values in cds/App.config."); throw new Exception(service.LastCrmError); } else { throw service.LastCrmException; } } } catch (Exception ex) { SampleHelpers.HandleException(ex); } finally { if (service != null) { service.Dispose(); } Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }
internal bool HasDependencies(Guid webresourceId) { var request = new RetrieveDependenciesForDeleteRequest { ComponentType = SolutionComponentType.WebResource, ObjectId = webresourceId }; var response = (RetrieveDependenciesForDeleteResponse)innerService.Execute(request); return response.EntityCollection.Entities.Count != 0; }
/// <summary> /// Shows how to detect dependencies that may cause a managed solution to become /// un-deletable. /// /// Get all solution components of a solution /// For each solution component, list the dependencies upon that component. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptForDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); // Call the method to create any data that this sample requires. CreateRequiredRecords(); //<snippetGetSolutionDependencies1> // Grab all Solution Components for a solution. QueryByAttribute componentQuery = new QueryByAttribute { EntityName = SolutionComponent.EntityLogicalName, ColumnSet = new ColumnSet("componenttype", "objectid", "solutioncomponentid", "solutionid"), Attributes = { "solutionid" }, // In your code, this value would probably come from another query. Values = { _primarySolutionId } }; IEnumerable<SolutionComponent> allComponents = _serviceProxy.RetrieveMultiple(componentQuery).Entities.Cast<SolutionComponent>(); foreach (SolutionComponent component in allComponents) { // For each solution component, retrieve all dependencies for the component. RetrieveDependentComponentsRequest dependentComponentsRequest = new RetrieveDependentComponentsRequest { ComponentType = component.ComponentType.Value, ObjectId = component.ObjectId.Value }; RetrieveDependentComponentsResponse dependentComponentsResponse = (RetrieveDependentComponentsResponse)_serviceProxy.Execute(dependentComponentsRequest); // If there are no dependent components, we can ignore this component. if (dependentComponentsResponse.EntityCollection.Entities.Any() == false) continue; // If there are dependencies upon this solution component, and the solution // itself is managed, then you will be unable to delete the solution. Console.WriteLine("Found {0} dependencies for Component {1} of type {2}", dependentComponentsResponse.EntityCollection.Entities.Count, component.ObjectId.Value, component.ComponentType.Value ); //A more complete report requires more code foreach (Dependency d in dependentComponentsResponse.EntityCollection.Entities) { DependencyReport(d); } } //</snippetGetSolutionDependencies1> //Find out if any dependencies on a specific global option set would prevent it from being deleted //<snippetGetSolutionDependencies8> // Use the RetrieveOptionSetRequest message to retrieve // a global option set by it's name. RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = _globalOptionSetName }; // Execute the request. RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)_serviceProxy.Execute( retrieveOptionSetRequest); _globalOptionSetId = retrieveOptionSetResponse.OptionSetMetadata.MetadataId; if (_globalOptionSetId != null) { //Use the global OptionSet MetadataId with the appropriate componenttype // to call RetrieveDependenciesForDeleteRequest RetrieveDependenciesForDeleteRequest retrieveDependenciesForDeleteRequest = new RetrieveDependenciesForDeleteRequest { ComponentType = (int)componenttype.OptionSet, ObjectId = (Guid)_globalOptionSetId }; RetrieveDependenciesForDeleteResponse retrieveDependenciesForDeleteResponse = (RetrieveDependenciesForDeleteResponse)_serviceProxy.Execute(retrieveDependenciesForDeleteRequest); Console.WriteLine(""); foreach (Dependency d in retrieveDependenciesForDeleteResponse.EntityCollection.Entities) { if (d.DependentComponentType.Value == 2)//Just testing for Attributes { String attributeLabel = ""; RetrieveAttributeRequest retrieveAttributeRequest = new RetrieveAttributeRequest { MetadataId = (Guid)d.DependentComponentObjectId }; RetrieveAttributeResponse retrieveAttributeResponse = (RetrieveAttributeResponse)_serviceProxy.Execute(retrieveAttributeRequest); AttributeMetadata attmet = retrieveAttributeResponse.AttributeMetadata; attributeLabel = attmet.DisplayName.UserLocalizedLabel.Label; Console.WriteLine("An {0} named {1} will prevent deleting the {2} global option set.", (componenttype)d.DependentComponentType.Value, attributeLabel, _globalOptionSetName); } } } //</snippetGetSolutionDependencies8> DeleteRequiredRecords(promptForDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } }