2009-09-02 83 views
9

我試圖遍歷SPListItem.Versions集合來查找最新的批准列表項。查找SPListItem的最新批准版本

我的清單項目有三個版本:前兩個是批准的,最後一個是草案。但我的代碼說他們都在草稿中!請幫忙!

// Iterate through all versions 
for (int index = 0; index < item.Versions.Count; index++) 
{ 
    SPListItem versionedItem = item.Versions[index].ListItem; 

    // Check if moderation information is set to approved 
    if (versionedItem.ModerationInformation.Status.Equals(SPModerationStatusType.Approved)) 
    { 
     // We found an approved version! 
     itemFound = versionedItem; 
    } 
} 

回答

9

item.Versions [指數]返回一個SPListItemVersion實例,SPListItemVersion.ListItem返回父SPListItem。因此,您的versionedItem最終會將相同的對象稱爲item,並且您一遍又一遍地檢查相同的版本。

我相信你真的要檢查

if (item.Versions[index].Level == SPFileLevel.Published) { 
    // check item.Versions[index].VersionLabel 
} 
+0

它的工作,謝謝! 讓父母像那樣有點尷尬嗎?如果你問我什麼時候在SharePoint中稱爲批准,我想,列表中的Moderation和列表項上的Level!! – 2009-09-02 08:51:09

6

我的代碼最終看上去像這樣:

if (doclist.EnableVersioning) 
{ 
    SPListItemVersionCollection allVersions = item.Versions; 

    // Iterate through all versions 
    foreach (SPListItemVersion version in allVersions) 
    { 
     if (version.Level == SPFileLevel.Published) 
     { 
      itemFound = version.ListItem; 
     } 
    } 
} 

整齊漂亮,我真的很希望當客戶部署它的作品!

+3

調用itemFound = version.ListItem;將返回可能不是最新批准的最新版本。當心 – 2009-10-27 03:50:26

9

Mattias建議您實施的方式是最好的方法。由於這些項目是從最近到最老的順序排列的,因此它有點尷尬但仍然有效。這意味着您可能很快就會在發佈的版本中獲得匹配。

擴大對MSDN SPListItemVersionCollection article(特別是塞巴斯蒂安·沃伊切霍夫斯基的加法):

// Current version of the item (note: this may be a draft) 
SPListItem.Versions[0] 

// Previous version of the item 
SPListItem.Versions[1] 

// First version of the item 
SPListItem.Versions[SPListItem.Versions.Count - 1] 
+0

謝謝,非常有幫助!並感謝編輯我的草率拼寫! :) – 2009-09-02 10:02:16

+0

沒問題。編輯以幫助用戶稍後查找您的問題。 – 2009-09-02 10:45:08

+0

由於您現在提供的鏈接指向SharePoint 2013,並且您引用的社區內容已附加到以前的版本:http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.splistitemversioncollection(v = office .12).aspx – Chloraphil 2012-11-28 15:46:39