Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

What attributes uniquely identify an item in a SharePoint 2010 list? For example, I want to programmatically find all items in a list, given only the URL of the list's location. Then I want to iterate over all of the list's items, creating a "UniqueListItem" object for each item in the list. What properties would that UniqueListItem need, to be able to uniquely identify an item in the list? I think each SP list has a unique list GUID, and then each item within that list has an integer identifying it. Is that correct? And how would I programmatically find those things? Thanks.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

Get the list. If GetListFromUrl throws an exception the list doesn't exist. If it does, iterate over your SPListItem collection.

SPList list;
try {list = web.GetListFromUrl("http://sharepoint/sites/wea/Lists/TestList"); }
catch { list = null; } //list does not exist

if (list != null)
{
    SPListItemCollection itemCollection = list.Items;
    foreach (SPListItem item in itemCollection)
    {
        //do something
    }
}
share|improve this answer
    
Thanks. Though I believe your code has some typos since you assigned list the value null. –  KyleM Jun 10 '11 at 18:28
1  
No. I declared the list, then entered the try catch. As I mentioned in my post, the line list = web.GetListFromUrl("sharepoint/sites/wea/Lists/TestList"); will throw an exception if the list doesn't exist. I'm catching that exception and assigning it a null value. –  Tim Gabrhel Jun 10 '11 at 18:29
1  
Ah, my apologies, I see what you did there. Though 'list' will already be null if the GetListFromUrl method throws an exception. –  KyleM Jun 10 '11 at 18:31
1  
Marked as correct yet Tim didn't actually answer the question about the primary key of the list item? –  Ryan Jun 13 '11 at 10:18
1  
Short of the extra text in the OP, my provided solution does exactly what he asked for. Find a list, given the URL. Then, iterate over each of the list items. –  Tim Gabrhel Jun 13 '11 at 12:54

The list can be identified by URL and has a GUID ID.

Items within the list have a unique integer ID, and a GUID UniqueId. The SPList class has both GetItemById and GetItemByUniqueId methods. Given the choice, the normal Int ID is normally used.

share|improve this answer
1  
+1 for being very comprehensive - I didn't know that list items had a GUID as well as the ID. –  Ryan Jun 13 '11 at 10:19
1  
+1 for being so thorough. I did notice that when I looked through the SPList class but when I posted this question I didn't know that SPList was used for SP2010. Thanks for responding! –  KyleM Jun 13 '11 at 13:43

The SPList object has a GetItemById method

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.