Determine the identifier of the next SPListItem in SPList MOSS?

I am trying to determine which identifier of the next list item will be created in the MOSS list. Is there any way to do this?

Thank you MagicAndi

+3
source share
4 answers

The following CAML request will determine the most likely identifier that will be generated as follows:

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 when the last created list item (with identifier N) is deleted, and then we use the code above to get the next identifier - it will return N when it should be N +1.

0
source
0

, , , , ID + 1. , .

, uqly.. .

, ? , , GUID, .

0

.

, , :

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

I had version control on my list, but I did not want to create two different versions of my new item. It was decided 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();

You might think that when you turn off the versions, you will delete all versions except the current one, but this is not so.

This does not give you the following identifier, but may solve the problem for someone.

0
source

Source: https://habr.com/ru/post/1714522/


All Articles