Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is it possible to display the FieldRenderingControl of a FormField without providing a ListId?

I'm creating a dynamic form based off of a content type. Most of the time the data from this form will be stored into a SharePoint list but the business requirements state that it needs to be able to save the data into an external database and bypass SharePoint completely.

I'm able to create the form by looping through all of the SPFields in the SPContentType object. However, in order to render the control, I need to provide a list ID. I can hack a solution by providing a dummy list ID but would like to make it as clean as possible.

Below is the code I'm using in a visual web part.

public partial class vwpDynamicFormCtrl : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Gets the CT data
        string CTId = "0x010099999";
        string listGUID = "99999999-9999-9999-9999-999999999999";

        using (SPWeb web = SPContext.Current.Web)
        {
            SPList list = web.Lists[new Guid(listGUID)];
            SPContentType oCT = web.AvailableContentTypes[new SPContentTypeId(CTId)];

            //Loops through CT Fields and Generates a Form
            foreach (SPField oSPF in oCT.Fields)
            {
                //Create TR
                TableRow oTR = new TableRow();
                TableCell oTCLabel = new TableCell();
                TableCell oTCField = new TableCell();

                //Populate TC Label
                oTCLabel.Text = oSPF.Title;

                ////Populate TC Field
                BaseFieldControl bfc = oSPF.FieldRenderingControl;
                bfc.FieldName = oSPF.InternalName;
                bfc.ControlMode = SPControlMode.Display;
                bfc.ListId = list.ID; //<<< CAN I DO WITHOUT THIS???
                oTCField.Controls.Add(bfc);

                //Populate TR and Table
                oTR.Cells.Add(oTCLabel);
                oTR.Cells.Add(oTCField);
                tblForm.Rows.Add(oTR);
            }
        }
    }
}
share|improve this question
add comment

2 Answers

First off: Madness!

Now that that's out of the way: It seems you're using SPControlMode.Display (weirdly, for an edit form), which will mean the BaseFieldControl is trying to display an item's value as if it was on the Display Item page. This means it needs the list to get the data.

Changing it to SPControlMode.New (the control in Edit mode with no value) may bypass the code that uses the list to fetch the value, allowing the ListId property to not be set.

share|improve this answer
add comment
up vote 0 down vote accepted

After coming back to this several times, I have not found a solution for this. There is a very narrow use case for using CFTs on a custom form. Understandably they were constructed to depend on a list instance when rendering the UI.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.