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(); txtFilter.Text = lookupFieldWithPicker.AdditionalFilter; } } lookupWeb = WebsForCurrentUser.Where(w=>w.ID==new Guid(dropDownListLookupWeb.SelectedValue)).First(); // If the field is being edited, verify that the lookup list still exists //Cheat for provision lookup field by programing. Guid lookupListId = Guid.NewGuid(); if (lookupFieldWithPicker != null) { if (!lookupFieldWithPicker.LookupList.IsGuid()) { var lkList = lookupWeb.GetList(lookupFieldWithPicker.LookupList); lookupListId = lkList.ID; lookupFieldWithPicker.UpdateLookupReferences(lookupWeb, lkList); } else { lookupListId = new Guid(lookupFieldWithPicker.LookupList); } if (lookupFieldWithPicker.LookupList != null && !lookupFieldWithPicker.LookupList.IsGuid()) { var lkList = lookupWeb.GetList(lookupFieldWithPicker.LookupList); lookupListId = lkList.ID; lookupFieldWithPicker.UpdateLookupReferences(lookupWeb, lkList); } else { lookupListId = new Guid(lookupFieldWithPicker.LookupList); } if (lookupFieldWithPicker != null && lookupFieldWithPicker.LookupList.ToUpper() != "SELF" && lookupWeb.Lists.OfType<SPList>().Where(l => l.ID == lookupListId) .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) == lookupListId).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"; } } }
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(); lookupFieldWithPicker.AdditionalFilter = txtFilter.Text; //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; }