Search This Blog

2009-05-10

How to discover all the list from a site collection

A simple example using the WSS object model to access the top-level site within a target site collection and discover all its lists.

Step 1.After creating a site collection,create a console application "SPListGet".
Open Visual Studio->File ->New project->Visial C#->Console application


Step 2. Add a reference to the Microsoft.Sharepoint.dll

Click on Project->Add Reference and select


Step 3.import the name space "Microsoft.SharePoint" by

using Microsoft.SharePoint;

Step 4.Write the following code in the Main() block

string sitePath = "http://manab:36069/sites/DevSite/";
// enter object model through site collection.
SPSite siteCollection = new SPSite(sitePath);
// obtain reference to top-level site.
SPWeb site = siteCollection.RootWeb;
// enumerate through lists of site
foreach (SPList list in site.Lists)
{
Console.WriteLine(list.Title);
}
Console.ReadLine();
// clean up by calling Dispose.
site.Dispose();
siteCollection.Dispose();

Step 5.Run your console application clicking on F5,You will get following output as follows



The complete code is as follows.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace SPListGet
{
class Program
{
static void Main(string[] args)
{
string sitePath = "http://manab:36069/sites/DevSite/";
// enter object model through site collection.
SPSite siteCollection = new SPSite(sitePath);
// obtain reference to top-level site.
SPWeb site = siteCollection.RootWeb;
// enumerate through lists of site
foreach (SPList list in site.Lists)
{
//if you want to populate only the document library,otherwise
//remove the if condition
if(list.BaseTemplate==SPListTemplateType.DocumentLibrary)
{
Console.WriteLine(list.Title);
}
}
Console.ReadLine();
// clean up by calling Dispose.
site.Dispose();
siteCollection.Dispose();
}
}
}

No comments: