0% found this document useful (0 votes)
13 views

ExtendedLookupFieldEditor Cs

Uploaded by

devi das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

ExtendedLookupFieldEditor Cs

Uploaded by

devi das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

26/11/2024 11:13 ExtendedLookupFieldEditor.

cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using SharePointStu.LookupField.Code.Common;
using SharePointStu.LookupField.Common;

namespace SharePointStu.LookupField.Fields
{
/// <summary>
/// Control used for creating and editing the <see
cref="T:SharePointStu.LookupField.Fields.ExtendedLookupFieldType"/> field's settings.
/// </summary>
public class ExtendedLookupFieldEditor : UserControl, IFieldEditor
{
#region Private Members

private const string TEXT_FIELD = "Value";


private const string VALUE_FIELD = "Key";

private string _errorMsg;


private ExtendedLookupFieldType _extendedLookupFieldType;

#endregion Private Members

#region Protected Members

protected Literal litSectionTitle;


protected Literal litSectionDescription;
protected Literal litWebForList;
protected Literal litLookupList;
protected Literal litDisplayColumn;
protected Label lblSelectedWebForList;
protected Label lblSelectedLookupList;
protected Label lblSelectedDisplayColumn;
protected DropDownList ddlWebForList;
protected DropDownList ddlLookupList;
protected DropDownList ddlDisplayColumn;
protected CheckBox chkAllowMultipleItems;
protected InputFormRequiredFieldValidator validator_ddlWebForList;
protected InputFormRequiredFieldValidator validator_ddlLookupList;
protected InputFormRequiredFieldValidator validator_ddlDisplayColumn;

#endregion Protected Members

#region Initialisation

protected override void OnInit(EventArgs e)


{
base.OnInit(e);

ApplyResourceValues();
InitialiseControls();
}

#endregion Initialisation

#region IFieldEditor Members

public void InitializeWithField(SPField field)


{
_extendedLookupFieldType = field as ExtendedLookupFieldType;

if (Page.IsPostBack)
return;

file:///C:/Users/amal.bahrini/Desktop/Data cloud/ExtendedLookupFieldEditor.cs 1/5


26/11/2024 11:13 ExtendedLookupFieldEditor.cs
if (_extendedLookupFieldType != null)
{

// Only allow editing for new columns, unless the list\columns defined no longer
exist
if (_extendedLookupFieldType.WebSourceId != Guid.Empty)
{
using (SPWeb web =
SPContext.Current.Site.OpenWeb(_extendedLookupFieldType.WebSourceId))
{
lblSelectedWebForList.Text = web.Title;
lblSelectedWebForList.Visible = true;
ddlWebForList.Visible = false;
validator_ddlWebForList.Enabled = false;

SPList lookupList = (from list in web.Lists.Cast<SPList>()


where
list.ID.Equals(_extendedLookupFieldType.LookupListId)
select list).SingleOrDefault();

if (lookupList != null)
{
SetAsReadOnly(lblSelectedLookupList, lookupList.Title, ddlLookupList,
validator_ddlLookupList);

if
(lookupList.Fields.Contains(_extendedLookupFieldType.DisplayColumnId))
{
SPField displayColumn =
lookupList.Fields[_extendedLookupFieldType.DisplayColumnId];
SetAsReadOnly(lblSelectedDisplayColumn, displayColumn.Title,
ddlDisplayColumn, validator_ddlDisplayColumn);
}
else
{
SetAsNotFoundError(lblSelectedDisplayColumn, "Previously
configured display field was not found, please update.", ddlDisplayColumn);
BindDisplayColumns(lookupList);
}
}
else
{
SetAsNotFoundError(lblSelectedLookupList, "Previously configured
lookup list was not found, please update.", ddlLookupList);
BindLookupList();
}
}
}
}
}

public void OnSaveChange(SPField field, bool bNewField)


{
ExtendedLookupFieldType extendedLookupFieldType = field as ExtendedLookupFieldType;

if (DropDownListHasSelectedItem(ddlWebForList))
extendedLookupFieldType.WebSourceId = new Guid(ddlWebForList.SelectedItem.Value);
if (DropDownListHasSelectedItem(ddlLookupList))
extendedLookupFieldType.LookupListId = new Guid(ddlLookupList.SelectedItem.Value);
if (DropDownListHasSelectedItem(ddlDisplayColumn))
extendedLookupFieldType.DisplayColumnId = new
Guid(ddlDisplayColumn.SelectedItem.Value);

public bool DisplayAsNewSection


{
get
{

file:///C:/Users/amal.bahrini/Desktop/Data cloud/ExtendedLookupFieldEditor.cs 2/5


26/11/2024 11:13 ExtendedLookupFieldEditor.cs
return true;
}
}

#endregion IFieldEditor Members

#region Binding

private void BindWebForList()


{
Dictionary<Guid, string> webs =
SharePointHelper.GetListOfWebsFromWeb(SPContext.Current.Web.ParentWeb);
BindList(ddlWebForList, webs);
}

private void BindLookupList()


{
Dictionary<Guid, string> lists =
SharePointHelper.GetListsForWeb(SPContext.Current.Web.ParentWeb, SPBaseType.GenericList);
BindList(ddlLookupList, lists);
}

private void BindDisplayColumns(SPList list)


{
const string DEFAULT_COLUMN = "Title";

Dictionary<Guid, string> fields = SharePointHelper.GetFieldsForList(list,


SPFieldType.Text);
BindList(ddlDisplayColumn, fields);

SetSelectedItemForDropDownList(ddlDisplayColumn, DEFAULT_COLUMN);
}

private void BindList(DropDownList dropDownList, Dictionary<Guid, string> dataSource)


{
dropDownList.DataSource = dataSource;
dropDownList.DataTextField = TEXT_FIELD;
dropDownList.DataValueField = VALUE_FIELD;
dropDownList.DataBind();
}

private void SetSelectedItemForDropDownList(DropDownList dropDownList, string


selectedItem)
{
if (dropDownList.Items.Count == 0)
return;

ListItem listItem = dropDownList.Items.FindByText(selectedItem);


if (listItem != null)
{
dropDownList.SelectedIndex = dropDownList.Items.IndexOf(listItem);
}
else
{
dropDownList.SelectedIndex = 0;
}
}

private void DropDownList_OnDataBound(object sender, EventArgs e)


{
((DropDownList)sender).Items.Insert(0, new ListItem(string.Empty, string.Empty));
}

#endregion Binding

#region Private Methods

private void SetAsReadOnly(Label itemLabel, string itemText, DropDownList dropDownList,


InputFormRequiredFieldValidator validator)

file:///C:/Users/amal.bahrini/Desktop/Data cloud/ExtendedLookupFieldEditor.cs 3/5


26/11/2024 11:13 ExtendedLookupFieldEditor.cs
{
itemLabel.Text = itemText;
itemLabel.Visible = true;
dropDownList.Visible = false;
validator.Enabled = false;
}

private void SetAsNotFoundError(Label itemLabel, string error, DropDownList dropDownList)


{
itemLabel.Text = error;
itemLabel.CssClass = "ms-error";
itemLabel.Visible = true;
dropDownList.Enabled = true;
}

private bool DropDownListHasSelectedItem(DropDownList dropDownList)


{
return dropDownList.SelectedItem != null &&
!string.IsNullOrEmpty(dropDownList.SelectedItem.Value);
}

private void ApplyResourceValues()


{
litSectionTitle.Text = "Lookup Column Settings";
litSectionDescription.Text = "Specify the lookup web, list, and display column.";
litWebForList.Text = "Source web to get information from:";
litLookupList.Text = "Get information from:";
litDisplayColumn.Text = "In this column:";
chkAllowMultipleItems.Text = "Allow multiple values";
validator_ddlWebForList.ErrorMessage = "You must select a web as the source for the
lookup field.";
validator_ddlLookupList.ErrorMessage = "You must select a list as the source for the
lookup field.";
validator_ddlDisplayColumn.ErrorMessage = "You must select a disp;ay column from the
lookup list.";
}

private void InitialiseControls()


{
ddlWebForList.DataBound += DropDownList_OnDataBound;
ddlLookupList.DataBound += DropDownList_OnDataBound;

if (!IsPostBack)
{
BindWebForList();
ddlLookupList.Enabled = false;
ddlDisplayColumn.Enabled = false;
}
}

private void EnableColumnDropDownLists()


{
ddlDisplayColumn.Enabled = ddlLookupList.SelectedIndex > 0;
if (!ddlDisplayColumn.Enabled)
{
ddlDisplayColumn.Items.Clear();
}
}

private void DisableColumnControl()


{
lblSelectedDisplayColumn.Visible = false;
ddlDisplayColumn.Items.Clear();
ddlDisplayColumn.Enabled = false;
}

#endregion Private Methods

#region Rendering

file:///C:/Users/amal.bahrini/Desktop/Data cloud/ExtendedLookupFieldEditor.cs 4/5


26/11/2024 11:13 ExtendedLookupFieldEditor.cs

protected override void Render(HtmlTextWriter writer)


{
if (!string.IsNullOrEmpty(_errorMsg))
{
writer.Write(string.Format("<p class='ms-error'>{0}</p>", _errorMsg));
return;
}

base.Render(writer);
}

#endregion Rendering

#region Events

protected void ddlWebForListOnSelectedIndexChanged(object sender, EventArgs eventArgs)


{
DropDownList dropDownList = sender as DropDownList;

if (dropDownList.SelectedItem != null && dropDownList.SelectedItem.Value.IsGuid())


{
BindLookupList();

lblSelectedWebForList.Visible = false;
ddlLookupList.Visible = true;
ddlLookupList.Enabled = true;
lblSelectedLookupList.Visible = false;
validator_ddlLookupList.Enabled = true;
}
else
{
ddlLookupList.Items.Clear();
ddlLookupList.Enabled = false;
DisableColumnControl();
}
}

protected void ddlLookupListOnSelectedIndexChanged(object sender, EventArgs eventArgs)


{
DropDownList dropDownList = sender as DropDownList;

if (dropDownList.SelectedItem != null && dropDownList.SelectedItem.Value.IsGuid())


{
Guid webId = (DropDownListHasSelectedItem(ddlWebForList)) ? new
Guid(ddlWebForList.SelectedItem.Value) : _extendedLookupFieldType.WebSourceId;
Guid listId = new Guid(dropDownList.SelectedItem.Value);

using (SPWeb web = SPContext.Current.Site.OpenWeb(webId))


{
SPList list = web.Lists.GetList(listId, false);
BindDisplayColumns(list);
}

lblSelectedLookupList.Visible = false;
EnableColumnDropDownLists();
}
else
{
DisableColumnControl();
}
}

#endregion Events
}
}

file:///C:/Users/amal.bahrini/Desktop/Data cloud/ExtendedLookupFieldEditor.cs 5/5

You might also like