Sunday, November 16, 2008

‘Title’ field and property not available on SPItem when using a View to access the SPListItemCollection

You can get an SPListItemCollection from a List using a View with the following code:

SPList list = SPContext.Current.Web.Lists["My List"];

SPListItemCollection items = list.GetItems(list.Views["All Items"]);

When then try to access the Title field of any of the individual SPListItem instances in the collection you will receive an ArgumentExecption


//all these statements will throw an Argument Exception
string titleField = item[0]["Title"];
string titleProperty = items[0].Title;
string titleValue = items[0].GetFormattedValue("Title");

As a workaround, you can access the Title field value using the LinkTitle field:


//this will return the correct value
string title = items[0].GetFormattedValue("LinkTitle");

No comments:

Post a Comment