Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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(); } –  spStacker Jan 17 '12 at 22:16
    
you need to add using System.Linq;, sorry I forgot to mention it. –  Kai Jan 18 '12 at 6:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.