2014-01-13 90 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

,因爲它適用於在網站/網站/測試網站編輯表單沒有理由不應該尋找新形式的列表,瞭解更多信息和背景。這裏需要如果可以的話....