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

I got this interview question today, any idea?

Difference between SPList.AllItems.GetItem() or SPLIst.GetItem();

share|improve this question
    
from the documentation it doesn't look like SPList has a property AllItems, so the first example is invalid? still learning sp though, so not sure if this is the case. –  TZHX Jun 8 '12 at 9:49
    
thats what I thought, this interview question seemed weird to me as well –  Esteban V Jun 8 '12 at 9:52

2 Answers 2

up vote 5 down vote accepted

The 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:

  • you misheard the question?
  • the interviewer wanted to trip you up by asking ridiculous details. In a real situation confronted with two similar methods/attributes you could just Google it to see which is appropriate (MS documentation is amazing).
share|improve this answer

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.

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.