Adding Relationship Programatically In MSCRM

I faced a Peculiar Behaviour in MSCRM. If I am trying to add Relationship through UI Its not allowing me and CRM is getting Time out.

Then I have increase the Time out of CRM but no luck

So I have started writing Codes for Creating Relationship

For Creating Relation ship in CRM We need to use Metadata Service

There are two types of Relationship We can create

1)One To Many and

2) Many to Many

Creating One to Many Relationship

public static void createOneToManyTest(MetadataService metadataService)
{
try
{

OneToManyMetadata oneToMany = new OneToManyMetadata();

oneToMany.SchemaName = “new_relationship_name”;

oneToMany.ReferencedEntity = “systemuser”;

//Side A

oneToMany.ReferencedAttribute = “systemuserid”;

oneToMany.ReferencingEntity = “incident”;

oneToMany.ReferencingAttribute = “incidentid”;

oneToMany.RelationshipType = EntityRelationshipType.OneToMany;

oneToMany.SchemaName = “new_systemuser_incident_ResolvedBy”;

LocLabel locLabel = new LocLabel();
locLabel.Label = “Resolved By”;
locLabel.LanguageCode = new CrmNumber(1033);

LocLabel[] lstLable = new LocLabel[1];
lstLable[0] = locLabel;

oneToMany.AssociatedMenuGroup = new CrmAssociatedMenuGroup(AssociatedMenuGroup.Details);
oneToMany.AssociatedMenuLabel = new CrmLabel(lstLable, new LocLabel(“Resolved By”, new CrmNumber(1033)));
oneToMany.AssociatedMenuOrder = new CrmNumber(15001);

oneToMany.AssociatedMenuBehavior = new CrmAssociatedMenuBehavior(AssociatedMenuBehavior.UseLabel);

oneToMany.CascadeAssign = new CrmCascadeType(CascadeType.NoCascade);
oneToMany.CascadeDelete = new CrmCascadeType(CascadeType.RemoveLink);
oneToMany.CascadeMerge = new CrmCascadeType(CascadeType.NoCascade);
oneToMany.CascadeReparent = new CrmCascadeType(CascadeType.NoCascade);
oneToMany.CascadeShare = new CrmCascadeType(CascadeType.NoCascade);
oneToMany.CascadeUnshare = new CrmCascadeType(CascadeType.NoCascade);

oneToMany.IsCustomRelationship = new CrmBoolean(true);
oneToMany.IsValidForAdvancedFind = new CrmBoolean(true);
oneToMany.SecurityType = SecurityTypes.Pointer;

CreateOneToManyRequest oneToManyRequest = new CreateOneToManyRequest();

LookupAttributeMetadata lkpMeta = new LookupAttributeMetadata();
lkpMeta.EntityLogicalName = EntityName.incident.ToString();
lkpMeta.DisplayName = new CrmLabel(lstLable, new LocLabel(“Resolved By”, new CrmNumber(1033)));
lkpMeta.LogicalName = “ResolvedBy”;
lkpMeta.RequiredLevel = new CrmAttributeRequiredLevel(AttributeRequiredLevel.None);
lkpMeta.SchemaName = “new_ResolvedByid”;
oneToManyRequest.Lookup = lkpMeta;
oneToManyRequest.OneToManyRelationship = oneToMany;

metadataService.Execute(oneToManyRequest);
}
catch (SoapException e)
{

Console.WriteLine(“Error Occured:” + e.Detail.ToString());
}
catch (Exception Ex)
{
Console.WriteLine(“Error Occured:” + Ex.StackTrace.ToString());
}

}

Creating Many to Many RelationShip

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;
LocLabel locLabel = new LocLabel();
locLabel.Label = “my custom label”;
locLabel.LanguageCode = new CrmNumber(1033);

LocLabel[] lstLable = new LocLabel[1];
lstLable[0] = locLabel;

manyToMany.Entity1AssociatedMenuLabel = new CrmLabel(lstLable, new LocLabel(“my custom lable”,new CrmNumber(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 = new CrmLabel(lstLable, new LocLabel(“my custom lable”, new CrmNumber(1033)));
manyToMany.Entity2AssociatedMenuOrder = new CrmNumber();

manyToMany.Entity2AssociatedMenuOrder.Value = 15001;

CreateManyToManyRequest manyToManyRequest = new CreateManyToManyRequest();

manyToManyRequest.IntersectEntitySchemaName = manyToMany.IntersectEntityName;

manyToManyRequest.ManyToManyRelationship = manyToMany;

metadataService.Execute(manyToManyRequest);

}

Advertisements