如何:将访问控制项添加到元数据对象

必须授予用户相关权限,使其能够在元数据对象上执行方法、编辑设置和设置权限。此代码示例演示如何使用对象模型来添加元数据对象的访问控制项。第一个示例演示为当前用户添加访问控制项。第二个示例演示添加示例中所述的用户。请注意如何使用特殊字符 (|) 将权限组合授予用户。

示例

此示例演示为当前用户和指定用户添加访问控制项。

先决条件

项目引用

在运行此示例前在您的控制台应用程序代码项目中添加以下项目引用:

  • Microsoft.SharePoint

  • Microsoft.SharePoint.Portal

  • Microsoft.Office.Server

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server.ApplicationRegistry.Administration;
using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;
using WSSAdmin = Microsoft.SharePoint.Administration;
using OSSAdmin = Microsoft.Office.Server.Administration;

namespace Microsoft.SDK.SharePointServer.Samples
{
    class GetStartedAndCreateSystem
    {
        const string yourSSPName ="EnterYourSSPNameHere";
        const string userName ="domainname\\username";

        static void Main(string[] args)
        {
            SetupBDC();
            SetAccessControlListForCurrentUser();
            SetAccessControlListForSpecifiedUser();
            Console.WriteLine("Press any key to exit...");
            Console.Read();
        }
        static void SetupBDC()
        {
            SqlSessionProvider.Instance().SetSharedResourceProviderToUse(yourSSPName);
        }
        public static void SetAccessControlListForCurrentUser()
        {
            LobSystemInstance mySysInstance = null;
            LobSystemInstanceCollection sysInsCollection = ApplicationRegistry.Instance.GetLobSystemInstancesLikeName("AdventureWorksSampleFromCode");
            foreach (LobSystemInstance sysInstance in sysInsCollection)
            {
                if (sysInstance.Name == "AdventureWorksSampleFromCode")
                {
                    mySysInstance = sysInstance;
                    break;
                }
            }
            LobSystem ls = mySysInstance.LobSystem;
            IAccessControlList acl = ls.GetAccessControlList();
            String currentIdentity = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            acl.Add(new IndividualAccessControlEntry(currentIdentity, BdcRights.SetPermissions | BdcRights.Execute));
            try
            {
                ls.SetAccessControlList(acl);
            }

            catch (AccessDeniedException)
            {
            }
        }

        public static void SetAccessControlListForSpecifiedUser()
        {
            LobSystemInstance mySysInstance = null;
            LobSystemInstanceCollection sysInsCollection = ApplicationRegistry.Instance.GetLobSystemInstancesLikeName("AdventureWorksSampleFromCode");
            foreach (LobSystemInstance sysInstance in sysInsCollection)
            {
                if (sysInstance.Name == "AdventureWorksSampleFromCode")
                {
                    mySysInstance = sysInstance;
                    break;
                }
            }
            LobSystem ls = mySysInstance.LobSystem;
            IAccessControlList acl = ls.GetAccessControlList();
            //replace the domain and user names here
            String currentIdentity = userName;
            acl.Add(new IndividualAccessControlEntry(currentIdentity, BdcRights.SetPermissions | BdcRights.UseInBusinessDataInLists | BdcRights.SelectableInClients));
            try
            {
                ls.SetAccessControlList(acl);
            }
            catch (AccessDeniedException)
            {
                //your exception handling code here
            }
            Console.WriteLine("Done");
        }
    }
}

See Also

概念

业务数据目录:元数据模型