Check whether SharePoint list is an External List


Description:

In this article we will be seeing how to check whether the SharePoint list is an External list or not using SharePoint Object Model and Powershell.

Prerequisites:

  1. Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 on the server.
  2. Microsoft Visual Studio.
  3. At least one external content type registered in the BDC Metadata Store and an external list based on the external content type.

SPServiceContext Class:

SPServiceContext class is required to make a call to the Service Application.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)

SPServiceContextScope class:

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Using SharePoint Object Model:

  1. Open Visual Studio 2010.
  2. Go to File => New => Project.
  3. Select a C# Console application project template.
  4. Select .NET Framework 3.5 when you create the project.
  5. From the View menu, click Property Pages to bring up the project properties.
  6. In the Build tab, for the Platform target, select Any CPU.
  7. Add the following references

    • Microsoft.SharePoint
    • System.Web
     
  8. Add the following namespaces

    • using Microsoft.SharePoint;
    • using System.Net;
     
  9. Replace Program.cs with the following code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using System.Net;

    namespace HTTPSTesting
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (SPSite site = new SPSite("https://serrvername.com/r/R/"))
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    SPServiceContextScope scope = new SPServiceContextScope(context);|
                    using (SPWeb web = site.RootWeb)
                    {
                        SPList list = web.Lists.TryGetList("DEV09 External List - Remote");
                        if (list != null)
                        {
                            if (list.HasExternalDataSource)
                            {
                                Console.WriteLine(list.Title + " is an external list");
                            }
                            else
                            {
                                Console.WriteLine(list.Title + " is not an external list");
                            }
                        }
                        else
                        {
                            Console.WriteLine("List does not exists");
                        }
                        Console.ReadLine();
                    }
                }
            }
        }
    }
     
  10. Hit F5.
  11. Output:

    Sharepoint

Using Powershell:

  1. Go to Start => All Programs => Microsoft SharePoint 2010 products => SharePoint 2010 Management Shell.
  2. Run as an administrator.
  3. Run the following script.

    $site=Get-SPSite "https://servername.com/r/R/"
    $context=[Microsoft.SharePoint.SPServiceContext]::GetContext($site)
    $scope=New-Object Microsoft.SharePoint.SPServiceContextScope($context)
    $web=$site.RootWeb
    $list=$web.Lists.TryGetList("DEV09 External List - Remote")
    if ($list -ne $null)
    {
                if ($list.HasExternalDataSource)
                {
                            write-host $list.Title  " is an external list"
                }
                else
                {
                            write-host $list.Title  " is not an external list"
                }
    }
    else
    {
                write-host "List does not exists"
    }
     
     

Next Recommended Readings