protected override void CreateChildControls() { LookupFieldWithPicker lookupFieldPicker = (LookupFieldWithPicker)this.Field; SPWeb web = Web.Site.OpenWeb(lookupFieldPicker.LookupWebId); SPList lookupList = web.Lists[new Guid(lookupFieldPicker.LookupList)]; SPField lookupField = null; try { lookupField = lookupList.Fields.GetFieldByInternalName(lookupFieldPicker.LookupField); } catch { //field has been deleted, fallback is the id field web = Web.Site.OpenWeb(lookupFieldPicker.LookupWebId); lookupField = lookupList.Fields[SPBuiltInFieldId.ID]; this.List.ParentWeb.AllowUnsafeUpdates = true; lookupFieldPicker = (LookupFieldWithPicker)this.List.Fields[Field.Id]; lookupFieldPicker.LookupField = lookupField.InternalName; lookupFieldPicker.Update(true); } this.lookupEditor = new LookupFieldWithPickerEntityEditor(); this.lookupEditor.CustomProperty = new LookupFieldWithPickerPropertyBag(lookupFieldPicker.LookupWebId, lookupList.ID, lookupField.Id, lookupFieldPicker.SearchableFields, lookupFieldPicker.MaxSearchResults, lookupFieldPicker.EntityEditorRows).ToString(); this.lookupEditor.MultiSelect = lookupFieldPicker.AllowMultipleValues; this.Controls.Add(lookupEditor); base.CreateChildControls(); }
private void SetFieldControlValue(object value) { this.lookupEditor.Entities.Clear(); ArrayList list = new ArrayList(); LookupFieldWithPicker lookupFieldPicker = (LookupFieldWithPicker)this.Field; if (this.ControlMode == SPControlMode.New && lookupEditor.Entities.Count == 0 && String.IsNullOrEmpty(lookupFieldPicker.CustomDefaultValue) == false) { string strValue = ParseDefaultValue(lookupFieldPicker.CustomDefaultValue); if (strValue == null) { return; } PickerEntity defaultEntity = this.lookupEditor.ValidateEntity(new PickerEntity() { Key = strValue, DisplayText = strValue }); if (defaultEntity != null) { list.Add(defaultEntity); } } else { if (value == null || value.ToString() == "") { return; } SPFieldLookupValue lookupValue = value as SPFieldLookupValue; PickerEntity entity = this.lookupEditor.GetEntityById(lookupValue.LookupId); if (entity != null) { list.Add(entity); } } this.lookupEditor.UpdateEntities(list); }
public void OnSaveChange(SPField field, bool isNewField) { lookupList = lookupWeb.Lists[new Guid(dropDownListLookupList.SelectedValue)]; lookupFieldWithPicker = field as LookupFieldWithPicker; if (isNewField) { lookupFieldWithPicker.LookupWebId = lookupWeb.ID; lookupFieldWithPicker.LookupList = lookupList.ID.ToString(); } lookupFieldWithPicker.LookupField = dropDownListLookupField.SelectedValue; lookupFieldWithPicker.AllowMultipleValues = checkBoxListMultiLookup.Checked; lookupFieldWithPicker.UnlimitedLengthInDocumentLibrary = true; if (radioButtonListRelationContraints.SelectedValue == "None") { lookupFieldWithPicker.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.None; } else if (radioButtonListRelationContraints.SelectedValue == "Restrict") { lookupFieldWithPicker.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict; lookupFieldWithPicker.Indexed = true; } else if (radioButtonListRelationContraints.SelectedValue == "Cascade") { lookupFieldWithPicker.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Cascade; lookupFieldWithPicker.Indexed = true; } lookupFieldWithPicker.SearchableFields = checkBoxListSearchableColumns.Items.OfType <ListItem>().Where(i => i.Selected).Select(i => i.Value).ToList(); lookupFieldWithPicker.TempDependentLookups = checkBoxListProjectedColumns.Items.OfType <ListItem>().Where(i => i.Selected).Select(i => i.Value).ToList(); //Hack: At this stage the control is not able to persist custom properties. Cache the current instance of the field. This instance will be needed later in the request cycle in the OnAdded and OnUpdated events HttpContext.Current.Items[typeof(LookupFieldWithPicker).Name] = lookupFieldWithPicker; }
public void InitializeWithField(SPField field) { lookupFieldWithPicker = field as LookupFieldWithPicker; //HACK: Delete dependent lookup fields when the delete button has been clicked if (lookupFieldWithPicker != null && Page.Request["__EVENTTARGET"] != null && Page.Request["__EVENTTARGET"].Contains("onetidDeleteItem")) { List <string> dependentFieldNames = lookupFieldWithPicker.GetDependentLookupInternalNames(); foreach (string dependentFieldName in dependentFieldNames) { lookupFieldWithPicker.fields.Delete(dependentFieldName); } } // It is not supported to change the lookup web or lookup list after the field has been created if (lookupFieldWithPicker != null) { dropDownListLookupWeb.Enabled = false; dropDownListLookupList.Enabled = false; } // If the field is being edited, verify that the lookup web still exists if (lookupFieldWithPicker != null && WebsForCurrentUser.Where(w => w.ID == lookupFieldWithPicker.LookupWebId) .Count() == 0) { //TODO: Extract the error message into a ressource file exceptions.Add(new FileNotFoundException("Either you don't have permissions to the lookup site or the site has been deleted")); return; } if (!IsPostBack) { // Fill the webs intially FillWebDropDown(); // Select Web if (lookupFieldWithPicker == null) { dropDownListLookupWeb.SelectedValue = SPContext.Current.Web.ID.ToString(); } else { dropDownListLookupWeb.SelectedValue = lookupFieldWithPicker.LookupWebId.ToString(); } } lookupWeb = WebsForCurrentUser.Where(w => w.ID == new Guid(dropDownListLookupWeb.SelectedValue)).First(); // If the field is being edited, verify that the lookup list still exists if (lookupFieldWithPicker != null && lookupFieldWithPicker.LookupList.ToUpper() != "SELF" && lookupWeb.Lists.OfType <SPList>().Where(l => l.ID == new Guid(lookupFieldWithPicker.LookupList)) .Count() == 0) { //TODO: Extract the error message into a ressource file exceptions.Add(new FileNotFoundException("Either you don't have permissions to the lookup list or the list has been deleted")); return; } if (!IsPostBack) { // Fill Lists FillLookupListDropDown(); //Select List if (lookupFieldWithPicker == null) { dropDownListLookupList.SelectedIndex = 0; } else { dropDownListLookupList.Items.OfType <ListItem>().Where(i => new Guid(i.Value) == new Guid(lookupFieldWithPicker.LookupList)).First().Selected = true; } } if (!IsPostBack) { lookupList = lookupWeb.Lists[new Guid(dropDownListLookupList.SelectedValue)]; FillLookupFieldDropDown(); FillSearchableColumnsCheckBoxList(); FillProjectionColumnsCheckBoxList(); if (lookupFieldWithPicker == null) { dropDownListLookupField.SelectedIndex = 0; } else { // verify that the main dependent field has not been deleted. if (dropDownListLookupField.Items.OfType <ListItem>().Where(i => i.Value == lookupFieldWithPicker.LookupField).Count() > 0) { //set the main dependent field as configured dropDownListLookupField.SelectedValue = lookupFieldWithPicker.LookupField; labelLookupFieldError.Visible = false; } else { labelLookupFieldError.Visible = true; } // select dependent fields List <string> dependentFieldNames = lookupFieldWithPicker.GetDependentLookupInternalNames(); foreach (string dependentFieldName in dependentFieldNames) { SPFieldLookup dependentField = (SPFieldLookup)lookupFieldWithPicker.fields.GetFieldByInternalName(dependentFieldName); ListItem item = checkBoxListProjectedColumns.Items.OfType <ListItem>().Where( i => i.Value == dependentField.LookupField).FirstOrDefault(); if (item != null) { item.Selected = true; } } //select searchable fields List <string> searchableFields = lookupFieldWithPicker.SearchableFields; foreach (string searchableFieldName in searchableFields) { ListItem item = checkBoxListSearchableColumns.Items.OfType <ListItem>().Where( i => i.Value == searchableFieldName).FirstOrDefault(); if (item != null) { item.Selected = true; } } // select multi lookup checkBoxListMultiLookup.Checked = lookupFieldWithPicker.AllowMultipleValues; } } if (!IsPostBack && lookupFieldWithPicker != null) { if (lookupFieldWithPicker.RelationshipDeleteBehavior == SPRelationshipDeleteBehavior.None) { radioButtonListRelationContraints.SelectedValue = "None"; } else if (lookupFieldWithPicker.RelationshipDeleteBehavior == SPRelationshipDeleteBehavior.Restrict) { radioButtonListRelationContraints.SelectedValue = "Restrict"; } else if (lookupFieldWithPicker.RelationshipDeleteBehavior == SPRelationshipDeleteBehavior.Cascade) { radioButtonListRelationContraints.SelectedValue = "Cascade"; } } }