I have a list (tick_gen) which has a view called "tick_dView" this view just has one row. How can I fetch the value of "Tick_id" column using SharePoint object model?
1 Answer
At first get a view, next create an SPQuery object using this view's query and then get your item.
Code will look like this one:
var view = list.Views["tick_dView"];
var query = new SPQuery();
query.Query = view.Query;
query.RowLimit = view.RowLimit;
var item = list.GetItems(query).Cast<SPListItem>().FirstOrDefault();
if(item != null)
{
var tickId = item["Tick_id"];
}
You can also pass view directly into spquery constructor (and ommit 3 and 4th lines), but doing this you must be sure, that Tick_id
field is present in the tick_dView
. If not, you will need to specify additional view fields for spquery.
-
it worked..though i was getting error when i try to cast..the Cast was not comming up in the intelleigence so i went ahead and used ur logic with litle syntax change var list = web.Lists["tick_list"]; var view = list.Views["tick_dView"]; var query = new SPQuery(); query.Query = view.Query; query.RowLimit = view.RowLimit; SPListItemCollection sp1 = list.GetItems(query); foreach (SPListItem listitem in sp1) { string IDVAL = listitem[Tick_id].ToString(); }– SandeshRJan 17, 2012 at 22:16
-
you need to add
using System.Linq;
, sorry I forgot to mention it. Jan 18, 2012 at 6:27