Compartilhar via


Sample: Enable field security for an entity

 

Applies To: Dynamics CRM 2013

This sample code is for Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online. Download the Microsoft Dynamics CRM SDK package. It can be found in the following location in the download package:

SampleCode\CS\FieldSecurity\EnableFieldSecurityForAnEntity.cs

SampleCode\VB\FieldSecurity\EnableFieldSecurityForAnEntity.vb

Requirements

For more information about the requirements for running the sample code provided in this SDK, see Use the sample and helper code.

Example

This sample shows how to enable field security for an entity.


// Connect to the Organization service. 
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
    // This statement is required to enable early-bound type support.
    _serviceProxy.EnableProxyTypes();

    CreateRequiredRecords();

    // Create Field Security Profile.
    FieldSecurityProfile managersProfile = new FieldSecurityProfile();
    managersProfile.Name = "Managers";
    _profileId = _serviceProxy.Create(managersProfile);
    Console.Write("Created Profile, ");

    // Create the request object and set the monikers with the
    // teamprofiles_association relationship.
    AssociateRequest teamToProfile = new AssociateRequest
    {
        Target = new EntityReference(FieldSecurityProfile.EntityLogicalName, _profileId),
        RelatedEntities = new EntityReferenceCollection
        {
            new EntityReference(Team.EntityLogicalName, _teamId)
        },
        Relationship = new Relationship("teamprofiles_association")
    };

    // Execute the request.
    _serviceProxy.Execute(teamToProfile);

    // Create custom activity entity.
    CreateEntityRequest req = new CreateEntityRequest()
    {
        Entity = new EntityMetadata
        {
            LogicalName = "new_tweet",
            DisplayName = new Label("Tweet", 1033),
            DisplayCollectionName = new Label("Tweet", 1033),
            OwnershipType = OwnershipTypes.UserOwned,
            SchemaName = "New_Tweet",
            IsActivity = true,
            IsAvailableOffline = true,
            IsAuditEnabled = new BooleanManagedProperty(true),
            IsMailMergeEnabled = new BooleanManagedProperty(false)
        },
        HasActivities = false,
        HasNotes = true,
        PrimaryAttribute = new StringAttributeMetadata()
        {
            SchemaName = "Subject",
            LogicalName = "subject",
            RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
            MaxLength = 100,
            DisplayName = new Label("Subject", 1033)
        }
    };

    // Execute the request.
    _serviceProxy.Execute(req);
    Console.Write("Entity Created, ");

    // Create custom attributes.
    CreateAttributeRequest attrReq = new CreateAttributeRequest()
    {
        Attribute = new StringAttributeMetadata()
        {
            LogicalName = "new_identity",
            DisplayName = new Label("Identity", 1033),
            SchemaName = "New_Identity",
            MaxLength = 500,
            RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended),
            IsSecured = true
        },
        EntityName = "new_tweet"
    };

    // Execute the request.
    CreateAttributeResponse identityAttributeResponse = (CreateAttributeResponse)_serviceProxy.Execute(attrReq);
    _identityId = identityAttributeResponse.AttributeId;
    Console.Write("Identity Created, ");

    attrReq = new CreateAttributeRequest()
    {
        Attribute = new StringAttributeMetadata()
        {
            LogicalName = "new_message",
            DisplayName = new Label("Message", 1033),
            SchemaName = "New_Message",
            MaxLength = 140,
            RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended),
            IsSecured = true
        },
        EntityName = "new_tweet"
    };

    // Execute the request.
    CreateAttributeResponse messageAttributeResponse = (CreateAttributeResponse)_serviceProxy.Execute(attrReq);
    _messageId = messageAttributeResponse.AttributeId;
    Console.Write("Message Created, ");

    // Create the field permission for the Identity attribute.
    FieldPermission identityPermission = new FieldPermission()
    {
        AttributeLogicalName = "new_identity",
        EntityName = "new_tweet",
        CanRead = new OptionSetValue(FieldPermissionType.Allowed),
        FieldSecurityProfileId = new EntityReference(FieldSecurityProfile.EntityLogicalName, _profileId)
    };

    // Execute the request
    _identityPermissionId = _serviceProxy.Create(identityPermission);
    Console.Write("Permission Created. ");

    DeleteRequiredRecords(promptforDelete);
}

' Connect to the Organization service. 
' The using statement assures that the service proxy will be properly disposed.
_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)
Using _serviceProxy
    ' This statement is required to enable early-bound type support.
    _serviceProxy.EnableProxyTypes()

    CreateRequiredRecords()

    ' Create Field Security Profile.
    Dim managersProfile As New FieldSecurityProfile()
    managersProfile.Name = "Managers"
    _profileId = _serviceProxy.Create(managersProfile)
    Console.Write("Created Profile, ")

    ' Create the request object and set the monikers with the
    ' teamprofiles_association relationship.
    Dim teamToProfile As AssociateRequest = New AssociateRequest With { _
        .Target = New EntityReference(FieldSecurityProfile.EntityLogicalName, _profileId), _
        .RelatedEntities = New EntityReferenceCollection From {New EntityReference(Team.EntityLogicalName, _teamId)}, _
        .Relationship = New Relationship("teamprofiles_association")}

    ' Execute the request.
    _serviceProxy.Execute(teamToProfile)


    ' Create custom activity entity.
    Dim req As New CreateEntityRequest() With {.Entity = New EntityMetadata With { _
            .LogicalName = "new_tweet", .DisplayName = New Label("Tweet", 1033), _
            .DisplayCollectionName = New Label("Tweet", 1033), .OwnershipType = OwnershipTypes.UserOwned, _
            .SchemaName = "New_Tweet", .IsActivity = True, .IsAvailableOffline = True, _
            .IsAuditEnabled = New BooleanManagedProperty(True), .IsMailMergeEnabled = New BooleanManagedProperty(False)}, _
            .HasActivities = False, .HasNotes = True, .PrimaryAttribute = New StringAttributeMetadata() With { _
                .SchemaName = "Subject", .LogicalName = "subject", _
                .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), _
                .MaxLength = 100, .DisplayName = New Label("Subject", 1033)}}

    ' Execute the request.
    _serviceProxy.Execute(req)
    Console.Write("Entity Created, ")

    ' Create custom attributes.
    Dim attrReq As New CreateAttributeRequest() With {.Attribute = New StringAttributeMetadata() With { _
            .LogicalName = "new_identity", .DisplayName = New Label("Identity", 1033), .SchemaName = "New_Identity", _
            .MaxLength = 500, .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended), _
            .IsSecured = True}, .EntityName = "new_tweet"}

    ' Execute the request.
    Dim identityAttributeResponse As CreateAttributeResponse = CType(_serviceProxy.Execute(attrReq), CreateAttributeResponse)
    _identityId = identityAttributeResponse.AttributeId
    Console.Write("Identity Created, ")

    attrReq = New CreateAttributeRequest() With {.Attribute = New StringAttributeMetadata() With { _
            .LogicalName = "new_message", .DisplayName = New Label("Message", 1033), .SchemaName = "New_Message", _
            .MaxLength = 140, .RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.Recommended), _
            .IsSecured = True}, .EntityName = "new_tweet"}

    ' Execute the request.
    Dim messageAttributeResponse As CreateAttributeResponse = CType(_serviceProxy.Execute(attrReq), CreateAttributeResponse)
    _messageId = messageAttributeResponse.AttributeId
    Console.Write("Message Created, ")

    ' Create the field permission for the Identity attribute.
    Dim identityPermission As New FieldPermission() With {.AttributeLogicalName = "new_identity", .EntityName = "new_tweet", _
        .CanRead = New OptionSetValue(FieldPermissionType.Allowed), _
        .FieldSecurityProfileId = New EntityReference(FieldSecurityProfile.EntityLogicalName, _profileId)}

    ' Execute the request
    _identityPermissionId = _serviceProxy.Create(identityPermission)
    Console.Write("Permission Created. ")

    DeleteRequiredRecords(promptforDelete)
End Using

See Also

IOrganizationService
How field security can be used to control access to field values in Microsoft Dynamics CRM 2013
Field security entities
Sample: Retrieve field sharing records