Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

How to delete entity and its dependencies programmatically?

My client have asked me to delete a custom entity and its dependencies (which prevents deletion of this entity) programmatically.

I have retrieved the dependencies of the custom entity using RetrieveDependenciesForDeleteRequest. This request is giving me the collection of dependencies, which must be deleted before the deletion of that custom entity.

                RetrieveDependenciesForDeleteResponse resp =
                    (RetrieveDependenciesForDeleteResponse) service.Execute(req);

                //A more complete report requires more code
                foreach (Entity dependencyEntity in resp.EntityCollection.Entities)
                {
                    service.Delete(d.LogicalName, dependencyEntity.Id);
                }

Now the issue I am facing here is that dependencyEntity.Id is always empty GUID, instead of valid GUID.

Can some one help me how to achieve this functionality?

Upvotes: 2

Views: 5718

Answers (1)

Henk van Boeijen
Henk van Boeijen

Reputation: 7948

The Entity items in the response merely act as data transport objects. They are not real entities. What you are looking for is the attribute named "dependentcomponentobjectid" (this is a Guid type, not an EntityReference). Attribute "dependentcomponenttype" (OptionSetValue) gives you a clue about the type of the component you are dealing with.

Note that a dependentcomponent can in turn be a required component other components are depending on. (E.g. a workflow depends on a workflow activity, which in turn depends on a plugin assembly.) So, a robust removal tool would need to follow a recursive strategy.

Upvotes: 2

Related Questions