Click here to Skip to main content
15,884,095 members
Articles / Web Development / ASP.NET

How to use SP Control (FormField) in WebPart for MOSS 2007

Rate me:
Please Sign up or sign in to vote.
1.80/5 (3 votes)
20 Oct 2009CPOL4 min read 35.5K   4   6
Use of FormField control of SharePoint to create controls in WebPart

Introduction

This article talks about a simplified way of creating controls for fields of SharePoint List in WebPart. This uses SP FormField control to create controls for SP list instead of basic ASP.NET controls. With FormField control, a developer can manage the appearance of controls as per his/her requirement very easily. The reader is expected to have sufficient knowledge of creating, deploying WebPart for SharePoint 2007. If you are new to WebPart or SharePoint, please see other articles to understand WebPart and SharePoint 2007 before reading this article.

Background

SharePoint by default provides "Add New" and "Edit" pages for SP Custom List. But it will display all controls in edit mode. Many times, you need to display page with only few column controls or editable controls depending upon business logic. To achieve this dynamic control creation, one can use WebPart and completely control display of columns. The controls for a field/column of List can be created in ASP.NET. But this means one has to write a code to:

  1. identify type of control
  2. populate control with master data like that in dropdown list
  3. set proper value from List Item to control

SharePoint provides a wrapper to various controls but even then, you need to pick a suitable control for a given field. All this means that the developer needs to write and maintain code for each field separately.

SharePoint also provides FormField control that can really help a developer to get rid of common work of selection of control, populating the control with data and allows the developer to concentrate on business logic he/she wants to put in WebPart.

Using the Code

First, add the following Microsoft.SharePoint.dll reference to your WebPart project. Include the following namespaces in your WebPart class. The language used in this article is C#.

C#
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

FormField internally uses properties of SP Field to decide which control is to be created, what data should be populated. Below are the important properties of FormField that help a developer to tie up control to specified Field and Item of the List:

ListIdThis is the GUID of List.
FieldNameThis is the internal name of Field. Using ListId and FieldName control gets necessary Field details.
ControlModeDisplay - Control is created for read only data.
New - Editable control is created with default value. Populates master data if required.
Edit - Editable control is created with value from Item of the List. Required ItemId property set.
ItemIdThis is an item id of the record in List. For Edit mode, this has to be provided so that item data value can be set to control.
IDUnique Id of the control that developer must set. This helps the developer to find control on page while retrieving data filled by user in control.
ValueThe property helps the developer to set default value or get user entered value from the control.

To create SP Controls in WebPart, first you need to get List, Fields and Item objects of SP Custom List.

C#
// Variable storing SP List Name
string mylistName = "FormField_Demo_List";
// Variable storing index of item of the list used to render the data
int itemIndex = 0;
// Get List and Item objects 
SPWeb myweb = SPContext.Current.Web;
SPList mylist = myweb.Lists[mylistName];
SPItem = mylist.GetItemById(itemIndex);
// Get field instances for which controls need to be created 
// You can use GetField or GetFieldByInternalName methods to get SPField object
SPField field1 = mylist.Fields.GetField("Field1");
SPField field2 = mylist.Fields.GetField("Field2");
SPField field3 = mylist.Fields.GetField("Field3");

To dynamically create controls, one can take leverage of SP View to maintain list of fields to be displayed in WebPart, e.g. you can create "DraftView" on list and add only editable fields to this SP View and this view can be internally used in WebPart to determine which fields should be shown in editable mode for a record in Draft status.

C#
for (int i = 0, size = formView.ViewFields.Count; i < size;)
{
    field = list.Fields.GetFieldByInternalName(formView.ViewFields[i]);
    ....
    ....// Rest of the code...
}

The next step is to create instances of FormField control for each Field of the List and add it to WebPart's Control collection. For each FormField control, you have to set ControlMode and other properties. The following method helps to achieve the same.

