SharePoint - Client Side Object Model(CSOM) - Managed - Get Lists that are not hidden and have Item count more than 0 - LINQ Syntax
Problem Definition
Retrieve all Lists in a SharePoint site that are not hidden and contain some items using Managed Code and Client Side Object Model(CSOM) for SharePoint 2013.
Prerequisites
Code Sample
/// <summary>
/// Print lists titles
/// </summary>
/// <param name="listTitle"> List Title </param>
public static void GetAllLists_LinqSyntax()
{
//var spContext = new ClientContext("http://<your server>/<site>/");
using (spContext)
{
// Client side SP Web Conext
var web = spContext.Web;
// Initilize web and load web object with title and description(Helps to improve performance)
spContext.Load(web,w=>w.Title,wde=>wde.Description);
var lists = web.Lists;
//Query to fetch lists that are not hidden and have item count over 0
var query = from ls in lists.Include(l => l.Title
, l => l.Description
, l => l.Hidden)
where ls.Hidden == false &&
ls.ItemCount > 0
select ls;
//Load the linq query
var loadedLists = spContext.LoadQuery(query);
//Execute query
spContext.ExecuteQuery();
// Print the Count of Fields
Console.WriteLine($"Lists Found: {loadedLists.Count()}");
// Print the Details with Header
Console.WriteLine($"{"Title".PadRight(30)}");
Console.WriteLine("--------------------------------------------------------------------------------");
foreach (List f in loadedLists)
{
Console.WriteLine($"{f.Title.PadRight(30)}");
}
}
}
Code Result
Title
--------------------------------------------------------------------------------
Content and Structure Reports
MessageTest
Pages
Reusable Content
Site Collection Images
Style Library
Comments
Post a Comment