5

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...:)

5

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

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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