自定义窗体和FieldRenderingControl出错

时间:2014-01-13 20:23:25

标签: sharepoint-2010

我们正在尝试使用字段的FieldRenderingControl创建自定义表单。在我们在不在根站点中的列表中尝试新表单之前,一切都很好。

为site /(works)中的list1新建/编辑

编辑site / sites / testsite中的list2(工作) 站点/站点/测试站点中的list2的新功能(不起作用)

我们在uls日志中看到的错误是: “列表不存在。您选择的页面包含一个不存在的列表。它可能已被其他用户删除。”

以下是我们正在使用的代码:

foreach (SPField f in listOfFields) // cType.Fields)
    {
        var field = f;

        if (item != null)
            field = item.Fields.GetFieldByInternalName(field.InternalName);

        string uniqueid = field.Id.ToString().Replace("-", "_");
        if (!(field.FieldRenderingControl is TaxonomyFieldControl))
        {
            var editControl = field.FieldRenderingControl;
            editControl.ID = "fld_" + uniqueid; // fix for Lookup picker
            editControl.FieldName = field.InternalName;

            //edit mode if id is provided
            if (item != null)
            {
                editControl.ControlMode = SPControlMode.Edit;
                editControl.ItemId = item.ID;

                var context = SPContext.GetContext(HttpContext.Current, item.ID, list.ID, web);
                editControl.RenderContext = context;
                editControl.ItemContext = context;
            }
            else
            {
                editControl.ControlMode = SPControlMode.New;

                var context = SPContext.GetContext(HttpContext.Current, list.DefaultView.ID, list.ID, web);
                editControl.RenderContext = context;
            }

            editControl.ListId = list.ID;

            this.pnlControls.Controls.Add(new System.Web.UI.WebControls.Label
            {
                ID = "lbl_" + uniqueid,
                Text = field.Title
            });
            this.pnlControls.Controls.Add(editControl);
       }
  }

如果需要,我可以提供更多代码。任何帮助将不胜感激。

感谢。

编辑 - 来自ULS日志

“列表不存在。您选择的页面包含一个不存在的列表。它可能已被其他用户删除。”

Stack Trace:
System.NullReferenceException: Object reference not set to an instance of an object.   
 at Microsoft.SharePoint.SPContext.GetContentTypeThroughQueryString(String strIdx)    
 at Microsoft.SharePoint.SPContext.get_ContentTypes()    
 at Microsoft.SharePoint.SPContext.ContentTypeInternal(String contentTypeId)    
 at Microsoft.SharePoint.SPContext.get_Fields()    
 at Microsoft.SharePoint.WebControls.FieldMetadata.get_Field()    
 at Microsoft.SharePoint.WebControls.TextField.CreateChildControls()    
 at System.Web.UI.Control.EnsureChildControls()    
 at Microsoft.SharePoint.WebControls.BaseFieldControl.OnLoad(EventArgs e)    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Control.LoadRecursive()    
 at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)cc

2 个答案:

答案 0 :(得分:0)

因为它适用于site / sites / testsite中的编辑表单,所以没有理由不在新表单中找到列表,如果可以的话,这里需要更多信息和上下文....

答案 1 :(得分:0)

原来我需要将SPControlMode.New添加到上下文以及FieldRenderingControl。

我将代码更改为以下版本,现在可以使用。

var isNewItem = item == null;

if (isNewItem) item = list.AddItem();

foreach (var f in listOfFields) // cType.Fields)
{
    var field = f;

    if (!isNewItem)
        field = item.Fields.GetFieldByInternalName(field.InternalName);

    string uniqueid = field.Id.ToString().Replace("-", "_");
    if (!(field.FieldRenderingControl is TaxonomyFieldControl))
    {
        var editControl = field.FieldRenderingControl;
        editControl.ID = "fld_" + uniqueid; // fix for Lookup picker
        editControl.FieldName = field.InternalName;

        //edit mode if id is provided
        if (!isNewItem)
        {
            editControl.ControlMode = SPControlMode.Edit;
            editControl.ItemId = item.ID;

            var context = SPContext.GetContext(HttpContext.Current, item.ID, list.ID, web);
            editControl.RenderContext = context;
            editControl.ItemContext = context;
        }
        else
        {
            editControl.ControlMode = SPControlMode.New;
            editControl.ItemId = item.ID;

            var context = SPContext.GetContext(HttpContext.Current, item.ID, list.ID, web);
            context.FormContext.FormMode = SPControlMode.New;
            editControl.RenderContext = context;
            editControl.ItemContext = context;
        }

        editControl.ListId = list.ID;

        this.pnlControls.Controls.Add(new System.Web.UI.WebControls.Label
        {
            ID = "lbl_" + uniqueid,
            Text = field.Title
        });
        this.pnlControls.Controls.Add(editControl);
    }   
}