代码段:使用管理对象模型将访问控制项添加到 MetadataObject

上次修改时间: 2010年5月7日

适用范围: SharePoint Server 2010

本文内容
说明
先决条件
使用此示例

说明

以下代码示例说明如何使用服务器上的 BDC 管理对象模型以编程方式将访问控制项添加到元数据对象(此示例中的模型)。

备注

可以使用 BDC 管理客户端对象模型以类似方式在客户端上创建外部内容类型。

先决条件

  • Microsoft SharePoint Server 2010 或 Microsoft SharePoint Foundation 2010 位于服务器上。

  • Microsoft .NET Framework 3.5 和 Microsoft Visual Studio 位于客户端计算机上。

  • BDC 元数据存储中至少已注册一个外部内容类型。

使用此示例

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

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

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

  4. 关闭项目属性窗口。

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

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

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  7. 将 Program.cs 中自动生成的代码替换为此过程结束时列出的代码。

  8. 将"<siteUrl>"字符串值替换为有效的 SharePoint 网站 URL。

  9. 将"<EntityNamespace>"和"<EntityName>"替换为现有实体的命名空间和实体名。

  10. 保存项目。

  11. 编译并运行项目。

using System;
using Microsoft.BusinessData.Infrastructure;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.Administration;
using Microsoft.SharePoint.BusinessData.Infrastructure;
using Microsoft.SharePoint.BusinessData.SharedService;

namespace Microsoft.SDK.SharePoint.Samples.Bdc.AddAccessControlEntry
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the Catalog for the SharePoint site.
            BdcService service =
                SPFarm.Local.Services.GetValue<BdcService>(
                String.Empty);
            SPSite site = new SPSite("<siteUrl>");
            SPServiceContext context = SPServiceContext.GetContext(site);

            AdministrationMetadataCatalog catalog =
                service.GetAdministrationMetadataCatalog(context);

            // Retrieve an existing Entity.
            Entity entity = catalog.GetEntity(
                "<EntityNamespace>", "<EntityName>");

            // Add Execute permissions for a specified user to the Entity.
            Console.WriteLine(
                "Type the user account to add Execute Rights Access: ");
            string userAccount = Console.ReadLine();

            IAccessControlList acl = entity.GetAccessControlList();
            Console.WriteLine(
                "Adding " + userAccount + 
                "with Execute and Set Permission rights to entity: " + entity.Name);
            IAccessControlEntry ace = new IndividualAccessControlEntry(
                BdcAccessControlList.TranslateFriendlyStringToEncodedClaim(
                userAccount),
                BdcRights.Execute | BdcRights.SetPermissions);
            acl.Add(ace);
            entity.SetAccessControlList(acl);
            // Copy entity permissions to its methods so that they can 
            // be executed by the added user.
            entity.CopyAclAcrossChildren();

            // Retrieve the LobSystem for this entity and add the user 
            // to it as well. This will allow the added user to access 
            // the external source for retrieving data.
            LobSystem lobSystem = entity.LobSystem;
            IAccessControlList lobSystemAcl = 
                lobSystem.GetAccessControlList();
            lobSystemAcl.Add(ace);
            lobSystem.SetAccessControlList(lobSystemAcl);
        }

    }
}

请参阅

引用

BdcService

Services

GetAdministrationMetadataCatalog(SPServiceContext)

AdministrationMetadataCatalog

GetEntity(String, String)

Entity

GetAccessControlList()

SetAccessControlList(IAccessControlList)

CopyAclAcrossChildrenForSetting(String)

IAccessControlList

IndividualAccessControlEntry

IAccessControlEntry

LobSystem