+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to Iterate throgh list to get all listitems

How to Iterate throgh list to get all listitems

4 posts, 0 answered
  1. Sunny
    Sunny avatar
    9 posts
    Registered:
    22 Nov 2011
    15 Jul 2014
    Link to this post

    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 

  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    15 Jul 2014 in reply to Sunny
    Link to this post

    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

  3. Sunny
    Sunny avatar
    9 posts
    Registered:
    22 Nov 2011
    15 Jul 2014
    Link to this post

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

  4. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    Registered:
    24 Nov 2016
    17 Jul 2014
    Link to this post
    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
     
4 posts, 0 answered