1

I am trying to load ListFieldIterator control dynamically and it is not working as expected.

Step1: Created Application Page

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationPage1.aspx.cs" Inherits="TaskList.Layouts.Utilities.ApplicationPage1" 
DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
   <SharePoint:ListFieldIterator ID="listFieldIterator" ControlMode="New" runat="server" ></SharePoint:ListFieldIterator>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
My Application Page
</asp:Content>

Code behind

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace TaskList.Layouts.Utilities
{
    public partial class ApplicationPage1 : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (listFieldIterator.ListId == Guid.Empty)
            {
                string listName = Convert.ToString(this.Page.Request.QueryString["ListName"]);
                if (!string.IsNullOrEmpty(listName))
                {
                    listFieldIterator.ListId = SPContext.Current.Web.Lists[Convert.ToString(this.Page.Request.QueryString["ListName"])].ID;
                }


                switch (Convert.ToString(this.Page.Request.QueryString["ControlMode"]))
                {
                    case "New":
                        listFieldIterator.ControlMode = SPControlMode.New;
                        break;
                    case "Display":
                        listFieldIterator.ControlMode = SPControlMode.Display;
                        break;
                    case "Edit":
                        listFieldIterator.ControlMode = SPControlMode.Edit;
                        break;
                    default:
                        listFieldIterator.ControlMode = SPControlMode.New;
                        break;
                }
            }
        }
    }
}

I am passing two values from query string 1. ListName 2. ControlMode

Whatever value I pass it gives me below error message

[NullReferenceException: Object reference not set to an instance of an object.]
   Microsoft.SharePoint.WebControls.ListFieldIterator.CreateChildControls() +167
   System.Web.UI.Control.EnsureChildControls() +145
   Microsoft.SharePoint.WebControls.TemplateBasedControl.OnLoad(EventArgs e) +61
   System.Web.UI.Control.LoadRecursive() +65
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Control.LoadRecursive() +190
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2427

If I hard code ListID on HTML page it works fine but when I set it dinamically its giving me above mentioned error. While doing debugging I came to know that ListFieldIterator control's field collection is not being populated and because of that control is not being rendered.

2 Answers 2

1

Removing below line solved the issue.

if (listFieldIterator.ListId == Guid.Empty)

When we access property of ListFieldIterator control its Create Child control method will be called and in my code i have not set value LISTID property at that point and due to that control is throwing exception.

How silly I am... :)

0

ListFieldIterator is designed to show one item from the list, designated by the ItemId property. Set that property in addition to the ListId property.

1
  • 1
    What if I want to add new Item? Commented Apr 6, 2013 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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