Hi,
I'm getting below exception for the code, where 'Project Description' is the field display name.
SPWeb oWebsite = SPContext.Current.Web; SPList oList = oWebsite.Lists["Project Info"]; SPListItemCollection collItem = oList.GetItems("Project Description"); if (collItem.Count > 0) { foreach (SPListItem oItem in collItem) { description = oItem["Project Description"].ToString(); } }
System.ArgumentException: Value does not fall within the expected range. at Microsoft.SharePoint.SPFieldMap.GetColumnNumber(String strFieldName, Boolean bThrow) at Microsoft.SharePoint.SPListItemCollection.GetColumnNumber(String groupName, Boolean bThrowException) at Microsoft.SharePoint.SPListItemCollection.GetRawValue(String fieldname, Int32 iIndex, Boolean bThrow) at Microsoft.SharePoint.SPListItem.GetValue(SPField fld, Int32 columnNumber, Boolean bRaw, Boolean bThrowException) at Microsoft.SharePoint.SPListItem.GetValue(String strName, Boolean bThrowException) at Microsoft.SharePoint.SPListItem.get_Item(String fieldName)
But this code works fine, where 'All Items' is the view.
SPWeb oWebsite = SPContext.Current.Web; SPList oList = oWebsite.Lists["Project Info"]; SPListItemCollection collItem = oList.GetItems(oList.Views["All Items"] ); if(collItem.Count>0) { foreach(SPListItem oItem in collItem) { description = oItem["Project Description"].ToString(); } }
It seems that there is some issue with field internal name. But why it works, when we use view to get list items?