Avatar of brendanlefavre
brendanlefavre
Flag for United States of America asked on

SPListItem

How can I get the last entry in a list? The example below will gain me access to the items in a list, but I'm getting stuck on how to get the last item submitted.

using (SPSite siteCol = new SPSite("http://server/sites/Contoso"))
{
    using (SPWeb web = siteCol.RootWeb)
    {
        SPList list = web.GetList("/sites/Contoso/Lists/Books");
        SPListItemCollection items = list.GetItems("Title", "LogIDNumber",);
    }
}

Open in new window


Cheers,
Brendan
Microsoft SharePoint

Avatar of undefined
Last Comment
brendanlefavre

8/22/2022 - Mon
abhitrig

Use a CAML query to get the items and get the last item in the result.

<Query>
    <OrderBy>
            <FieldRef Name="LogIDNumber" Ascending="FALSE" />
    </OrderBy>
</Query>


If you  want the last modified/created  item, you can also use the modified/created by field
Jamie McAllister

Something like this should do it.
SPQuery query = new SPQuery();
 query.Query = "<OrderBy><FieldRef Name='Created'
Ascending='FALSE' /></OrderBy>";
query.RowLimit = 1;
SPListItemCollection col = myList.GetItems(query);
                           
if (col.Count > 0)
{
  //do something
}

Open in new window

ASKER CERTIFIED SOLUTION
Member_6283346

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
brendanlefavre

ASKER
this helped me resolve my issue

Cheers,
Brendan
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy