below is the way to read Entity Attributes and Display in GridView
// please connect to your CRM organization using appropriate URL
IOrganizationService crmservice;
CrmConnection crmconn = new CrmConnection();
crmservice = crmconn.Connect2CRMNew(orguri);
RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.All,
LogicalName = entityname,
RetrieveAsIfPublished = true
};
RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)crmservice.Execute(retrieveEntityRequest);
EntityMetadata currentEntity = retrieveEntityResponse.EntityMetadata;
// Attributes
DataTable dtmain = new DataTable();
dtmain.Columns.Add(new DataColumn("Schema name"));
dtmain.Columns.Add(new DataColumn("Data Type"));
dtmain.Columns.Add(new DataColumn("Size"));
dtmain.Columns.Add(new DataColumn("Required Level"));
dtmain.Columns.Add(new DataColumn("Is Audit Enabled"));
dtmain.Columns.Add(new DataColumn("Is Custom Attribute"));
dtmain.Columns.Add(new DataColumn("AttributeID"));
foreach(AttributeMetadata allattributes in currentEntity.Attributes)
{
DataRow dtrow = dtmain.NewRow();
dtrow[0] = allattributes.LogicalName.ToString();
dtrow[3] = allattributes.RequiredLevel.Value.ToString();
dtrow[4] = allattributes.IsAuditEnabled.Value.ToString();
dtrow[5] = allattributes.IsCustomAttribute.Value.ToString();
dtrow[6] = allattributes.MetadataId.Value.ToString();
dtrow[1] = allattributes.AttributeType.Value;
switch (allattributes.AttributeType.Value.ToString())
{
//case "Money":
// dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.MoneyAttributeMetadata)(allattributes)).MaxValue.ToString();
// break;
//case "Virtual":
// dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.V)(allattributes)).MaxLength.ToString();
// break;
case "Decimal":
dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.DecimalAttributeMetadata)(allattributes)).MaxValue.ToString();
break;
//case "Boolean":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.BooleanAttributeMetadata)(allattributes)).
// break;
//case "State":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.StateAttributeMetadata)(allattributes)).
// break;
case "String":
dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.StringAttributeMetadata)(allattributes)).MaxLength.ToString();
break;
//case "DateTime":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.DateTimeAttributeMetadata)(allattributes)).
// break;
case "Integer":
dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.IntegerAttributeMetadata)(allattributes)).MaxValue.ToString();
break;
//case "Lookup":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.LookupAttributeMetadata)(allattributes)).
// break;
//case "Uniqueidentifier":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.Uniq)(allattributes)).
// break;
//case "Picklist":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)(allattributes)).OptionSet.
// break;
};
dtmain.Rows.Add(dtrow);
}
gvAttributes.DataSource = dtmain.DefaultView;
gvAttributes.DataBind();
pnlattribute.Visible = true;
Session.Add("TaskTable",dtmain);
// please connect to your CRM organization using appropriate URL
IOrganizationService crmservice;
CrmConnection crmconn = new CrmConnection();
crmservice = crmconn.Connect2CRMNew(orguri);
RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.All,
LogicalName = entityname,
RetrieveAsIfPublished = true
};
RetrieveEntityResponse retrieveEntityResponse = (RetrieveEntityResponse)crmservice.Execute(retrieveEntityRequest);
EntityMetadata currentEntity = retrieveEntityResponse.EntityMetadata;
// Attributes
DataTable dtmain = new DataTable();
dtmain.Columns.Add(new DataColumn("Schema name"));
dtmain.Columns.Add(new DataColumn("Data Type"));
dtmain.Columns.Add(new DataColumn("Size"));
dtmain.Columns.Add(new DataColumn("Required Level"));
dtmain.Columns.Add(new DataColumn("Is Audit Enabled"));
dtmain.Columns.Add(new DataColumn("Is Custom Attribute"));
dtmain.Columns.Add(new DataColumn("AttributeID"));
foreach(AttributeMetadata allattributes in currentEntity.Attributes)
{
DataRow dtrow = dtmain.NewRow();
dtrow[0] = allattributes.LogicalName.ToString();
dtrow[3] = allattributes.RequiredLevel.Value.ToString();
dtrow[4] = allattributes.IsAuditEnabled.Value.ToString();
dtrow[5] = allattributes.IsCustomAttribute.Value.ToString();
dtrow[6] = allattributes.MetadataId.Value.ToString();
dtrow[1] = allattributes.AttributeType.Value;
switch (allattributes.AttributeType.Value.ToString())
{
//case "Money":
// dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.MoneyAttributeMetadata)(allattributes)).MaxValue.ToString();
// break;
//case "Virtual":
// dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.V)(allattributes)).MaxLength.ToString();
// break;
case "Decimal":
dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.DecimalAttributeMetadata)(allattributes)).MaxValue.ToString();
break;
//case "Boolean":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.BooleanAttributeMetadata)(allattributes)).
// break;
//case "State":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.StateAttributeMetadata)(allattributes)).
// break;
case "String":
dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.StringAttributeMetadata)(allattributes)).MaxLength.ToString();
break;
//case "DateTime":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.DateTimeAttributeMetadata)(allattributes)).
// break;
case "Integer":
dtrow[2] = ((Microsoft.Xrm.Sdk.Metadata.IntegerAttributeMetadata)(allattributes)).MaxValue.ToString();
break;
//case "Lookup":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.LookupAttributeMetadata)(allattributes)).
// break;
//case "Uniqueidentifier":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.Uniq)(allattributes)).
// break;
//case "Picklist":
// //dtrow[6] = ((Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata)(allattributes)).OptionSet.
// break;
};
dtmain.Rows.Add(dtrow);
}
gvAttributes.DataSource = dtmain.DefaultView;
gvAttributes.DataBind();
pnlattribute.Visible = true;
Session.Add("TaskTable",dtmain);
No comments:
Post a Comment