locked
Many to many (N:N) relationship- how to code RRS feed

  • Question

  • how i can code or associate many 2 many relationships ? need any example

    Sunday, January 4, 2009 8:47 AM

Answers

  • Hi,

     

    Use the follow code:

     

    Add a CRM web service reference: http://localhost:5555/MSCRMServices/2007/CrmService.asmx

    private void addMyRelation(Guid myEntityId)
    {

    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.AuthenticationType = 0;      

    token.OrganizationName = "MyOrgName";  

        

    CrmService service = new CrmService();   

    service.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;      

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

         
    new_MyEntity obMyEntity = (new_MyEntity)service.Retrieve(EntityName.new_MyEntity.ToString(), myEntityId, new AllColumns());

    Moniker moniker1 = new Moniker();
    moniker1.Name = "new_MyEntity ";

    moniker1.Id = new Guid(myEntityId.ToString());

           

    Moniker moniker2 = new Moniker();       

    moniker2.Name = "new_MyOtherEntity";   

    moniker2.Id = new Guid(myOtherEntityId.ToString());

           

    AssociateEntitiesRequest request = new AssociateEntitiesRequest();

           

    request.Moniker1 = moniker1;       

    request.Moniker2 = moniker2;       

    request.RelationshipName = "new_new_myEntity_new_myOtherEntity";

          

    service.Execute(request);

    }

     

    Reference post: http://forums.microsoft.com/Dynamics/ShowPost.aspx?PostID=2917965&SiteID=27

    Sunday, January 4, 2009 10:54 AM
  • See here

     

    public static void createManyToManyTest(MetadataService metadataService)

    {

    ManyToManyMetadata manyToMany = new ManyToManyMetadata();

    manyToMany.SchemaName = "new_relationship_name";

    manyToMany.IntersectEntityName = "new_intersect_name";

    //Side A

    manyToMany.Entity1LogicalName = "account";

    manyToMany.Entity1AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();

    manyToMany.Entity1AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;

    manyToMany.Entity1AssociatedMenuGroup = new CrmAssociatedMenuGroup();

    manyToMany.Entity1AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;

    manyToMany.Entity1AssociatedMenuLabel = CrmUtils.CreateSingleLabel("SIDE A pointing to Side B", 1033);

    manyToMany.Entity1AssociatedMenuOrder = new CrmNumber();

    manyToMany.Entity1AssociatedMenuOrder.Value = 15001;

    //Side B

    manyToMany.Entity2LogicalName = "contact";

    manyToMany.Entity2AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();

    manyToMany.Entity2AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;

    manyToMany.Entity2AssociatedMenuGroup = new CrmAssociatedMenuGroup();

    manyToMany.Entity2AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;

    manyToMany.Entity2AssociatedMenuLabel = CrmUtils.CreateSingleLabel("SIDE B pointing to Side A", 1033);

    manyToMany.Entity2AssociatedMenuOrder = new CrmNumber();

    manyToMany.Entity2AssociatedMenuOrder.Value = 15001;

    CreateManyToManyRequest manyToManyRequest = new CreateManyToManyRequest();

    manyToManyRequest.IntersectEntitySchemaName = manyToMany.IntersectEntityName;

    manyToManyRequest.ManyToManyRelationship = manyToMany;

    metadataService.Execute(manyToManyRequest);

    }

     

     

    Regards,

    Imran

     

    Sunday, January 4, 2009 1:19 PM
    Moderator

All replies

  • Hi,

     

    Use the follow code:

     

    Add a CRM web service reference: http://localhost:5555/MSCRMServices/2007/CrmService.asmx

    private void addMyRelation(Guid myEntityId)
    {

    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.AuthenticationType = 0;      

    token.OrganizationName = "MyOrgName";  

        

    CrmService service = new CrmService();   

    service.Url = "http://localhost:5555/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;      

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

         
    new_MyEntity obMyEntity = (new_MyEntity)service.Retrieve(EntityName.new_MyEntity.ToString(), myEntityId, new AllColumns());

    Moniker moniker1 = new Moniker();
    moniker1.Name = "new_MyEntity ";

    moniker1.Id = new Guid(myEntityId.ToString());

           

    Moniker moniker2 = new Moniker();       

    moniker2.Name = "new_MyOtherEntity";   

    moniker2.Id = new Guid(myOtherEntityId.ToString());

           

    AssociateEntitiesRequest request = new AssociateEntitiesRequest();

           

    request.Moniker1 = moniker1;       

    request.Moniker2 = moniker2;       

    request.RelationshipName = "new_new_myEntity_new_myOtherEntity";

          

    service.Execute(request);

    }

     

    Reference post: http://forums.microsoft.com/Dynamics/ShowPost.aspx?PostID=2917965&SiteID=27

    Sunday, January 4, 2009 10:54 AM
  • See here

     

    public static void createManyToManyTest(MetadataService metadataService)

    {

    ManyToManyMetadata manyToMany = new ManyToManyMetadata();

    manyToMany.SchemaName = "new_relationship_name";

    manyToMany.IntersectEntityName = "new_intersect_name";

    //Side A

    manyToMany.Entity1LogicalName = "account";

    manyToMany.Entity1AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();

    manyToMany.Entity1AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;

    manyToMany.Entity1AssociatedMenuGroup = new CrmAssociatedMenuGroup();

    manyToMany.Entity1AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;

    manyToMany.Entity1AssociatedMenuLabel = CrmUtils.CreateSingleLabel("SIDE A pointing to Side B", 1033);

    manyToMany.Entity1AssociatedMenuOrder = new CrmNumber();

    manyToMany.Entity1AssociatedMenuOrder.Value = 15001;

    //Side B

    manyToMany.Entity2LogicalName = "contact";

    manyToMany.Entity2AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();

    manyToMany.Entity2AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;

    manyToMany.Entity2AssociatedMenuGroup = new CrmAssociatedMenuGroup();

    manyToMany.Entity2AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;

    manyToMany.Entity2AssociatedMenuLabel = CrmUtils.CreateSingleLabel("SIDE B pointing to Side A", 1033);

    manyToMany.Entity2AssociatedMenuOrder = new CrmNumber();

    manyToMany.Entity2AssociatedMenuOrder.Value = 15001;

    CreateManyToManyRequest manyToManyRequest = new CreateManyToManyRequest();

    manyToManyRequest.IntersectEntitySchemaName = manyToMany.IntersectEntityName;

    manyToManyRequest.ManyToManyRelationship = manyToMany;

    metadataService.Execute(manyToManyRequest);

    }

     

     

    Regards,

    Imran

     

    Sunday, January 4, 2009 1:19 PM
    Moderator