Monday 18 June 2012

Retrieve all document libraries in a SharePoint site programmatically


Hello all

Today I had a very different requirenment where I had to retrieve all the document libraries in a SharePoint site (not the lists). What I mean is I needed a list of all the list\libraries that were created from "101" template. Also, the code needed to be in Client Object model. I did a lot of research about If there is a switch to check if a list is a type document library but I got nothing. So here is what I did

<script type="text/javascript">
var currentcontext = null;
var currentweb = null;
ExecuteOrDelayUntilScriptLoaded(GetLibrariesOnly, "sp.js");
function GetLibrariesOnly()
{
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext.get_web();
this.listCollection = currentweb.get_lists();
currentcontext.load(listCollection);
currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));
}
function ExecuteOnSuccess(sender, args) {
var listEnumerator = this.listCollection.getEnumerator();
   var allLibs ="";
   while (listEnumerator.moveNext())
   {
            var list = listEnumerator.get_current();
            if(list.get_baseTemplate() == '101')
            {
             allLibs+= list.get_title() + list.get_baseTemplate(); + '\n';               
            }
   }
    alert("All Libraries" + '\n' + allLibs);
 }
function ExecuteOnFailure(sender, args) {
alert("Error in Getting Lists");
}
</script>

Enjoy!

Ads by Google

No comments:

Post a Comment