Thursday, 25 February 2010

How to Add Current Item Field Values to DataFormWebPart as a parameter

How to Add Current Item Field Values to DataFormWebPart as a parameter

One of my colleague had to implement a page which should give master detail functionality. So the requirement was displaying related items in the display form page. So she placed two webparts one for current list item (master) and one for related items (details). As it was display form (DispForm.aspx) the only she had was ItemID which is in the querystring. But it was not enough, she wanted it to pass One of the current item field value to filter related Items List. There is no option in the DataFormWebPart to get that. Therefore she asked me if there is any workaround for this.

I did couple of investigation but could not find anything. Then I produced the following solution.

Basicly you place the following class into your project. That is a replacement for DataFormWebPart which allows you to set current Item Field Values as parameters.

* Deploy this class with your DLL into GAC
* Add this as a safe control into web.config
* Open your page do the following modifications

Place this to your page and change assembly, namespace with yours.

<%@ Register Tagprefix="ExWebPart" Namespace="Sobiens.SharePoint.WebParts" Assembly="Sobiens.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e76709a445fee1d6" %>

Goto DataFormWebPart tag and change it to <ExWebPart:EnhancedDataFormWebPart
Finally change your parameterbinding

<ParameterBinding Name="Country" Location="None" DefaultValue="currentitemvalue(Country)" />

This will set Parameter named Country to current item Country field's value.

Hope this helps.



public class EnhancedDataFormWebPart : DataFormWebPart
{
XmlDocument _ParameterBindingsXmlDocument = null;
XmlDocument ParameterBindingsXmlDocument
{
get
{
if (_ParameterBindingsXmlDocument == null)
{
_ParameterBindingsXmlDocument = new XmlDocument();
_ParameterBindingsXmlDocument.LoadXml("" + this.ParameterBindings + "");
}
return _ParameterBindingsXmlDocument;
}
}

XmlNode _ParameterBindingsXmlNode = null;
XmlNode ParameterBindingsXmlNode
{
get
{
if (_ParameterBindingsXmlNode == null)
{
_ParameterBindingsXmlNode = ParameterBindingsXmlDocument.SelectSingleNode("/ParameterBindings");
}
return _ParameterBindingsXmlNode;
}
}


private void SetParameterBinding(string key, string value)
{
XmlNode node = ParameterBindingsXmlNode.SelectSingleNode("/ParameterBindings/ParameterBinding[@Name = '" + key + "']");
if (node == null)
{
XmlNode parameterBindingNode = ParameterBindingsXmlDocument.CreateElement("ParameterBinding");

XmlAttribute nameAtrribute = ParameterBindingsXmlDocument.CreateAttribute("Name");
nameAtrribute.Value = key;
XmlAttribute locationAttribute = ParameterBindingsXmlDocument.CreateAttribute("Location");
locationAttribute.Value = "None";
XmlAttribute defaultValueAttribute = ParameterBindingsXmlDocument.CreateAttribute("DefaultValue");
defaultValueAttribute.Value = value;

parameterBindingNode.Attributes.Append(nameAtrribute);
parameterBindingNode.Attributes.Append(locationAttribute);
parameterBindingNode.Attributes.Append(defaultValueAttribute);

ParameterBindingsXmlNode.AppendChild(parameterBindingNode);
}
else
{
node.Attributes["DefaultValue"].Value = value;
}
}

private void ReplaceFormParameterBindings()
{
foreach (XmlNode node in ParameterBindingsXmlNode.ChildNodes)
{
string defaultValue=node.Attributes["DefaultValue"].Value;
if (defaultValue.ToLower().StartsWith("currentitemvalue(") == true)
{
int startIndex = defaultValue.IndexOf("(");
int lastIndex = defaultValue.IndexOf(")");
string fieldName = node.Attributes["DefaultValue"].Value.Substring(startIndex+1, lastIndex - startIndex-1);
if (SPContext.Current.Item[fieldName] != null)
{
node.Attributes["DefaultValue"].Value = SPContext.Current.Item[fieldName].ToString();
}
}
}
}

private void SetPropertiesAsParameterBindings()
{
ReplaceFormParameterBindings();
this.ParameterBindings = ParameterBindingsXmlNode.InnerXml;
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
SetPropertiesAsParameterBindings();
}
}

No comments:

Post a Comment