Sharepoint - How to get only the document libraries from a sharePoint site using SharePoint Client Object Model?

Hi u just get all list from site and just check it's BaseType like this

using (ClientContext clientcontext= new ClientContext("http://your server"))
{

    //Load Libraries from SharePoint
    clientcontext.Load(clientcontext.Web.Lists);
    clientcontext.ExecuteQuery();
    foreach (List list in clientcontext.Web.Lists)
    {
       try
       {
            if (list.BaseType.ToString() == "DocumentLibrary")
             {
                 // here u get all document library
             }
       }
       catch()
       {}
    }
}

This is better as it only pulls back the document libraries rather than all the lists which you then have to check in a loop.

 var Libraries = clientContext.LoadQuery(clientContext.Web.Lists.Where(l => l.BaseTemplate == 101));
 clientContext.ExecuteQuery();

Also note that I've used the BaseTemplate property which has more granularity than BaseType. You can get ListTemplate enumeration values which map to the CSOM BaseTemplate property from here.

Tags: