TryGetList method in SharePoint 2010
below is sample code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.SharePoint;
namespace MultiChoice
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://serverName/sites/Vijai/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList("Test");
if (list != null)
{
List is Exists
}
else
{
List Is not Exists
}
}
}
}
}
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Iftikar HussainPosted Jul 30, 2013, 12:38 PM
Your code is fine. What error you are getting.
Regards,
Iftikar
Vignesh KumarPosted Jul 30, 2013, 8:43 AM
Hi,
Try this!
C#:
using (SPSite site = new SPSite("http://serverName:XXX/"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("My List");
if (list != null)
{
Console.WriteLine("List exists in the site");
}
else
{
Console.WriteLine("List does not exist in the site");
}
Console.ReadLine();
}
}
POWERSHELL SCRIPT:
$siteURL="http://serverName:XXX/"
$listName="Shared Documents"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$list=$web.Lists.TryGetList($listName)
if($list -ne $null)
{
write-host -f green $listName "exists in the site"
}
else
{
write-host -f yellow $listName "does not exist in the site"
}