Monday, April 2, 2012

Get all SharePoint document library objects in descending order using ClientContext

Leave a Comment

To demonstrate this task we will create Console Application in Visual  Studio 2010. 
Add Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime assemblies to the references.
This assemblies resides inside 14 hive ISAPI folder.

Refer Web from ClientContext

 ClientContext clientContext = new ClientContext("siteurl");
 Web web = clientContext.Web;

Get list collection from web

 ListCollection listCollection = web.Lists;
 clientContext.Load(listCollection);
 clientContext.ExecuteQuery();


Iterate through listcollection to get lists of Base Type: document library

System.Collections.Generic.List documentlists = new List();
         
          foreach (List list in listCollection)
          {
            if(list.BaseType == BaseType.DocumentLibrary)
            {
                documentlists.Add(list);             
            }
          }

Using linq extension get all the objects in descending order.

      var sortedlist = documentlists.OrderByDescending(p => p.Title);

sortedlist will contain list of sorted document library in descending order by its title. 

0 comments: