public void CreateOneToManyRelationship <TOne, TMany>(Expression <Func <TMany, EntityReference <TOne> > > lookupExpr, EntityAttributes.Metadata.AttributeRequiredLevel lookupRequiredLevel, string relationshipPrefix = "new") where TOne : CrmPlusPlusEntity, new() where TMany : CrmPlusPlusEntity, new() { Guard.This(relationshipPrefix).AgainstNullOrEmpty(); var oneEntityName = EntityNameAttribute.GetFromType <TOne>(); var manyEntityName = EntityNameAttribute.GetFromType <TMany>(); var oneDisplayName = EntityInfoAttribute.GetFromType <TOne>().DisplayName; var lookupPropertyName = PropertyNameAttribute.GetFromType(lookupExpr); var oneToManyRequest = new CreateOneToManyRequest { OneToManyRelationship = new OneToManyRelationshipMetadata { ReferencedEntity = oneEntityName, ReferencingEntity = manyEntityName, SchemaName = relationshipPrefix.EndsWith("_") ? relationshipPrefix : relationshipPrefix + "_" + oneEntityName + "_" + manyEntityName, AssociatedMenuConfiguration = new AssociatedMenuConfiguration { Behavior = AssociatedMenuBehavior.UseLabel, Group = AssociatedMenuGroup.Details, Label = oneDisplayName.ToLabel(), Order = 10000 }, CascadeConfiguration = new CascadeConfiguration { Assign = CascadeType.NoCascade, Delete = CascadeType.RemoveLink, Merge = CascadeType.NoCascade, Reparent = CascadeType.NoCascade, Share = CascadeType.NoCascade, Unshare = CascadeType.NoCascade } }, Lookup = new LookupAttributeMetadata { SchemaName = lookupPropertyName, DisplayName = (oneDisplayName + " Lookup").ToLabel(), RequiredLevel = new AttributeRequiredLevelManagedProperty(lookupRequiredLevel.ToSimilarEnum <AttributeRequiredLevel>()), Description = (oneDisplayName + " Lookup").ToLabel() } }; service.Execute(oneToManyRequest); }
public void CreateEntity <T>() where T : CrmPlusPlusEntity, new() { var entityName = EntityNameAttribute.GetFromType <T>(); var entityInfo = EntityInfoAttribute.GetFromType <T>(); var createEntityRequest = new CreateEntityRequest { Entity = new EntityMetadata { SchemaName = entityName, DisplayName = entityInfo.DisplayName.ToLabel(), DisplayCollectionName = entityInfo.PluralDisplayName.ToLabel(), Description = entityInfo.Description.ToLabel(), OwnershipType = entityInfo.OwnershipType, IsActivity = false }, PrimaryAttribute = new StringAttributeMetadata { SchemaName = entityName + "primary", RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), MaxLength = 100, FormatName = Microsoft.Xrm.Sdk.Metadata.StringFormatName.Text, DisplayName = "Primary attribute".ToLabel(), Description = string.Format("The primary attribute for the {0} entity", entityInfo.DisplayName).ToLabel() } }; var response = (CreateEntityResponse)service.Execute(createEntityRequest); var addReq = new AddSolutionComponentRequest() { ComponentType = (int)SolutionComponentTypes.Entity, ComponentId = response.EntityId, SolutionUniqueName = Solution.Name }; service.Execute(addReq); }