web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Friyank’s blog / Enable audit for Entity MS-...

Enable audit for Entity MS-CRM (C#.NET)

Friyank Profile Picture Friyank 944

 

To connect CRM check my blog here

To Create Entity check my blog here

Only Code No Talks

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Configuration;
namespace AuditableEntityBlog
{
    class Program
    {
        static void Main(string[] args)
        {
            CrmServiceClient crmServiceClientObj = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CrmOnlineStringFromAppConfig"].ConnectionString);
            if (!crmServiceClientObj.IsReady)
                Console.WriteLine("No Connection was Made.");
            Console.WriteLine("Connected");
            string entityName = "new_AuditableEntity";
            CreateEntityRequest createrequest = new CreateEntityRequest
            {
                Entity = new EntityMetadata
                {
                    SchemaName = entityName,
                    DisplayName = new Label("Auditable Entity", 1033),
                    DisplayCollectionName = new Label("Auditables", 1033),
                    Description = new Label("An entity to store information about Auditable Entity", 1033),
                    OwnershipType = OwnershipTypes.UserOwned,
                    IsActivity = false, < strong > IsAuditEnabled = new BooleanManagedProperty(true) </ strong > },
                PrimaryAttribute = new StringAttributeMetadata
                {
                    SchemaName = "new_name",
                    RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                    MaxLength = 100,
                    FormatName = StringFormatName.Text,
                    DisplayName = new Label("Adviser Name", 1033),
                    Description = new Label("The primary attribute for the Auditable entity.", 1033)
                },
            };
            PublishXmlRequest publishEntityRequest = new PublishXmlRequest
            {
                ParameterXml = String.Format("{0}", entityName.ToLower())
            }; try
            {
                var result = crmServiceClientObj.Execute(createrequest);
                if (result.Results != null)
                    Console.WriteLine("Auditable Entity Created"); crmServiceClientObj.Execute(publishEntityRequest);
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to Create Entity. \nReason {0}", e.Message);
            }
            Console.ReadLine();
        }
    }
}


Comments

*This post is locked for comments