Tuesday, 1 May 2012

Code to create Custom Activity and Attributes in CRM 2011



In this article , I am going to explain how to Create Custom Activity Entity and Attribute


For Better Understanding I divided this article in two part
(i)      Create the custom activity entity
(ii)     Add attributes to the custom activity entity


Namespace need to include 
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Crm.Sdk.Messages;

Create the custom activity entity
CreateEntityRequest request = new CreateEntityRequest

{
    HasNotes = true,
    HasActivities = false,
    PrimaryAttribute = new StringAttributeMetadata
    {
        SchemaName = "Subject",
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
        MaxLength = 100,
        DisplayName = new Label("Subject", 1033)
    },
    Entity = new EntityMetadata
    {
        IsActivity = true,
        SchemaName = customEntityName,
        DisplayName = new Label("Instant Message", 1033),
        DisplayCollectionName = new Label("Instant Messages", 1033),
        OwnershipType = OwnershipTypes.UserOwned,
        IsAvailableOffline = true,

    }
};

_serviceProxy.Execute(request);



Add an attribute to the custom activity entity
CreateAttributeRequest fontFamilyAttributeRequest =

    new CreateAttributeRequest
    {
        EntityName = customEntityName,
        Attribute = new StringAttributeMetadata
        {
            SchemaName = prefix + "fontfamily",
            DisplayName = new Label("Font Family", 1033),
            MaxLength = 100
        }
    };
CreateAttributeResponse fontFamilyAttributeResponse =
    (CreateAttributeResponse)_serviceProxy.Execute(
    fontFamilyAttributeRequest);

Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,

                                                    serverConfig.HomeRealmUri,
                                                    serverConfig.Credentials,
                                                    serverConfig.DeviceCredentials))
{
    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                 
    String prefix = "new_";
    String customEntityName = prefix + "instantmessage";

    // Create the custom activity entity.
    CreateEntityRequest request = new CreateEntityRequest
    {
        HasNotes = true,
        HasActivities = false,
        PrimaryAttribute = new StringAttributeMetadata
        {
            SchemaName = "Subject",
            RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
            MaxLength = 100,
            DisplayName = new Label("Subject", 1033)
        },
        Entity = new EntityMetadata
        {
            IsActivity = true,
            SchemaName = customEntityName,
            DisplayName = new Label("Instant Message", 1033),
            DisplayCollectionName = new Label("Instant Messages", 1033),
            OwnershipType = OwnershipTypes.UserOwned,
            IsAvailableOffline = true,

        }
    };

    _serviceProxy.Execute(request);

    //Entity must be published

    // Add few attributes to the custom activity entity.
    CreateAttributeRequest fontFamilyAttributeRequest =
        new CreateAttributeRequest
        {
            EntityName = customEntityName,
            Attribute = new StringAttributeMetadata
            {
                SchemaName = prefix + "fontfamily",
                DisplayName = new Label("Font Family", 1033),
                MaxLength = 100
            }
        };
    CreateAttributeResponse fontFamilyAttributeResponse =
        (CreateAttributeResponse)_serviceProxy.Execute(
        fontFamilyAttributeRequest);

    CreateAttributeRequest fontColorAttributeRequest =
        new CreateAttributeRequest
        {
            EntityName = customEntityName,
            Attribute = new StringAttributeMetadata
            {
                SchemaName = prefix + "fontcolor",
                DisplayName = new Label("Font Color", 1033),
                MaxLength = 50
            }
        };
    CreateAttributeResponse fontColorAttributeResponse =
        (CreateAttributeResponse)_serviceProxy.Execute(
        fontColorAttributeRequest);

    CreateAttributeRequest fontSizeAttributeRequest =
        new CreateAttributeRequest
        {
            EntityName = customEntityName,
            Attribute = new IntegerAttributeMetadata
            {
                SchemaName = prefix + "fontSize",
                DisplayName = new Label("Font Size", 1033)
            }
        };
    CreateAttributeResponse fontSizeAttributeResponse =
        (CreateAttributeResponse)_serviceProxy.Execute(
        fontSizeAttributeRequest);
}


No comments:

Post a Comment

Note: only a member of this blog may post a comment.