C#
public FormField GetFormFieldControl
    (Guid listId, SPItem item, SPField field, SPControlMode mode, object defaultValue)
{
    FormField formField = new FormField();
    formField.ListId = listId; 
    formField.FieldName = field.InternalName;
    formField.ID = string.Format("Field_{0}", 
	field.InternalName); // Unique Id to identify control on page
    formField.ControlMode = mode; //SPControlMode.Display || 
		SPControlMode.New || SPControlMode.Edit

    // Tieup with Item
    if (item != null)
    {
        formField.ItemId = item.ID;
    }
    // Assign CssClass required in case of Note and Text type fields
    if (field.Type == SPFieldType.Note || field.Type == SPFieldType.Text)
    {
        formField.CssClass = "ms-short";
    }

    // You can provide customized default value for New item
    if (defaultValue != null)
    {
        formField.Value = defaultValue;
    }

    return formField;
}

The FormField control will render the necessary control on page and the user can see the controls just like default New and Edit page controls. To retrieve data/value entered by user in the control, you can use the following methods:

C#
public object GetSharePointControlValue(ControlCollection Controls, SPField field)
{
    string fieldName = string.Format("Field_{0}", field.InternalName);
    Control returnObject = null;

    if (Controls != null)
    {
        foreach (Control control in Controls)
        {
            returnObject = FindControlRecursive(control, fieldName);
            if (returnObject != null)
                break;
        }
    }

    if (returnObject != null && returnObject is FormField)
    {
        return ((FormField)returnObject).Value;
    }

    return null;
}
public Control FindControlRecursive(Control Root, string Id)
{
    if (Root.ID == Id)
        return Root;
    Control FoundCtl = null;
    foreach (Control Ctl in Root.Controls)
    {
        FoundCtl = FindControlRecursive(Ctl, Id);
        if (FoundCtl != null)
            return FoundCtl;
    }

    return null;
}

The following code updates the value in List Item:

C#
item[field.InternalName] = this.GetSharePointControlValue(this.Page.Controls, field);
// You can write additional logic to update item field data 
// with pre-defined values as per business logic.
// Finally you have to call Update() method to save data in List  
item.Update();

Sometimes it's a good idea to use customised display of data in table cell instead of using FormField to display content in read only mode. This can be achieved using GetFieldValueAsHtml() method of SPField that returns HTML tags for the data of the given field of item.

C#
TableCell cell = new TableCell();
cell.VerticalAlign = VerticalAlign.Top;
cell.CssClass = "ms-formbody";

if (item[field.InternalName] != null)
{
    string data = field.GetFieldValueAsHtml(item[field.InternalName]);
    if (false == string.IsNullOrEmpty(data))
    {
        cell.Text = data;
    }
    else
    {
        cell.Text = "[Unable to render data]";
    }
}
else
{
    cell.Text = " ";
}

Points of Interest

FormField provides a powerful way of representing SharePoint controls on page. You can easily configure it to take leverage of SharePoint styles and formats. You can now concentrate on business logic and forget about control creation activities. It's always interesting to see text in Value property for complex fields like People/Group, Lookup, Radio, etc.

History

VersionDescription
1.0Initial version
1.1New instance creation for SPWeb removed
Item object retrieved using GetItemId method

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Infosys Technologies Ltd.
India India
The author is Technical Architect and has experience in architecture, design and development of Web applications in ASP.Net along with SQL Server 2005. He has also worked on MOSS 2007.

Comments and Discussions

 
QuestionSP2010 and BCS fields Pin
senglory24-Feb-13 10:41
senglory24-Feb-13 10:41 
Questionpermissions issue Pin
JamieScheitel30-Sep-10 5:12
JamieScheitel30-Sep-10 5:12 
GeneralMy vote of 1 Pin
itsm5-Aug-10 1:00
itsm5-Aug-10 1:00 
vnnv
GeneralSetting Value into Form field - ControlMode(Display) Pin
Sanket Shah Info28-Oct-09 5:54
Sanket Shah Info28-Oct-09 5:54 
GeneralMy vote of 1 Pin
jainraj26-Oct-09 20:30
jainraj26-Oct-09 20:30 
GeneralA few comments Pin
Andi Fandrich20-Oct-09 22:13
professionalAndi Fandrich20-Oct-09 22:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.