SharePoint 2010 Access other fields of a form within a custom fieldtype

As you might have noticed, i already wrote some posts about custom fieldtypes. But i didn’t wrote about this: How can you access the other fields and its values and maybe change them or use them within your custom fieldtype code?

UPDATE: watch this post

My posts about custom fieldtypes:

I luckily found this great post. The explanation is really good. So i put it in my code and it worked. I just changed the code a little bit.



//Function to interact with other fields
protected BaseFieldControl GetFieldControlByName(String fieldNameToSearch)
{
String iteratorId = GetIteratorByFieldControl(this).ClientID;
foreach (IValidator validator in Page.Validators)
{
if (validator is BaseFieldControl)
{
BaseFieldControl baseField = (BaseFieldControl)validator;
String fieldName = baseField.FieldName;
if ((fieldName == fieldNameToSearch) &&
(GetIteratorByFieldControl(baseField).ClientID == iteratorId))
{
return baseField;
}
}
}
return null;
}
//Function to search the field in the form
private ListFieldIterator GetIteratorByFieldControl(BaseFieldControl fieldControl)
{
Control iterator = this;
while (iterator != null && !(iterator is ListFieldIterator))
iterator = iterator.Parent;

return iterator as ListFieldIterator;
}

//Now writing values into the other field
TextField fieldText = (TextField)GetFieldControlByName("MeinTest");
fieldText.Value = "Wert1";


 

You should use this lines of code in your custom fieldtype, if you want to

  • fill certain values into the other fields
  • get values of certain fields
  • change values dependent on an event (e.g. button click)

In the end it could look like in the screens below.

On click “inserting values” the values Wert 1 and Wert2 will be stored into the fields which are below of the button.

And it is saving the values into the list:

Hope this helps you.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.