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
}
}
}
}
}
}


Vignesh KumarRank #1,579 · 1K reputationPosted Jul 30, 2013, 4:43 AM EDT
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"
}
Iftikar HussainRank #423 · 5.9K reputationPosted Jul 30, 2013, 8:38 AM EDT
Your code is fine. What error you are getting.
Regards,
Iftikar