I got this interview question today, any idea?
Difference between SPList.AllItems.GetItem() or SPLIst.GetItem();
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.Sign up to join this community
SPList class does not have an
AllItems property. Neither does it have a
GetItem method. So, there is no difference between these two, they are both invalid.
So, the following code:
SPList list = SPContext.Current.List; list.AllItems; list.GetItem();
Would generate the following errors:
'Microsoft.SharePoint.SPList' does not contain a definition for 'AllItems' and no extension method 'AllItems' accepting a first argument of type 'Microsoft.SharePoint.SPList' could be found (are you missing a using directive or an assembly reference?)
'Microsoft.SharePoint.SPList' does not contain a definition for 'GetItem' and no extension method 'GetItem' accepting a first argument of type 'Microsoft.SharePoint.SPList' could be found (are you missing a using directive or an assembly reference?)
Additionally, all three references Google has to "SPList.AllItems" point to this question (way to go SE SEO!).
I think the possibilities are:
Just some small points, in case it was just a mistake and to clarify for anyone new to SharePoint reading this...
SPList.GetItems (note the 's') is the main way you would query data, using one of the four available overloads:
SPList mylist = myWeb.Lists["myList"]; SPListItemCollection Result; Result = mylist.GetItems("LinkTitle", "AssignedTo", "DueDate", "Status"); // Note that query is an SPQuery that must be defined above Result = mylist.GetItems(query); SPView view = mylist.Views["My View"]; Result = mylist.GetItems(view); Result = mylist.GetItems(query, "My View");
AllItems on the other hand is 100% invalid. You would use SPList.Items to get the SPListItemCollection of all items in the list.