locked
Is there any way too see all data on all enteties an user owns. RRS feed

  • Question

  • Hi all,

    Is there any way to see all data an user owns in CRM2011?
    Maybe an custom report or view?

    Friday, April 10, 2015 9:00 AM

All replies

  • I could only think of one solution to this, a little bit of code to get a list of records a user owns. The List contains Entity objects with the Id & LogicalName. You could do this with a report too but you'll have to query every entity...

     public List<Entity> GetOwningRecords(IOrganizationService service, Guid systemUserId)
            {
                List<Entity> owningRecords = new List<Entity>();
    
                RetrieveAllEntitiesRequest retrieveAllEntitiesRequest = new RetrieveAllEntitiesRequest();
                retrieveAllEntitiesRequest.EntityFilters = EntityFilters.Entity;
                retrieveAllEntitiesRequest.RetrieveAsIfPublished = true;
                RetrieveAllEntitiesResponse retrieveAllEntitiesResponse = (RetrieveAllEntitiesResponse)service.Execute(retrieveAllEntitiesRequest);
    
                foreach (EntityMetadata entityMetadata in retrieveAllEntitiesResponse.EntityMetadata)
                {
                    if (entityMetadata.OwnershipType != OwnershipTypes.UserOwned)
                        continue;
    
                    try
                    {
                        QueryExpression query = new QueryExpression(entityMetadata.LogicalName)
                        {
                            ColumnSet = new ColumnSet("ownerid"),
                            Criteria =
                            {
                                Conditions =
                                {
                                    new ConditionExpression("ownerid",ConditionOperator.Equal, systemUserId)
                                }
                            }
                        };
    
                        EntityCollection result = service.RetrieveMultiple(query);
    
                        foreach (Entity entity in result.Entities)
                        {
                            owningRecords.Add(entity);
                        }
                    }
                    catch (FaultException<OrganizationServiceFault>)
                    {
                        Console.WriteLine(String.Format("Couldn't retrieve the records for {0}", entityMetadata.LogicalName));
                    }
                    catch (Exception)
                    {
                        Console.WriteLine(String.Format("Unexpected error for {0}", entityMetadata.LogicalName));
                    }
                }
    
                return owningRecords;
            }
    

    Hope this helps

    Friday, April 10, 2015 9:33 AM