代码段:确定某个列表是否为服务器上的外部列表

上次修改时间: 2010年4月20日

适用范围: SharePoint Server 2010

本文内容
说明
必备组件
使用该示例

说明

以下代码段演示了如何确定列表是否是外部列表。

必备组件

  • 服务器上安装了 Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010。

  • Microsoft Visual Studio。

  • BDC 元数据存储中至少注册了一种外部内容类型,并且存在基于该外部内容类型的外部列表。

    备注

    该示例中使用的外部列表无法使用传递身份验证。

使用该示例

  1. 启动 Visual Studio,然后创建一个 C# 控制台应用程序项目。在创建项目时,选择".NET Framework 3.5"。

  2. 从"视图"菜单中,单击"属性页"以显示项目属性。

  3. 在"生成"选项卡中,为"目标平台"选择"任何 CPU"。

  4. 关闭项目属性窗口。

  5. 在"解决方案资源管理器"中的"引用"下,移除除 System 和 System.Core 之外的所有项目引用。

  6. 将以下引用添加到项目中:

    1. Microsoft.SharePoint

    2. System.Web

  7. 用此过程末尾列出的代码替换 Program.cs 中自动生成的代码。

  8. 将 <siteUrl> 字符串值替换为有效值。

  9. 保存该项目。

  10. 编译并运行该项目。

using System;
using System.Web;
using Microsoft.SharePoint;

namespace Microsoft.SDK.SharePoint.Samples.Bdc.ExternalList
{
    class Program
    {
        static void Main(string[] args)
        {
            using(SPSite site = new SPSite("<siteUrl>"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                SPServiceContextScope contextScope = new SPServiceContextScope(context);

                SPWeb web = site.OpenWeb();
                foreach(SPList list in web.Lists)
                {
                    Console.Write("List: " + list.Title + " is ");
                    if (list.HasExternalDataSource)
                    {
                        Console.WriteLine("an external list");
                    }
                    else
                    {
                        Console.WriteLine("a regular list");
                    }
                }
            }
        }
    }
}

请参阅

概念

设置上下文便于使用 BDC 对象模型