Sitefinity CMS

Finding Lists Send comments on this topic.
See Also
Developing with Sitefinity > Modules > Modules API > Lists > Lists (NamedList) > Finding Lists

Glossary Item Box

There are several ways in which you can find one or more lists:


Let’s examine the examples for each of the ways to find one or more lists:

Get list by searching for a list with specific ID:

GetList(Guid id) Copy Code
// create a new instace of ListManager
Telerik.Lists.ListManager listManager = new Telerik.Lists.ListManager();
// Since we don't know ID of any of the lists we'll get all lists and use the first one
// to retrieve a specific list. Typically this way of getting a list would be used in conjuction
// with GridView or some similar control, when you get the primary key and then need
// to retrieve a list based on that primary key.
IList lists = listManager.GetLists();
if (lists.Count > 0)
{
    Telerik.Lists.INamedList firstList = (Telerik.Lists.INamedList)lists[0];
    
// let's get the ID of the first list which we will use to retrieve the list
    
// (this is a redundant step since we already have the list, but is used here to
    
// demonstrate the approach)
    
Guid listId = firstList.ID;
    Telerik.Lists.INamedList theList = listManager.GetList(listId);
    
// let's write the list name to see the result
    
Response.Write(theList.Name);
}

 

Get all lists in the Lists module for the specified provider:

GetLists() Copy Code
// create a new instance of ListManager
Telerik.Lists.ListManager listManager = new Telerik.Lists.ListManager();
// get all lists
IList allLists = listManager.GetLists();
// let's write the name of every list to see the result
foreach (Telerik.Lists.INamedList list in allLists)
{
   Response.Write(list.Name +
"<br />");
}

 

Get all lists in the Lists module ordered in a specific way:

GetLists(string sortExp) Copy Code
// create a new instance of ListManager
Telerik.Lists.ListManager listManager = new Telerik.Lists.ListManager();
// get all lists
IList allLists = listManager.GetLists("Name DESC");
// let's write the name of every lists and number of items it contains to see the result
foreach (Telerik.Lists.INamedList list in allLists)
{
   Response.Write(list.Name +
" ( " + list.Items.Count + " ) " + "<br />");
}

 

Get a specific subset of lists:

For this example we are going to use ObjectDataSource and bind it to a GridView:

GetLists(int from, int max) Copy Code
// odsLists is an ObjectDataSource declared in the UserControl
// for the TypeName of the ObjectDataSource we are going to set ListManager
odsLists.TypeName = "Telerik.Lists.ListManager";
// for SelectMethod we are going to set GetLists
odsLists.SelectMethod = "GetLists";
// we'll set MaximumRowsParameterName to "max"
odsLists.MaximumRowsParameterName = "max";
// the StartRowIndexParameterName we'll set to "from"
odsLists.StartRowIndexParameterName = "from";
// by setting the ObjectDataSource in this way, we have effectively
// told the ObjectDataSource to call Telerik.Lists.ListManager.GetLists(int from, int max) function
// gridLists is a GridView to which we are going to bind odsLists object data source
// make sure you turn on the Paging on the GridView so it sends values for max and from arguments
gridLists.DataSourceID = odsLists.ID;
// finally we are going to bind the GridView to the odsLists ObjectDataSource
gridLists.DataBind();


The example above is working with UserControl where the following controls are declared:

Copy Code
<asp:GridView ID="gridLists" runat="server" AllowPaging="True" PageSize="2">
</
asp:GridView>
<
asp:ObjectDataSource ID="odsLists" runat="server"></asp:ObjectDataSource>

 

Get a specific subset of lists ordered in a particular way:

 For this example we are going to use ObjectDataSource and bind it to a GridView:

GetLists(int from, int max, string sortExp) Copy Code
// odsLists is an ObjectDataSource declared in the UserControl
// for the TypeName of the ObjectDataSource we are going to set ListManager
odsLists.TypeName = "Telerik.Lists.ListManager";
// for SelectMethod we are going to set GetLists
odsLists.SelectMethod = "GetLists";
// we'll set MaximumRowsParameterName to "max"
odsLists.MaximumRowsParameterName = "max";
// the StartRowIndexParameterName we'll set to "from"
odsLists.StartRowIndexParameterName = "from";
// we'll set the SortParameterName to "sortExp"
odsLists.SortParameterName = "sortExp";
// by setting the ObjectDataSource in this way, we have effectively
// told the ObjectDataSource to call Telerik.Lists.ListManager.GetLists(int from, int max, string sortExp) function
// gridLists is a GridView to which we are going to bind odsLists object data source
// make sure you turn on the Paging on the GridView, as well as Sorting so it sends values for max, from and sortExp arguments
gridLists.DataSourceID = odsLists.ID;
// finally we are going to bind the GridView to the odsLists ObjectDataSource
gridLists.DataBind();

 

Get lists with specific ids:

GetLists(params Guid[] ids) Copy Code
// create a new instance of ListManager
Telerik.Lists.ListManager listManager = new Telerik.Lists.ListManager();
// we need to create an array of list IDs that we are going to use later to
// retrieve these lists. This step is redundant and is used here only
// for the purpose of demonstration. Usually, you would have the array of list IDs
// (e.g. from database or querystring) and you would use that array to get the lists
List<Guid> specificListIds = new List<Guid>();
      
// in this example we are going to retrieve only the first three lists
IList allLists = listManager.GetLists();
for (int i = 0; i < allLists.Count; i++)
{
   Telerik.Lists.INamedList list = (Telerik.Lists.INamedList)allLists[i];
   specificListIds.Add(list.ID);
   
// we'll exit the loop if more than three IDs have been added
   
if (i == 2)
      
break;
}
// we are going to get only lists whose ID is contained in the array we have
// passed as an argument. This function is generally useful for creating selectors
// - for example you could provide user interface for the user to choose which lists
// to display
IList specificLists = listManager.GetListsByIds(specificListIds.ToArray());
// finally we'll write the names of the lists that we have retrieved
foreach (Telerik.Lists.INamedList specificList in specificLists)
    Response.Write(specificList.Name +
"<br />");

 

See Also