CRM Expert Logo

CRM Expert Code Examples

Complete example of generated code to retrieve relationships (teams for users with known last name):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Standard Microsoft Dynamics CRM SDK references
using Microsoft.Xrm.Sdk;            // EntityCollection
using Microsoft.Xrm.Sdk.Query;      // QueryExpression
using Microsoft.Xrm.Sdk.Client;     // OrganizationServiceProxy 
using Microsoft.Xrm.Sdk.Metadata;   // EntityMetadata
using Microsoft.Xrm.Sdk.Messages;   // RetrieveAllEntitiesRequest

namespace CrmCustom
{
    public partial class LoadData
    {
        /// <summary>
        /// Retrieve Teams based on Users with known Lastname
        /// </summary>
        public static EntityCollection LoadValues_TeamsForUsers(
            OrganizationServiceProxy proxy, string sLastname)
        {
            return LoadValues_TwoLinks(proxy, "team",
                "teammembership", "teamid", "teamid",
                "systemuser", "systemuserid", "systemuserid",
                "lastname", sLastname);
        }

        /****  GENERIC RELATIONSHIP FUNCTIONS BELOW  ****/
        /****  GENERIC RELATIONSHIP FUNCTIONS BELOW  ****/
        /****  GENERIC RELATIONSHIP FUNCTIONS BELOW  ****/

        /// <summary>
        /// Retrieve related entities within one link.
        /// Case-insensitive match on sKnownValue
        /// </summary>
        public static EntityCollection LoadValues_OneLink(OrganizationServiceProxy proxy, string sQueryEntity, 
            string sLinkEntity, string sLinkFrom, string sLinkTo,
            string sKnownAttribute, string sKnownValue)
        {
            QueryExpression query = new QueryExpression(sQueryEntity);
            query.ColumnSet = new ColumnSet(true); // true = all columns
            LinkEntity link = query.AddLink(sLinkEntity, sLinkFrom, sLinkTo);
            link.LinkCriteria.AddCondition(new ConditionExpression(sKnownAttribute, ConditionOperator.Equal, sKnownValue));
            EntityCollection col = proxy.RetrieveMultiple(query);
            return col;
        }

        /// <summary>
        /// Retrieve related entities within two links
        /// Case-insensitive match on sKnownValue
        /// </summary>
        public static EntityCollection LoadValues_TwoLinks(OrganizationServiceProxy proxy,string sQueryEntity, 
            string sLinkEntity1, string sLinkFrom1, string sLinkTo1,
            string sLinkEntity2, string sLinkFrom2, string sLinkTo2,
            string sKnownAttribute, string sKnownValue)
        {
            QueryExpression query = new QueryExpression(sQueryEntity);
            query.ColumnSet = new ColumnSet(true); // true = all columns
            LinkEntity link1 = query.AddLink(sLinkEntity1, sLinkFrom1, sLinkTo1);
            LinkEntity link2 = link1.AddLink(sLinkEntity2, sLinkFrom2, sLinkTo2);
            link2.LinkCriteria.AddCondition(new ConditionExpression(sKnownAttribute, ConditionOperator.Equal, sKnownValue));
            EntityCollection col = proxy.RetrieveMultiple(query);
            return col;
        }
    }
}