Retrieving All Record owned by other Team Members – Part II

Retrieving All Record owned by other Team Members – Part II

In a recent post we showed how to retrieve all the team members of a particular record. This was different then using the default My Team Comments view, and we showed the relationship that is needed.

In this post we will show how to implement that same query using C# code. This can be used from within a plugin or a custom application that uses the CRM SDK.

        
      internal EntityCollection RetrieveMyTeamComments(Guid userId)
        {
            ColumnSet commentsColumns = new ColumnSet("bgx_commentid", "bgx_autonumber", "bgx_categoryid", "bgx_comment", "bgx_response", "ownerid", "owninguser", "bgx_submissionid");

            EntityCollection rc = new EntityCollection();
            QueryExpression query = new QueryExpression()
            {
                EntityName = "bgx_comment",
                ColumnSet = commentsColumns,
                Distinct = true,
                Criteria =
                {
                    Conditions = 
                    {
                       new ConditionExpression("statecode", ConditionOperator.Equal, 0)
                    }
                },
                LinkEntities = 
                {
                   new LinkEntity()
                   {
                       LinkToEntityName = "systemuser",
                       LinkToAttributeName = "systemuserid",
                       LinkFromEntityName = "scag_comment",
                       LinkFromAttributeName = "owninguser",
                       JoinOperator = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                       EntityAlias = "user",
                       LinkEntities = 
                       {
                           new LinkEntity()
                           {
                               LinkToEntityName="teammembership",
                               LinkToAttributeName="systemuserid",
                               LinkFromEntityName = "systemuser",
                               LinkFromAttributeName="systemuserid",
                               JoinOperator = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                               EntityAlias= "teammembership1",
                               LinkEntities = 
                               {
                                   new LinkEntity()
                                   {
                                       LinkToEntityName="team",
                                       LinkToAttributeName="teamid",
                                       LinkFromEntityName = "teammembership",
                                       LinkFromAttributeName="teamid",
                                       JoinOperator = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                                       EntityAlias= "team",
                                       LinkEntities = 
                                       {
                                           new LinkEntity()
                                           {
                                               LinkToEntityName="teammembership",
                                               LinkToAttributeName="teamid",
                                               LinkFromEntityName = "team",
                                               LinkFromAttributeName="teamid",
                                               JoinOperator = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                                               EntityAlias= "teammembership2",
                                               LinkEntities = 
                                               {
                                                   new LinkEntity()
                                                   {
                                                       LinkToEntityName = "systemuser",
                                                       LinkToAttributeName = "systemuserid",
                                                       LinkFromEntityName = "teammembership",
                                                       LinkFromAttributeName = "systemuserid",
                                                       JoinOperator = Microsoft.Xrm.Sdk.Query.JoinOperator.Inner,
                                                       EntityAlias = "systemuser",
                                                       LinkCriteria = new FilterExpression
                                                       {
                                                           FilterOperator = LogicalOperator.And,
                                                           Conditions = 
							   {
                                                               new ConditionExpression("systemuserid", ConditionOperator.Equal, userId)
                                                           }
                                                        }
                                                   }
                                               }
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
                }
            };

            EntityCollection results = new EntityCollection();
           
            try
            {
                results = service.RetrieveMultiple(query);
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                Console.WriteLine(ex.Message);
            }

            return results;

        }

You will notice the difference between the code and the advanced find from the previous view is that in the view there is only three levels of relationships, while in the code there are 5.
The reason for this is that in the advanced find view, we do not need to create the relationship with the teammembership entity.