locked
How to get list of Document library from site. RRS feed

  • Question

  • Hi All,

    How can I get list of document libraries from site?

    How to code to get list of document libraries name.?

     

    Thanks.

     

    Saturday, July 24, 2010 3:19 AM

Answers

  • Hi imsatasia,

     

    Thanks for your post.

    You can use the SPWeb.GetListsOfType Method.

     

    SPWeb.GetListsOfType Method

    Returns a collection of SPList objects of the specified base list type.

     

    public SPListCollection GetListsOfType(

             SPBaseType baseType

    )

     

    baseType

    Type: Microsoft.SharePoint.SPBaseType

    An enumeration value that specifies the base list type.

     

    Here is an example:

      SPListCollection lists = web.GetListsOfType(SPBaseType.DocumentLibrary);

                   foreach (SPList list in lists)

                   {

                     If(!list.Hidden)

                     {

                              Console.WriteLine(list.Title);

                     }                 

                   }

     

    More information:

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlistsoftype.aspx

    • Marked as answer by imsatasia Monday, July 26, 2010 6:39 PM
    Monday, July 26, 2010 3:31 AM
  • Imsatasia,

     

    using(SPSite cSite = new SPSite("http://localhost"))

    {

     using(SPWeb cWeb = cSite.OpenWeb())

      {

        SPListCollection docLibCollection = cWeb.Lists

     

         // Filter your lists with base type of Document Library

         http://vspug.com/marwantarek/2007/08/12/list-definitions-type-and-basetype/

      }

     

    }


    Thanks
    Akhilesh Nirapure
    • Marked as answer by imsatasia Monday, July 26, 2010 6:39 PM
    Saturday, July 24, 2010 4:59 AM
  • Simply iterate all the lists in ur site using sharepoint object model.

    add a filter on base template or base type properties to select only document libraries.

    print the name of the lists.

     

     

     

    namespace Testing_Purpose
    {
      class Program
      {
        static void Main(string[] args)
        {
          using (SPSite site = new SPSite(" ur site name"))
          { 
          using (SPWeb web = site.OpenWeb())
          {
            foreach (SPList list in web.Lists())
            {
              if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
              { 
              Console.WriteLine (list.Title)
              }
            }
          }
                  }
        }
      }
    }
    
    

     


    Varun Saxena
    • Marked as answer by imsatasia Monday, July 26, 2010 6:39 PM
    Monday, July 26, 2010 5:32 AM
  • Try using the sharepoint web services to fetch the list collection on a particular site and then iterate it accordingly.

     

    The sharepoint Site Data Service might help to fetch the list collection.

     

     

    Site Data Service http://<server-url>/_vti_bin/sitedata.asmx

    Varun Saxena
    • Marked as answer by imsatasia Monday, July 26, 2010 6:39 PM
    Monday, July 26, 2010 5:41 AM

All replies

  • Imsatasia,

     

    using(SPSite cSite = new SPSite("http://localhost"))

    {

     using(SPWeb cWeb = cSite.OpenWeb())

      {

        SPListCollection docLibCollection = cWeb.Lists

     

         // Filter your lists with base type of Document Library

         http://vspug.com/marwantarek/2007/08/12/list-definitions-type-and-basetype/

      }

     

    }


    Thanks
    Akhilesh Nirapure
    • Marked as answer by imsatasia Monday, July 26, 2010 6:39 PM
    Saturday, July 24, 2010 4:59 AM
  • Hi imsatasia,

     

    Thanks for your post.

    You can use the SPWeb.GetListsOfType Method.

     

    SPWeb.GetListsOfType Method

    Returns a collection of SPList objects of the specified base list type.

     

    public SPListCollection GetListsOfType(

             SPBaseType baseType

    )

     

    baseType

    Type: Microsoft.SharePoint.SPBaseType

    An enumeration value that specifies the base list type.

     

    Here is an example:

      SPListCollection lists = web.GetListsOfType(SPBaseType.DocumentLibrary);

                   foreach (SPList list in lists)

                   {

                     If(!list.Hidden)

                     {

                              Console.WriteLine(list.Title);

                     }                 

                   }

     

    More information:

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlistsoftype.aspx

    • Marked as answer by imsatasia Monday, July 26, 2010 6:39 PM
    Monday, July 26, 2010 3:31 AM
  • Hi Akhilesh & Wyane,

     

    Thanks for your help.

     

    It is worrking great with my local machine server.

    But, I'm not able to get Document library from my server which is not on my machine. How can I do that.??

     

    Thanks again.

     

    Monday, July 26, 2010 4:47 AM
  • Simply iterate all the lists in ur site using sharepoint object model.

    add a filter on base template or base type properties to select only document libraries.

    print the name of the lists.

     

     

     

    namespace Testing_Purpose
    {
      class Program
      {
        static void Main(string[] args)
        {
          using (SPSite site = new SPSite(" ur site name"))
          { 
          using (SPWeb web = site.OpenWeb())
          {
            foreach (SPList list in web.Lists())
            {
              if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
              { 
              Console.WriteLine (list.Title)
              }
            }
          }
                  }
        }
      }
    }
    
    

     


    Varun Saxena
    • Marked as answer by imsatasia Monday, July 26, 2010 6:39 PM
    Monday, July 26, 2010 5:32 AM
  • Try using the sharepoint web services to fetch the list collection on a particular site and then iterate it accordingly.

     

    The sharepoint Site Data Service might help to fetch the list collection.

     

     

    Site Data Service http://<server-url>/_vti_bin/sitedata.asmx

    Varun Saxena
    • Marked as answer by imsatasia Monday, July 26, 2010 6:39 PM
    Monday, July 26, 2010 5:41 AM