Compartilhar via


Code Snippet: Add an Access Control Entry to a MetadataObject Using the Administration Object Model

Applies to: SharePoint Server 2010

In this article
Description
Prerequisites
To use this example

Description

The following code example shows how to programmatically add an access control entry to a metadata object (model in this example) using the BDC Administration object model on the server.

Note

You can use the BDC Administration Client Object Model to create an external content type similarly on the client.

Prerequisites

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 on the server.

  • Microsoft .NET Framework 3.5 and Microsoft Visual Studio on the client computer.

  • At least one external content type registered in the BDC Metadata Store.

To use this example

  1. Start Visual Studio and create a C# Console application project. Select .NET Framework 3.5 when you create the project.

  2. From the View menu, click Property Pages to bring up the project properties.

  3. In the Build tab, for the Platform target, select Any CPU.

  4. Close the project properties window.

  5. In Solution Explorer, under References, remove all project references except for System and System.Core.

  6. Add the following references to the project:

    1. Microsoft.BusinessData

    2. Microsoft.SharePoint

    3. System.Web

  7. Replace the autogenerated code in Program.cs with the code listed at the end of this procedure.

  8. Replace the "<siteUrl>" string value with a valid SharePoint site URL.

  9. Replace the "<EntityNamespace>" and "<EntityName>" with the namespace and entity name of an existing entity.

  10. Save the project.

  11. Compile and run the project.

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);
        }

    }
}

See Also

Reference

BdcService

Services

GetAdministrationMetadataCatalog(SPServiceContext)

AdministrationMetadataCatalog

GetEntity(String, String)

Entity

GetAccessControlList()

SetAccessControlList(IAccessControlList)

CopyAclAcrossChildrenForSetting(String)

IAccessControlList

IndividualAccessControlEntry

IAccessControlEntry

LobSystem