Posts Tagged ‘Object Model’

Monday, January 28th, 2008

SharePoint Internals: SPList.GetItems

SharePoint is build upon lists. These lists contain items. So the action to retrieve items from these lists is quite common. But when you’re coding against the object model. You might run into surprises. In the previous post I already talked about internal names and display names of fields. Today I’ll talk about an issue when retrieving items from a list, and more specifially from a view.

When SharePoint is retrieving items from a list or view, SPList.GetItems is always called. This method will actually create a new collection of items. Those items will actually be retrieved by a query. To query in SharePoint we use a SPQuery object. This object is a container for SQL like queries. You select the fields you want to see, you have some conditions, a rowlimit, … And this is were a possible problem can reside. When retrieving items from a view, the query will only retrieve the fields defined in the SPView. In other words: the SPListItems you will get do not include all the data. This can be very confusing, as you expect a one-to-one relationship between the data of an object and the SPListItem. You should not forget however that the SPListItem is actually a proxy container for XML data. The object encapsulates the data for easy access and is not the actual full (logic) list item.

So how can we solve this? Well pretty easily actually. We create a real SPQuery object to retrieve our items with:

SPView view = myList.DefaultView;

SPQuery query = new SPQuery();
query.Query = view.Query;

SPListItemCollection myColl = myList.GetItems(query);

Pretty easy, huh. The thing to remember here: SPListItem objects encapsulate XML data and are not the actual list items! Have a great week, everyone!

Friday, January 25th, 2008

SharePoint Internals: InternalName versus DisplayName

When creating columns (more commonly called fields) in SharePoint through the interface, you have to enter a name for it. This name is used throughout the lists and sites, included internally. Except when you try to change the name, it’ll only reflect on the outside. Internally the old name will be kept. This is because a field has two names: an internal name and a display name. When creating a field, you set both. When renaming it, you only change the display name. (There is actually no way to change the internal name afterwards)

But why is this a concern? Well, in the object model it can become quite vague when to use the internal name and when to use the display name. Here is a short list with some common methods and the name they need.

  • SPFieldCollection[name] : SPField
    name: DisplayName
    unexistent: exception
  • SPFieldCollection.GetField(name) : SPField
    name: internalName, displayName or internalName and displayName from the current context
    unexistent: exception
  • SPFieldCollection.GetFieldByInternalName(name) : SPField
    name: internalName
    unexistent: exception
  • SPFieldCollection.ContainsField(name) : bool
    name: displayName or internalName
    unexistent: boolean
  • SPListItem[name] : object
    name: internalName, displayName or internalName and displayName from the current context
    unexistent: null
  • SPListItem.GetFormattedValue(name) : string
    name: internalName, displayName or internalName and displayName from the current context
    unexistent: exception

If I find more relevant functions, I will update this list. On a related note, there also exists a static name. This is a name used by the field type. This is different from the internal name, as the internal name must be unique in its list and could have changed.

Hope this clears up some confusion about field naming in SharePoint. See ya.