public void When_delete_is_invoked_with_an_existing_entity_by_alternate_key_that_entity_is_delete_from_the_context() { var context = new XrmFakedContext(); var accountMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityMetadata(); accountMetadata.LogicalName = Account.EntityLogicalName; var alternateKeyMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata(); alternateKeyMetadata.KeyAttributes = new string[] { "AccountNumber" }; accountMetadata.SetFieldValue("_keys", new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata[] { alternateKeyMetadata }); context.InitializeMetadata(accountMetadata); //Initialize the context with a single entity var account = new Entity("account"); account.Id = Guid.NewGuid(); account.Attributes.Add("AccountNumber", 9000); context.Initialize(account); var service = context.GetOrganizationService(); var delete = new DeleteRequest { Target = new EntityReference("account", "AccountNumber", 9000) }; service.Execute(delete); Assert.True(context.Data["account"].Count == 0); }
public static void Should_Retrieve_A_Correct_Entity_By_Alternate_Key() { var fakedContext = new XrmFakedContext(); var accountMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityMetadata(); accountMetadata.LogicalName = Account.EntityLogicalName; var alternateKeyMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata(); alternateKeyMetadata.KeyAttributes = new string[] { "alternateKey" }; accountMetadata.SetFieldValue("_keys", new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata[] { alternateKeyMetadata }); fakedContext.InitializeMetadata(accountMetadata); var account = new Entity(Account.EntityLogicalName); account.Id = Guid.NewGuid(); account.Attributes.Add("alternateKey", "key"); fakedContext.Initialize(account); var fakedService = fakedContext.GetOrganizationService(); var request = new RetrieveRequest { Target = new EntityReference(Account.EntityLogicalName, "alternateKey", "key"), ColumnSet = new ColumnSet(allColumns: true) }; var retrievedAccount = (RetrieveResponse)fakedService.Execute(request); Assert.Equal(account.Id, retrievedAccount.Entity.Id); }
public void When_updating_an_entity_by_alternate_key_the_context_should_reflect_changes() { var context = new XrmFakedContext(); var service = context.GetOrganizationService(); var accountMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityMetadata(); accountMetadata.LogicalName = Account.EntityLogicalName; var alternateKeyMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata(); alternateKeyMetadata.KeyAttributes = new string[] { "AccountNumber" }; accountMetadata.SetFieldValue("_keys", new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata[] { alternateKeyMetadata }); context.InitializeMetadata(accountMetadata); var e = new Entity("account"); e["AccountNumber"] = 9000; e["name"] = "Before update"; var guid = service.Create(e); Assert.Equal(context.Data["account"][guid]["name"], "Before update"); //now update the name e = new Entity("account", "AccountNumber", 9000); e["name"] = "After update"; service.Update(e); Assert.Equal(context.Data["account"][guid]["name"], "After update"); }
internal void Parse(Microsoft.Xrm.Sdk.Metadata.EntityMetadata entity) { var doc = new XmlDocument(); doc.LoadXml(this.FormXml); this.ParseTabs(doc); this.ParseAttributes(doc, entity); }
public void An_entity_which_references_another_existent_entity_by_alternate_key_can_be_updated_when_validate_is_true() { var context = new XrmFakedContext(); context.ValidateReferences = true; IOrganizationService service = context.GetOrganizationService(); var accountMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityMetadata(); accountMetadata.LogicalName = Account.EntityLogicalName; var alternateKeyMetadata = new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata(); alternateKeyMetadata.KeyAttributes = new string[] { "alternateKey" }; accountMetadata.SetFieldValue("_keys", new Microsoft.Xrm.Sdk.Metadata.EntityKeyMetadata[] { alternateKeyMetadata }); context.InitializeMetadata(accountMetadata); var account = new Entity(Account.EntityLogicalName); account.Id = Guid.NewGuid(); account.Attributes.Add("alternateKey", "keyValue"); var account2 = new Entity(Account.EntityLogicalName); account2.Id = Guid.NewGuid(); account2.Attributes.Add("alternateKey", "keyValue2"); Entity otherEntity = new Entity("otherEntity"); otherEntity.Id = Guid.NewGuid(); otherEntity["new_accountId"] = new EntityReference("account", "alternateKey", "keyValue"); context.Initialize(new List <Entity>() { account, account2, otherEntity }); var entityToUpdate = new Entity("otherEntity") { Id = otherEntity.Id, ["new_accountId"] = new EntityReference("account", "alternateKey", "keyValue2") }; service.Update(entityToUpdate); Entity otherEntityInContext = service.Retrieve("otherEntity", otherEntity.Id, new ColumnSet(true)); Assert.Equal(((EntityReference)otherEntityInContext["new_accountId"]).Id, account2.Id); }
private void ParseAttributes(XmlDocument doc, Microsoft.Xrm.Sdk.Metadata.EntityMetadata entity) { Dictionary <string, Attribute> attributes = new Dictionary <string, Attribute>(); foreach (XmlNode xmlNode in doc.GetElementsByTagName("control")) { string controlName = xmlNode.Attributes["id"].Value.ToString().ToLower(); var fieldName = controlName.ToLower(); if (fieldName.StartsWith("header_") || fieldName.StartsWith("footer_")) { fieldName = fieldName.Substring(7); } var attr = entity.Attributes.Where(r => r.LogicalName == fieldName).SingleOrDefault(); if (attr != null) { if (attributes.TryGetValue(fieldName, out Attribute at)) { var nextControls = new List <Control>(); nextControls.AddRange(at.Controls); nextControls.Add(new Control { Name = controlName }); at.Controls = nextControls.ToArray(); } else { var next = new Attribute { LogicalName = attr.LogicalName, DisplayName = attr.DisplayName?.UserLocalizedLabel?.Label, Type = attr.AttributeType, Controls = new Control[] { new Control { Name = controlName } } }; attributes.Add(fieldName, next); } } } this.Attributes = attributes.Values.ToArray(); }
/// <summary> /// Method to create the cell template. /// </summary> /// <param name="cellNode"></param> /// <param name="entityMetadata"></param> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="ApplicationException"></exception> public override ICellTemplate CreateTemplate(System.Xml.Linq.XNode cellNode, Microsoft.Xrm.Sdk.Metadata.EntityMetadata entityMetadata) { if (!IsInitialized) { throw new InvalidOperationException("Factory is not initialized."); } ICellMetadata cellMetadata; if (MetadataFactory is FormXmlCellMetadataFactory) { var formMetadataFactory = MetadataFactory as FormXmlCellMetadataFactory; cellMetadata = formMetadataFactory.GetMetadata(cellNode, entityMetadata, LanguageCode, ToolTipEnabled, RecommendedFieldsRequired, ValidationText, WebFormMetadata, ForceAllFieldsRequired, EnableValidationSummaryLinks, ValidationSummaryLinkText, Messages, BaseOrganizationLanguageCode); } else { cellMetadata = MetadataFactory.GetMetadata(cellNode, entityMetadata, LanguageCode); } return(CreateCellTemplate(cellMetadata, entityMetadata)); }
protected virtual CodeGenerationType GetTypeForAttributeInternal(Microsoft.Xrm.Sdk.Metadata.EntityMetadata entityMetadata, Microsoft.Xrm.Sdk.Metadata.AttributeMetadata attributeMetadata, IServiceProvider services) { return(DefaultService.GetTypeForAttribute(entityMetadata, attributeMetadata, services)); }
public CodeGenerationType GetTypeForOptionSet(Microsoft.Xrm.Sdk.Metadata.EntityMetadata entityMetadata, Microsoft.Xrm.Sdk.Metadata.OptionSetMetadataBase optionSetMetadata, IServiceProvider services) { return(GetTypeForOptionSetInternal(entityMetadata, optionSetMetadata, services)); }
public CodeGenerationType GetTypeForEntity(Microsoft.Xrm.Sdk.Metadata.EntityMetadata entityMetadata, IServiceProvider services) { return(GetTypeForEntityInternal(entityMetadata, services)); }
public CodeGenerationType GetTypeForAttribute(Microsoft.Xrm.Sdk.Metadata.EntityMetadata entityMetadata, Microsoft.Xrm.Sdk.Metadata.AttributeMetadata attributeMetadata, IServiceProvider services) { return(GetTypeForAttributeInternal(entityMetadata, attributeMetadata, services)); }
protected virtual CodeGenerationType GetTypeForOptionSetInternal(Microsoft.Xrm.Sdk.Metadata.EntityMetadata entityMetadata, Microsoft.Xrm.Sdk.Metadata.OptionSetMetadataBase optionSetMetadata, IServiceProvider services) { return(DefaultService.GetTypeForOptionSet(entityMetadata, optionSetMetadata, services)); }
/// <summary> /// Method to create cell template /// </summary> /// <param name="cellMetadata"></param> /// <param name="entityMetadata"></param> /// <returns></returns> /// <exception cref="ApplicationException"></exception> public virtual ICellTemplate CreateCellTemplate(ICellMetadata cellMetadata, Microsoft.Xrm.Sdk.Metadata.EntityMetadata entityMetadata) { var fields = Fields.Where(f => f.AttributeName == cellMetadata.DataFieldName).ToList(); if (fields.Count() > 1) { throw new ApplicationException("Only one CrmEntityFormViewField with an AttributeName {0} can be specified.".FormatWith(cellMetadata.DataFieldName)); } var field = fields.FirstOrDefault(); var formXmlCellMetadata = cellMetadata as FormXmlCellMetadata; if (formXmlCellMetadata != null) { formXmlCellMetadata.FormView = FormView; if (formXmlCellMetadata.FormView != null && formXmlCellMetadata.FormView.Mode.HasValue) { if (formXmlCellMetadata.FormView.Mode.Value == FormViewMode.Insert && !formXmlCellMetadata.IsValidForCreate) { formXmlCellMetadata.ReadOnly = true; } if (formXmlCellMetadata.FormView.Mode.Value == FormViewMode.Edit && !formXmlCellMetadata.IsValidForUpdate) { formXmlCellMetadata.ReadOnly = true; } } if ((formXmlCellMetadata.IsNotesControl || formXmlCellMetadata.IsActivityTimelineControl) && (FormView.Mode == FormViewMode.Edit || FormView.Mode == FormViewMode.ReadOnly)) { return(new NotesControlTemplate(formXmlCellMetadata, ContextName, CellBindings, formXmlCellMetadata.IsActivityTimelineControl)); } if (formXmlCellMetadata.IsWebResource) { if (formXmlCellMetadata.WebResourceIsHtml) { return(new HtmlWebResourceControlTemplate(formXmlCellMetadata, ContextName, RenderWebResourcesInline)); } if (formXmlCellMetadata.WebResourceIsImage) { return(new ImageWebResourceControlTemplate(formXmlCellMetadata)); } } if (formXmlCellMetadata.IsSharePointDocuments) { return(new SharePointDocumentsControlTemplate(formXmlCellMetadata, ContextName, CellBindings)); } if (formXmlCellMetadata.IsSubgrid && (FormView.Mode == FormViewMode.Edit || FormView.Mode == FormViewMode.ReadOnly)) { return(new SubgridControlTemplate(formXmlCellMetadata, ContextName, CellBindings)); } if (formXmlCellMetadata.IsQuickForm) { return(new CrmQuickFormControlTemplate(formXmlCellMetadata, ContextName, CellBindings)); } if (formXmlCellMetadata.HasAttributeType("lookup")) { if (formXmlCellMetadata.LookupTargets.Length >= 1 && formXmlCellMetadata.LookupTargets[0] == "subject") { return(new SubjectControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if ((field != null && field.Type == FieldType.Dropdown) || formXmlCellMetadata.ControlStyle == WebForms.WebFormMetadata.ControlStyle.LookupDropdown) { return(new LookupControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } return(new ModalLookupControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (formXmlCellMetadata.HasAttributeType("customer")) { if ((field != null && field.Type == FieldType.Dropdown) || formXmlCellMetadata.ControlStyle == WebForms.WebFormMetadata.ControlStyle.LookupDropdown) { return(new CustomerControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } return(new ModalLookupControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (formXmlCellMetadata.IsFullNameControl) { return(new FullNameControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings, entityMetadata, FormView.Mode)); } if (formXmlCellMetadata.IsAddressCompositeControl) { return(new AddressCompositeControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings, entityMetadata, FormView.Mode)); } if (ShowOwnerFields.GetValueOrDefault(false) && formXmlCellMetadata.HasAttributeType("owner")) { if ((field != null && field.Type == FieldType.Dropdown) || formXmlCellMetadata.ControlStyle == WebForms.WebFormMetadata.ControlStyle.LookupDropdown) { return(new LookupControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } return(new ModalLookupControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("string")) { switch (cellMetadata.Format) { case "Email": return(new EmailStringControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); case "Url": return(new UrlStringControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); case "TickerSymbol": return(new TickerSymbolStringControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); case "TextArea": return(new MemoControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); default: return(new StringControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } } if (cellMetadata.HasAttributeType("picklist")) { // determine if the picklist should be a multi-select picklist var picklistvaluesfield = entityMetadata.Attributes.FirstOrDefault(a => a.LogicalName == string.Format("{0}selectedvalues", cellMetadata.DataFieldName)); if (picklistvaluesfield != null) { return(new MultiSelectPicklistControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } return(new PicklistControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("boolean")) { return(formXmlCellMetadata.ControlStyle == WebForms.WebFormMetadata.ControlStyle.MultipleChoice ? new MultipleChoiceControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings) : new BooleanControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("memo")) { return(new MemoControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("decimal")) { return(new DecimalControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("datetime")) { return(new DateTimeControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("double")) { return(new DoubleControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("integer")) { switch (cellMetadata.Format) { case "Duration": return(new DurationControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); case "Language": return(new LanguageControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); case "TimeZone": return(new TimeZoneControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); default: if (formXmlCellMetadata.ControlStyle == WebForms.WebFormMetadata.ControlStyle.RankOrderAllowTies || formXmlCellMetadata.ControlStyle == WebForms.WebFormMetadata.ControlStyle.RankOrderNoTies) { return(new RankOrderControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (formXmlCellMetadata.ControlStyle == WebForms.WebFormMetadata.ControlStyle.ConstantSum) { return(new ConstantSumControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (formXmlCellMetadata.ControlStyle == WebForms.WebFormMetadata.ControlStyle.StackRank) { return(new StackRankControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } return(new IntegerControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } } if (cellMetadata.HasAttributeType("state")) { return(new StateControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("status")) { return(new StatusReasonControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("money")) { return(new MoneyControlTemplate(field, formXmlCellMetadata, ValidationGroup, CellBindings, ContextName)); } } else { if (cellMetadata.HasAttributeType("string")) { return(string.Equals("email", cellMetadata.Format, StringComparison.InvariantCultureIgnoreCase) ? new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.EmailStringControlTemplate(cellMetadata, ValidationGroup, CellBindings) : new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.StringControlTemplate(cellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("picklist")) { return(new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.PicklistControlTemplate(cellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("boolean")) { return(new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.BooleanControlTemplate(cellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("memo")) { return(new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.MemoControlTemplate(cellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("datetime")) { return(new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.DateTimeControlTemplate(cellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("integer")) { return(new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.IntegerControlTemplate(cellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("money")) { return(new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.MoneyControlTemplate(cellMetadata, ValidationGroup, CellBindings)); } if (cellMetadata.HasAttributeType("datetime")) { return(new Microsoft.Xrm.Portal.Web.UI.CrmEntityFormView.DateTimeControlTemplate(cellMetadata, ValidationGroup, CellBindings)); } } if (!string.IsNullOrEmpty(cellMetadata.AttributeType) && EnableUnsupportedFields) { return(new UnsupportedControlTemplate(cellMetadata, ValidationGroup, CellBindings, EnableUnsupportedFields)); } return(new EmptyCellTemplate(cellMetadata)); }
protected virtual CodeGenerationType GetTypeForEntityInternal(Microsoft.Xrm.Sdk.Metadata.EntityMetadata entityMetadata, IServiceProvider services) { return(_defaultService.GetTypeForEntity(entityMetadata, services)); }