确定 MOSS SPList 中的下一个 SPListItem ID?

发布于 2024-07-30 12:37:52 字数 74 浏览 3 评论 0原文

我正在尝试确定要在 MOSS 列表中创建的下一个列表项的 ID。 有什么办法可以做到这一点吗?

谢谢,魔法安迪

I am trying to determine what the ID of the next list item to be created in a MOSS list will be. Is there any way of doing this?

Thanks, MagicAndi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱&纷飞 2024-08-06 12:37:52

以下 CAML 查询将确定接下来最有可能生成的 ID:

int iNextID = 1;

try
{
    using (SPSite objSite = new SPSite(sSiteUrl))
    {
        using (SPWeb objWeb = objSite.OpenWeb())
        {
            SPList objList = objWeb.Lists[sListName];

            SPQuery objQuery = new SPQuery();
            // objQuery.RowlImit = 1;
            objQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><RowLimit>1</RowLimit>";
            objQuery.Folder = objList.RootFolder;

            // Execute the query against the list
            SPListItemCollection colItems = objList.GetItems(objQuery);

            if (colItems.Count > 0)
            {
                string sMaxID = colItems[0]["ID"].ToString();
                iNextID += int.Parse(sMaxID);
            }
        }
    }
}
catch (Exception ex)
{
    ...
}

return iNextID;

但是,这对于删除最近生成的列表项(ID N)的情况不起作用,然后我们使用上面的代码给出下一个ID - 它将返回 N,而它应该是 N+1。

The following CAML query would determine the most likely ID to be generated next:

int iNextID = 1;

try
{
    using (SPSite objSite = new SPSite(sSiteUrl))
    {
        using (SPWeb objWeb = objSite.OpenWeb())
        {
            SPList objList = objWeb.Lists[sListName];

            SPQuery objQuery = new SPQuery();
            // objQuery.RowlImit = 1;
            objQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><RowLimit>1</RowLimit>";
            objQuery.Folder = objList.RootFolder;

            // Execute the query against the list
            SPListItemCollection colItems = objList.GetItems(objQuery);

            if (colItems.Count > 0)
            {
                string sMaxID = colItems[0]["ID"].ToString();
                iNextID += int.Parse(sMaxID);
            }
        }
    }
}
catch (Exception ex)
{
    ...
}

return iNextID;

However, this does not work for the case where the most recently generated list item (with ID N) is deleted, and we then use the above code to give the next ID - it will return N, when it should be N+1.

酒废 2024-08-06 12:37:52

看这个链接: http://snipplr.com/view/ 24210/next-splistitem-id-from-splist/,我尝试过这个解决方案,它确实克服了其他缺点,它涉及查询 WSS 内容数据库,这可能会贵一点,但你可以实现混合解决方案,仅当 colItems.Count == 0 在其他逻辑之后才查询数据库。

问候!

Look at this link: http://snipplr.com/view/24210/next-splistitem-id-from-splist/, I have tried this solution and it does overcome the other shortcomings, it involves querying the WSS content DB which can be a little bit more expensive, but you could implement a hybrid solution, where you would query the DB ONLY if colItems.Count == 0 after the other logic.

Regards!

揪着可爱 2024-08-06 12:37:52

如果您想更加确定,可以创建一个列表项,记录其 ID,删除该列表项,下一个列表项将具有 ID+1。 这应该可以克服最后一项被删除的情况。

当然,这非常简单……但从内容数据库读取也是如此。

我可以问一下为什么需要它吗? 或者,您可以将包含 GUID 的列添加到列表中,以便您可以自己预先生成 ID。

If you want to be more sure you can create a listitem, record its ID, delete the list item and the next listitem will have ID+1. This should overcome the case where the last item was deleted.

Of course this is quite uqly.. but so is reading from the content database.

May I ask why you need it? Alternatively you can add a column to your list that contains a GUID so you can pre-generate ID's yourself.

萌辣 2024-08-06 12:37:52

我找到了解决这个问题的另一种方法。

我遇到的问题与版本有关:

var newItem = myList.Items.Add();
newItem["Title"] = "whatever";
newItem.Update();
newItem["MyProperty"] = "whatever" + newItem.ID;
newItem.Update();

我的列表中有版本控制,但不想创建新项目的两个不同版本。
像这样解决它:

var newItem = myList.Items.Add();
newItem["Title"] = "whatever";
newItem.Update();
myList.EnableVersioning = false;
myList.Update();
newItem["MyProperty"] = "whatever" + newItem.ID;
newItem.Update();
myList.EnableVersioning = true;
myList.Update();

人们可能认为关闭版本控制会删除除当前版本之外的所有版本,但事实并非如此。

这不会为您提供下一个 ID,但可能会解决某人的根本问题。

I found another solution to this problem.

The problem I had was related to versions:

var newItem = myList.Items.Add();
newItem["Title"] = "whatever";
newItem.Update();
newItem["MyProperty"] = "whatever" + newItem.ID;
newItem.Update();

I had versioning on my list but didn't want to create two different versions of my new item.
Solved it like this:

var newItem = myList.Items.Add();
newItem["Title"] = "whatever";
newItem.Update();
myList.EnableVersioning = false;
myList.Update();
newItem["MyProperty"] = "whatever" + newItem.ID;
newItem.Update();
myList.EnableVersioning = true;
myList.Update();

One might think that turning versioning off would erase all versions except the current but it doesn't.

This doesn't give you the next ID but might solve the underlying problem for someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文