6

Usually when we need to get all the document libraries from a SharePoint site, we can follow the given code snippet:-

SPListCollection lstCol = oSite.OpenWeb().GetListsOfType(SPBaseType.DocumentLibrary);

But how to get the same result using SharePoint Client Object Model? Thanks in advance...:)

2 Answers 2

7

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()
       {}
    }
}
0
7

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.

2
  • Goes this work in JavaScript?
    – Batman
    Commented Aug 14, 2016 at 21:35
  • @Batman, not sure if there is a JavaScript equivalent; I doubt it can do the LINQ / IQueryable stuff. Commented Apr 10, 2017 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.