SPList的下一个SPListItem ID

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SPList的下一个SPListItem ID相关的知识,希望对你有一定的参考价值。

In order to accurately get the next ID from a SharePoint list you need to access the content database for one reason: If you delete all the items from a list, and execute this simple code: list.Items[list.ItemCount -1].ID) + 1 the ID will always return 0 when in reality the real ID of the next ID will be the auto-increment number in the content database.

Note that list.Items[list.ItemCount -1].ID) + 1 will work as long as one record is always kept in the list and is never deleted.
  1. SPSite site = new SPSite("sharepoint site URL");
  2. int NextID = 0;
  3. if(site.WebApplication.ContentDatabases.Count > 0)
  4. {
  5. string DBConnString = site.WebApplication.ContentDatabases[0].DatabaseConnectionString;
  6. SqlConnection con = new SqlConnection(DBConnString);
  7. try
  8. {
  9. con.Open();
  10. SqlCommand com = con.CreateCommand();
  11. com.CommandText = String.Format("select tp_NextAvailableId from AllLists where tp_ID = '{0}'", listId.ToString());
  12. NextID = (int)com.ExecuteScalar();
  13. }
  14. finally
  15. {
  16. con.Close();
  17. }
  18. }

以上是关于SPList的下一个SPListItem ID的主要内容,如果未能解决你的问题,请参考以下文章

SharePoint 2013 - 通过 REST 获取 SPListItem 版本

csharp SPListItem添加

csharp SPListItem

csharp 新增或者更新SPListItem中的多选字段

csharp SPList添加

将 VS2012 中的 GridView 表填充为带有 SPList Only Title 列的 webpart 具有值