2014-01-13 62 views
0

我们正在尝试使用FieldRenderingControl字段创建自定义窗体。一切都很好,直到我们尝试一个不在根站点中的列表中的新表单。自定义窗体和FieldRenderingControl的错误

所以新建/编辑在站点列表1 /(作品)

编辑在网站/网站/测试网站列表2(作品) 新在网站/网站/测试网站列表2(不工作)

我们在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 
+0

提供错误的完整堆栈跟踪更好的分析。 –

回答

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); 
    } 
} 
0

,因为它适用于在网站/网站/测试网站编辑表单没有理由不应该寻找新形式的列表,了解更多信息和背景。这里需要如果可以的话....