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

ExtendedLookupFieldType 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

ExtendedLookupFieldType 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 ExtendedLookupFieldType.

cs

using System;
using System.Threading;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SharePointStu.LookupField.Fields
{
/// <summary>
/// Class for managing the lookup based field.
/// </summary>
public class ExtendedLookupFieldType : SPFieldLookup
{
#region Private Members

private const string THREAD_WEBSOURCEID = "WebSourceId";


private const string THREAD_LOOKUPLISTID = "LookupListId";
private const string THREAD_DISPLAYCOLUMNID = "DisplayColumnId";
private const string PROPERTY_MULTIPLE = "Mult";
private const string RELATED_FIELD_PREFIX = "r_";

#endregion Private Members

#region Constructors

public ExtendedLookupFieldType(SPFieldCollection fields, string fieldName)


: base(fields, fieldName)
{
EnsureFieldRef();
}

public ExtendedLookupFieldType(SPFieldCollection fields, string typeName, string


displayName)
: base(fields, typeName, displayName)
{
EnsureFieldRef();
}

#endregion Constructors

#region Public Properties

/// <summary>
/// Get or set the Id of the SPWeb object containing the lookup list.
/// </summary>
public Guid WebSourceId
{
get { return GetThreadDataValueAsGuid(THREAD_WEBSOURCEID) != Guid.Empty ?
GetThreadDataValueAsGuid(THREAD_WEBSOURCEID) : LookupWebId; }
set { SetThreadDataValue(THREAD_WEBSOURCEID, value); }
}

/// <summary>
/// Get or set the Id of the lookup list.
/// </summary>
public Guid LookupListId
{
get { return GetThreadDataValueAsGuid(THREAD_LOOKUPLISTID) != Guid.Empty ?
GetThreadDataValueAsGuid(THREAD_LOOKUPLISTID) : new Guid(LookupList); }
set { SetThreadDataValue(THREAD_LOOKUPLISTID, value); }
}

/// <summary>
/// Get or set the Id of the field to use as the display column.
/// </summary>
public Guid DisplayColumnId
{
get { return GetThreadDataValueAsGuid(THREAD_DISPLAYCOLUMNID) != Guid.Empty ?
GetThreadDataValueAsGuid(THREAD_DISPLAYCOLUMNID) : new Guid(LookupField); }
file:///C:/Users/amal.bahrini/Desktop/Data cloud/ExtendedLookupFieldType.cs 1/5
26/11/2024 11:13 ExtendedLookupFieldType.cs
set { SetThreadDataValue(THREAD_DISPLAYCOLUMNID, value); }
}

/// <summary>
/// Get the Id of the field to use as the value column.
/// </summary>
public Guid ValueColumnId
{
get { return SPBuiltInFieldId.ID; }
}

/// <summary>
/// Get the internal name of the related data field.
/// </summary>
public string RelatedFieldName { get { return string.Concat(RELATED_FIELD_PREFIX,
Id.ToString("N")).Substring(0, 32); } }

#endregion Public Properties

#region Overridden Methods

public override BaseFieldControl FieldRenderingControl


{
get
{
BaseFieldControl fieldControl = new ExtendedLookupFieldControl(this);
fieldControl.FieldName = InternalName;

return fieldControl;
}
}

/// <summary>
/// Called when a field is created.
/// </summary>
/// <param name="op"></param>
public override void OnAdded(SPAddFieldOptions op)
{
AddRelatedField();
base.OnAdded(op);
Update();
}

/// <summary>
/// Called when a field is deleted.
/// </summary>
public override void OnDeleting()
{
RemoveRelatedFields();
base.OnDeleting();
}

public override void Update()


{
XmlDocument document = new XmlDocument();
document.LoadXml(SchemaXml);

if (AllowMultipleValues)
{
UpdateProperty(document, PROPERTY_MULTIPLE, "TRUE");
}

UpdateProperty(document, "WebId", WebSourceId);


UpdateProperty(document, "List", LookupListId);
UpdateProperty(document, "ShowField", DisplayColumnId);

SchemaXml = document.OuterXml;

base.Update();

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


26/11/2024 11:13 ExtendedLookupFieldType.cs
FreeThreadData();
}

#endregion Overridden Methods

#region Threading

private Guid GetThreadDataValueAsGuid(string propertyName)


{
LocalDataStoreSlot slot = Thread.GetNamedDataSlot(propertyName);
object dataSlot = Thread.GetData(slot);

if (dataSlot != null)
{
return (Guid)dataSlot;
}

return Guid.Empty;
}

private void SetThreadDataValue(string propertyName, object value)


{
Thread.SetData(Thread.GetNamedDataSlot(propertyName), value);
}

private void FreeThreadData()


{
Thread.FreeNamedDataSlot(THREAD_WEBSOURCEID);
Thread.FreeNamedDataSlot(THREAD_LOOKUPLISTID);
Thread.FreeNamedDataSlot(THREAD_DISPLAYCOLUMNID);
}

#endregion Threading

#region Private Methods

private void EnsureFieldRef()


{
if (Id == Guid.Empty || ParentList == null)
return;

if (!ParentList.Fields.ContainsFieldWithStaticName(RelatedFieldName))
return;

XmlDocument doc = new XmlDocument();


doc.LoadXml(SchemaXml);
if (doc.DocumentElement == null)
return;

XmlNodeList nodes =
doc.DocumentElement.SelectNodes(string.Format("//FieldRefs/FieldRef[@Name='{0}']",
RelatedFieldName));
if (nodes != null && nodes.Count > 0)
return;

bool allowUnsafeUpdates = ParentList.ParentWeb.AllowUnsafeUpdates;


try
{
ParentList.ParentWeb.AllowUnsafeUpdates = true;
AssociateInternalName(RelatedFieldName);
base.Update();
}
finally
{
ParentList.ParentWeb.AllowUnsafeUpdates = allowUnsafeUpdates;
}
}

private void AddRelatedField()

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


26/11/2024 11:13 ExtendedLookupFieldType.cs
{
if (SPContext.Current == null || SPContext.Current.List == null)
{
return;
}

SPList list = SPContext.Current.List;


SPFieldCollection fields = list.Fields;

SPFieldUrl field = new SPFieldUrl(fields, "RelatedLookupDataFieldType",


RelatedFieldName);
field.ReadOnlyField = true;
field.ShowInDisplayForm = false;
field.ShowInEditForm = false;
field.ShowInNewForm = false;
field.ShowInListSettings = false;
field.ShowInViewForms = false;
field.ShowInVersionHistory = false;
field.Hidden = true;

string internalName = fields.Add(field);


list.Update();

AssociateInternalName(internalName);
}

private void RemoveRelatedFields()


{
if (SPContext.Current == null || SPContext.Current.List == null)
{
return;
}

if (SPContext.Current.List.Fields.ContainsField(RelatedFieldName))
{
SPField field =
SPContext.Current.List.Fields.GetFieldByInternalName(RelatedFieldName);
field.ReadOnlyField = false;
field.Hidden = false;
field.Update();
SPContext.Current.List.Fields.Delete(field.InternalName);
}
}

private void AssociateInternalName(string fieldInternalName)


{
XmlDocument doc = new XmlDocument();

doc.LoadXml(SchemaXml);
XmlElement fieldRefs = doc.CreateElement("FieldRefs");
fieldRefs.InnerXml = string.Format("<FieldRef Name=\"{0}\" Explicit=\"TRUE\" />",
fieldInternalName);
doc.ChildNodes[0].AppendChild(fieldRefs);

SchemaXml = doc.InnerXml;
}

private void UpdateProperty(XmlDocument document, string name, object value)


{
if (document == null || document.DocumentElement == null)
return;

XmlAttribute attribute = document.DocumentElement.Attributes[name] ??


document.CreateAttribute(name);
attribute.Value = value.ToString();

document.DocumentElement.Attributes.Append(attribute);
}

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


26/11/2024 11:13 ExtendedLookupFieldType.cs
#endregion Private Methods
}
}

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

You might also like