top of page

Checking the Existence of SharePoint List using the TryGetList Method

SharePoint provides various methods to interact with lists, but sometimes it becomes necessary to check if a list exists before performing any operations on it. In this article, we will explore the TryGetList method in SharePoint, which allows us to determine the existence of a list without throwing null reference errors. We will demonstrate how to use this method using PowerShell and C# code examples.


What is TryGetList Method in SharePoint List?

In the SharePoint list, the TryGetList method is used to retrieve a list from a SharePoint site based on its name. It is a method commonly used in SharePoint development to check if a list exists and retrieve it if it does, without throwing an exception if the list is not found.


Here is a definition of the TryGetList method in SharePoint:

public static bool TryGetList(string listName, out SPList list)

Parameters:

  • listName: A string representing the name of the list you want to retrieve.

  • list: An out parameter of type SPList that will store the reference to the retrieved list if it exists.

Return value:

  • true if the list is found and retrieved successfully.

  • false if the list is not found.


Example:

using Microsoft.SharePoint;

public void ExampleMethod(string listName)
{
    SPList list;
    if (SPContext.Current.Web.TryGetList(listName, out list))
    {
        // The list exists, and you can perform operations on it.
        // For example, you can access its properties or iterate through its items.
    }
    else
    {
        // The list does not exist.
        // You can handle this case accordingly.
    }
}

By using the TryGetList method, you can safely check for the existence of a list without causing an exception and handle the scenario based on the returned boolean value.


Checking List Existence in SharePoint using TryGetList Method

We can check whether the list exists in SharePoint using TryGetList with PowerShell and C#.


Checking List Existence using PowerShell:

To check if a list exists in SharePoint using PowerShell, we can utilize the TryGetList method. Here's an example script:

$siteURL = "http://serverName:1111/"
$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"
}

In the above code, we first specify the URL of the SharePoint site and the name of the list we want to check. We then retrieve the site and its root web using Get-SPSite and $site.RootWeb respectively. Finally, we use the TryGetList method on the web.Lists object, passing the list name as a parameter. If the returned list object is not null, we display a message indicating that the list exists; otherwise, we display a message indicating its absence.


Checking List Existence using C#:

Similarly, we can employ the TryGetList method in SharePoint when working with C# code. Here's an example snippet:

using (SPSite site = new SPSite("http://serverName:1111/"))
{
  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();
  }
}

In the above C# code, we create a new instance of the SPSite object using the SharePoint site URL. Inside a using block, we retrieve the root web of the site using site.RootWeb. We then call the TryGetList method on web.Lists, providing the list name as an argument. If the returned list object is not null, we print a message stating that the list exists; otherwise, we print a message indicating its absence.


Benefits of the TryGetList Method:

  1. It avoids throwing exceptions when the list is not found, improving code robustness.

  2. It provides a more efficient way to check the list existence without the overhead of exception handling.

  3. It simplifies code by eliminating the need for try-catch blocks, resulting in cleaner code.

  4. It allows for customized handling when the list does not exist, providing a better user experience.

Limitations of the TryGetList Method:

  1. It can only be used within the SharePoint environment.

  2. It is primarily designed for retrieving lists based on name and lacks advanced filtering or querying capabilities.

  3. It requires providing the correct list name for successful retrieval.

  4. By default, it operates within the current site context, requiring additional steps for retrieving lists from different sites or specific webs.


Conclusion

By utilizing the TryGetList method in SharePoint, we can easily determine whether a list exists without encountering null reference errors. Whether you prefer PowerShell or C# code, this article has demonstrated how to check list existence using the TryGetList method in both scripting languages. This knowledge will be invaluable when developing SharePoint solutions that require list verification before further operations.

bottom of page