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.
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
}
}
-
Thanks. Though I believe your code has some typos since you assigned list the value null. – KyleM Jun 10 '11 at 18:28
-
1No. 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
-
1Ah, 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
-
1Marked 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
-
1Short 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.
-
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