Ejemplo n.º 1
0
        public static DataCollection <Entity> RetrieveDependentComponents(Guid workflowId, string workflowName)
        {
            RetrieveDependentComponentsRequest request = new RetrieveDependentComponentsRequest
            {
                ObjectId      = workflowId,
                ComponentType = 29
            };

            RetrieveDependentComponentsResponse response = (RetrieveDependentComponentsResponse)Context.Service.Execute(request);
            EntityCollection result = (EntityCollection)response.Results["EntityCollection"];

            return(result.Entities);
        }
Ejemplo n.º 2
0
        [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();
            }
        }
        /// <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;
            }
        }
        /// <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
            }
        }