Thursday, 15 April 2010

Get Item based on index from Sharepoint CAML Query to SPListItemCollection -



Get Item based on index from Sharepoint CAML Query to SPListItemCollection -

i trying query list items using caml (first time doing this) , sort items based on modified date can access recent documents. when trying splistitem based on id failing.

code:

spquery query = new spquery(); string camlquery = "<orderby><fieldref name='modified' ascending='false' /></orderby>"; query.viewattributes = "scope = 'recursive'"; query.viewfields = "<fieldref name='modified' /><fieldref name='title' /><fieldref name='name' />"; query.query = camlquery; query.includemandatorycolumns = true; splistitemcollection col = list.getitems(query); //failing here... think splistitem item = col.getitembyid(0);

is there way splistitem based on index within splistitemcollection?

i have tried

splistitem item = col[0];

also no luck.

here error getting:

value not fall within expected range. description: unhandled exception occurred during execution of current web request. please review stack trace more info error , originated in code.

exception details: system.argumentexception: value not fall within expected range.

if want splistitem using id not need go caml, seek this,

spsite osite = new spsite("http:/mytestsite/"); spweb oweb = osite.openweb(); splist olist = oweb.lists["mycustomlist"]; splistitem olistitem = olist.items.getitembyid(1);

but if insist on getting using caml, create sure include id in viewfields caml query.

and items using id :

foreach (listitem item in col) { if(item["id"] == 1) //only illustration { var itemfromid = item; } }

also note, value of id property not same index of item in collection of list items. property contains item's 1-based integer id, 1 greater id of item added. if item deleted, id not reused.[moreinfo]

sharepoint sharepoint-2010 caml

No comments:

Post a Comment