How to Iterate throgh list to get all listitems

Posted by Community Admin on 04-Aug-2018 06:16

How to Iterate throgh list to get all listitems

All Replies

Posted by Community Admin on 15-Jul-2014 00:00

I have Created a List by name FAQ and i have multiple items in that list.

HOw do i iterate through FAQ list to get title and content of all listitems.

I am unable to use forloop. My code is given below

    Telerik.Sitefinity.Lists.Model.List list = App.WorkWith().Lists().Where(l => l.Title == "FAQ").Get().FirstOrDefault(); 

  if (list != null && list.ListItemsCount > 0)
                 
                     foreach (var item in list)
                     
                  
                     
                     
                
                  

Here foreach loop is given me error 

Posted by Community Admin on 15-Jul-2014 00:00

Hi Sunny,

I'm not sure what error you are getting, but it seems you are trying to loop over a collection of Lists, where you actually first need to get the ListItems.

using (var manager = ListsManager.GetManager())
            
                var faqListItems = manager.GetListItems().Where(x => x.Parent.Title == "FAQ" && x.Status == ContentLifecycleStatus.Live);
                if (!faqListItems.Any()) return;
 
                foreach (var title in faqListItems.Select(listItem => listItem.Title))
                
                     
                
            

So you need to get the ListItems from a List (using the Parent property of a ListItem)

Best regards,
Daniel

Posted by Community Admin on 15-Jul-2014 00:00

Thanks Daniel,  But i need same code  using  Fluent API's 

Posted by Community Admin on 17-Jul-2014 00:00

Hi,

Please, review our documentation regarding Lists and List Items. There are samples for querying them using both Fluent and Native API.

Regards,
Nikola Zagorchev
Telerik

 
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
 

This thread is closed