2013-09-30 27 views
0

我有以下代碼:如何在不跳過合法循環的情況下處理nullreferenceexception?

foreach (SPListItem item in list.Items) 
{ 

    string itemId = item.ID.ToString(); 
    string contentType = item.ContentType.ToString(); 
    string displayName = item.DisplayName; 
    string name = item.Name; 

    // todo: Title not always retreiving? Likely due to folders, needs more handling 
    //string title = item["Title"].ToString() ?? null; 
    string title = ""; 

    string url = item.Url; 
    string author = item["Created By"].ToString(); 

    // todo: Checked out files catering 
    const string checkedOutBy = ""; 

    DateTime lastModified = Convert.ToDateTime(item["Modified"]); 
    string lastModifiedBy = item["Modified By"].ToString(); 
    DateTime created = Convert.ToDateTime(item["Created"]); 


    query.RecordItems(itemId, contentType, 
         displayName, name, 
         title, url, author, 
         checkedOutBy, 
         lastModified, 
         lastModifiedBy, 
         created, 
         author); 
} 

我的問題是環路標題或ContentType的一些迭代將拋出一個NullReferenceException,但不是所有的人。我相信我已經滿足了以下要求,但我不確定 - 有沒有更好的方法?

foreach (SPListItem item in list.Items) 
{ 
    try 
    { 
     string itemId = item.ID.ToString(); 
     string contentType = item.ContentType.ToString(); 
     string displayName = item.DisplayName; 
     string name = item.Name; 

     // todo: Title not always retreiving? Likely due to folders, needs more handling 
     //string title = item["Title"].ToString() ?? null; 
     string title = ""; 

     string url = item.Url; 
     string author = item["Created By"].ToString(); 

     // todo: Checked out files catering 
     const string checkedOutBy = ""; 

     DateTime lastModified = Convert.ToDateTime(item["Modified"]); 
     string lastModifiedBy = item["Modified By"].ToString(); 
     DateTime created = Convert.ToDateTime(item["Created"]); 


     query.RecordItems(itemId, contentType, 
          displayName, name, 
          title, url, author, 
          checkedOutBy, 
          lastModified, 
          lastModifiedBy, 
          created, 
          author); 
    } 
    catch (NullReferenceException ex) 
    { 
     Logger.Error("[{0}] Filed moving on file {1} as not all content was present", item.Name); 
    } 

} 
+0

的http:// msdn.microsoft.com/en-us/library/bb687949(v=office.12).aspx請收集一些SharePoint最佳實踐。例如'不要枚舉整個SPList.Items集合' –

回答

1

可以過濾項目和循環它們如下

list = list.Items.Where(i=> i.ContentType !=null && i["Title"] !=null).ToList(); 

,如果你不需要做所有上述驗證和過濾器,你最好移動到for loop

for (int i = 0; i < list.Items.Count; i++) 
{ 
    try 
    { 
     SPListItem item = list.Items[i]; 
     // your code 
    } 
    catch (NullReferenceException ex) 
    { 
     Logger.Error("[{0}] Filed moving on file {1} as not all content was present", item.Name); 
    } 

} 
+0

'SPListItem item = list.Items [i];'no。你不會在SharePoint中這樣做,因爲它不會緩存'list.Items',但每次都會從服務器獲取列表 –

+0

@DmitryDovgopoly OP沒有提供關於'list'的詳細信息。它可能已經被緩存了,你認爲這是這個異常的根本過程嗎? – Damith

+0

顯然它不是異常的來源,但它是非常不推薦的使用'SPList.Items'的方式。 'list'是'SPList',因爲在這種情況下它可以不是別的。 –

2

希望我真的在這裏uderstand的問題,所以爲什麼不..

foreach (SPListItem item in list.Items) 
{ 
    ...... 
    if(title == null || item.ContentType == null) 
     continue; 


    .... 
} 

如果這不是你問什麼,請澄清。

+2

有問題的代碼將拋出'item.ContentType.ToString();'所以你應該檢查'item.ContentType'而不是'contentetype' –

+0

@DmitryDovgopoly:釘它, 謝謝 ! – Tigran

相關問題