locked
Retrieving Many-to-Many Relationships RRS feed

  • Question

  • Hi,

    I have Entity A that link to Entity A (Link to itself) with Many-to-Many Relationships,
    For example: I have entity "new_role" and it's can represent a role that is stands for one role , or   it's can represent a Team that team can be 2 or more roles (Many-to-Many Relationships to the same entity).

    I  have try to retrieve (SDK) data by using the LinkFromEntityName but it's work fine jest between 2 difference entities – when I try to run the same logic to Many-to-Many Relationships to the same entity I got the Error that the attribute in the LinkFromAttributeName properties is not exists in the linkek entity

    Here is the sample from SDK:

     

    Code Snippet

    // Set up the CRM Service.

    CrmAuthenticationToken token = new CrmAuthenticationToken();

    token.AuthenticationType = 0;

    token.OrganizationName = "AdventureWorksCycle";

     

    CrmService service = new CrmService();

    service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";

    service.CrmAuthenticationTokenValue = token;

    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

     

    // Get the GUID of the current user.

    WhoAmIRequest who = new WhoAmIRequest();

    WhoAmIResponse whoResp = (WhoAmIResponse)service.Execute(who);

     

    Guid userid = whoResp.UserId;

     

    // Create a query expression.

    QueryExpression qe = new QueryExpression();

    qe.EntityName = "role";

    qe.ColumnSet = new AllColumns();

     

    // Create the link entity from role to systemuserroles.

    LinkEntity le = new LinkEntity();

    le.LinkFromEntityName = "role";

    le.LinkFromAttributeName = "roleid";

    le.LinkToEntityName = "systemuserroles";

    le.LinkToAttributeName = "roleid";

     

    LinkEntity le2 = new LinkEntity();

    le2.LinkFromEntityName = "systemuserroles";

    le2.LinkFromAttributeName = "systemuserid";

    le2.LinkToEntityName = "systemuser";

    le2.LinkToAttributeName = "systemuserid";

     

    // Create the condition to test the user ID.

    ConditionExpression ce = new ConditionExpression();

    ce.AttributeName = "systemuserid";

    ce.Operator = ConditionOperator.Equal;

    ce.Values = new object[]{userid};

     

    // Add the condition to the link entity.

    le2.LinkCriteria = new FilterExpression();

    le2.LinkCriteria.Conditions = new ConditionExpression[]{ce};

     

    // Add the from and to links to the query.

    le.LinkEntities = new LinkEntity[]{le2};

    qe.LinkEntities = new LinkEntity[]{le};

     

    // Retrieve the roles and write each one to the console.

    BusinessEntityCollection bec = service.RetrieveMultiple(qe);

    foreach (BusinessEntity e in bec.BusinessEntities)

    {

       role r = (role)e;

       Console.WriteLine(r.name.ToString());

    }

     

     

    Thanks

    ItzikBS

    Monday, September 22, 2008 6:05 AM