Enable audit for Entity MS-CRM (C#.NET)
Views (568)
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();
}
}
}

Like
Report
*This post is locked for comments