Dump the Entity Metadata

This sample shows how to use the MetadataService Web Service. It creates an XML file that provides a snapshot of attributes for a single entity using the Metadata APIs.




[C#]
sing System;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

using Microsoft.Crm.Sdk.Utility;
using CrmSdk;
using MetadataServiceSdk;

namespace Microsoft.Crm.Sdk.HowTo
{
///
/// This sample shows how to retrieve all the metadata and write it to a file.
///

public class HowToMetaData
{
static void Main(string[] args)
{
// TODO: Change the server URL and Organization to match your CRM Server and CRM Organization
HowToMetaData.Run("http://localhost:5555", "CRM_SDK");
}

public static bool Run(string crmServerUrl, string orgName)
{
// Standard MetaData Service Setup
MetadataService service = CrmServiceUtility.GetMetadataService(crmServerUrl, orgName);

#region Setup Data Required for this Sample

bool success = false;

#endregion

try
{
// Retrieve the Metadata
RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest();
request.MetadataItems = MetadataItems.All;
RetrieveAllEntitiesResponse metadata = (RetrieveAllEntitiesResponse)service.Execute(request);


// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter("testfile.xml"))
{
// Create Xml Writer.
XmlTextWriter metadataWriter = new XmlTextWriter(sw);

// Start Xml File.
metadataWriter.WriteStartDocument();

// Metadata Xml Node.
metadataWriter.WriteStartElement("Metadata");

AttributeMetadata currentAttribute;// Declared outside of loop

// Iterate through all entities and add their attributes and relationships to the file.
foreach(EntityMetadata currentEntity in metadata.CrmMetadata)
{
// Start Entity Node
metadataWriter.WriteStartElement("Entity");

// Write the Entity's Information.
metadataWriter.WriteElementString("Name", currentEntity.LogicalName);

if (currentEntity.DisplayName.UserLocLabel != null)
{
metadataWriter.WriteElementString("DisplayName", currentEntity.DisplayName.UserLocLabel.Label);
}
else
{
metadataWriter.WriteElementString("DisplayName", "[NONE]");
}

if (currentEntity.DisplayCollectionName.UserLocLabel != null)
{
metadataWriter.WriteElementString("DisplayCollectionName", currentEntity.DisplayCollectionName.UserLocLabel.Label);
}
else
{
metadataWriter.WriteElementString("DisplayCollectionName", "[NONE]");
}

metadataWriter.WriteElementString("IsCustomEntity", currentEntity.IsCustomEntity.ToString());
metadataWriter.WriteElementString("IsCustomizable", currentEntity.IsCustomizable.ToString());
metadataWriter.WriteElementString("ReportViewName", currentEntity.ReportViewName);
metadataWriter.WriteElementString("PrimaryField", currentEntity.PrimaryField);
metadataWriter.WriteElementString("PrimaryKey", currentEntity.PrimaryKey);

#region Attributes

// Write Entity's Attributes.
metadataWriter.WriteStartElement("Attributes");

for (int j = 0; j < currentEntity.Attributes.Length; j++)
{
// Get Current Attribute.
currentAttribute = currentEntity.Attributes[j];
// Start Attribute Node
metadataWriter.WriteStartElement("Attribute");

// Write Attribute's information.
metadataWriter.WriteElementString("Name", currentAttribute.LogicalName);
metadataWriter.WriteElementString("Type", currentAttribute.AttributeType.ToString());

if (currentAttribute.DisplayName.UserLocLabel != null)
{
metadataWriter.WriteElementString("DisplayName", currentAttribute.DisplayName.UserLocLabel.Label);
}
else
{
metadataWriter.WriteElementString("DisplayName", "[NONE]");
}

metadataWriter.WriteElementString("Description", currentAttribute.IsCustomField.ToString());
metadataWriter.WriteElementString("IsCustomField", currentAttribute.IsCustomField.ToString());
metadataWriter.WriteElementString("RequiredLevel", currentAttribute.RequiredLevel.ToString());
metadataWriter.WriteElementString("ValidForCreate", currentAttribute.ValidForCreate.ToString());
metadataWriter.WriteElementString("ValidForRead", currentAttribute.ValidForRead.ToString());
metadataWriter.WriteElementString("ValidForUpdate", currentAttribute.ValidForUpdate.ToString());

// Write the Default Value if it is set.
if (currentAttribute.DefaultValue != null)
{
metadataWriter.WriteElementString("DefualtValue", currentAttribute.DefaultValue.ToString());
}

// Write the Description if it is set.
if (currentAttribute.Description != null &&
currentAttribute.Description.UserLocLabel != null)
{
metadataWriter.WriteElementString("Description", currentAttribute.Description.UserLocLabel.Label);
}


// Write Type Specific Attribute Information using helper functions.

Type attributeType = currentAttribute.GetType();

if (attributeType == typeof(DecimalAttributeMetadata))
{
writeDecimalAttribute((DecimalAttributeMetadata)currentAttribute, metadataWriter);
}
else if (attributeType == typeof(FloatAttributeMetadata))
{
writeFloatAttribute((FloatAttributeMetadata)currentAttribute, metadataWriter);
}
else if (attributeType == typeof(IntegerAttributeMetadata))
{
writeIntegerAttribute((IntegerAttributeMetadata)currentAttribute, metadataWriter);
}
else if (attributeType == typeof(MoneyAttributeMetadata))
{
writeMoneyAttribute((MoneyAttributeMetadata)currentAttribute, metadataWriter);
}
else if (attributeType == typeof(PicklistAttributeMetadata))
{
writePicklistAttribute((PicklistAttributeMetadata)currentAttribute, metadataWriter);
}
else if (attributeType == typeof(StateAttributeMetadata))
{
writeStateAttribute((StateAttributeMetadata)currentAttribute, metadataWriter);
}
else if (attributeType == typeof(StatusAttributeMetadata))
{
writeStatusAttribute((StatusAttributeMetadata)currentAttribute, metadataWriter);
}
else if (attributeType == typeof(StringAttributeMetadata))
{
writeStringAttribute((StringAttributeMetadata)currentAttribute, metadataWriter);
}

// End Attribute Node
metadataWriter.WriteEndElement();

}
// End Attributes Node
metadataWriter.WriteEndElement();

#endregion

#region References From

metadataWriter.WriteStartElement("OneToMany");

// Get Current ReferencesFrom
foreach (OneToManyMetadata currentRelationship in currentEntity.OneToManyRelationships)
{
// Start ReferencesFrom Node
metadataWriter.WriteStartElement("From");
metadataWriter.WriteElementString("Name", currentRelationship.SchemaName);
metadataWriter.WriteElementString("IsCustomRelationship", currentRelationship.IsCustomRelationship.ToString());
metadataWriter.WriteElementString("ReferencedEntity", currentRelationship.ReferencedEntity);
metadataWriter.WriteElementString("ReferencingEntity", currentRelationship.ReferencingEntity);
metadataWriter.WriteElementString("ReferencedAttribute", currentRelationship.ReferencedAttribute);
metadataWriter.WriteElementString("ReferencingAttribute", currentRelationship.ReferencingAttribute);

// End ReferencesFrom Node
metadataWriter.WriteEndElement();
}

metadataWriter.WriteEndElement();

#endregion

#region References To

metadataWriter.WriteStartElement("ManyToOne");

foreach (OneToManyMetadata currentRelationship in currentEntity.ManyToOneRelationships)
{
// Start ReferencesFrom Node
metadataWriter.WriteStartElement("To");
metadataWriter.WriteElementString("Name", currentRelationship.SchemaName);
metadataWriter.WriteElementString("IsCustomRelationship", currentRelationship.IsCustomRelationship.ToString());
metadataWriter.WriteElementString("ReferencedEntity", currentRelationship.ReferencedEntity);
metadataWriter.WriteElementString("ReferencingEntity", currentRelationship.ReferencingEntity);
metadataWriter.WriteElementString("ReferencedAttribute", currentRelationship.ReferencedAttribute);
metadataWriter.WriteElementString("ReferencingAttribute", currentRelationship.ReferencingAttribute);

// End ReferencesFrom Node
metadataWriter.WriteEndElement();
}

metadataWriter.WriteEndElement();

#endregion

// End Entity Node
metadataWriter.WriteEndElement();
}

// End Metadata Xml Node
metadataWriter.WriteEndElement();
metadataWriter.WriteEndDocument();

// Close xml writer.
metadataWriter.Close();
}

#region check success

if (metadata.CrmMetadata.Length > 0)
{
success = true;
}

#endregion
}
catch (System.Web.Services.Protocols.SoapException)
{
// Add your error handling code here.
}

return success;
}


#region Specific Attribute Helper Functions

// Writes the Decimal Specific Attributes
private static void writeDecimalAttribute (DecimalAttributeMetadata attribute, XmlTextWriter writer)
{
writer.WriteElementString("MinValue", attribute.MinValue.ToString());
writer.WriteElementString("MaxValue", attribute.MaxValue.ToString());
writer.WriteElementString("Precision", attribute.Precision.ToString());
}

// Writes the Float Specific Attributes
private static void writeFloatAttribute (FloatAttributeMetadata attribute, XmlTextWriter writer)
{
writer.WriteElementString("MinValue", attribute.MinValue.ToString());
writer.WriteElementString("MaxValue", attribute.MaxValue.ToString());
writer.WriteElementString("Precision", attribute.Precision.ToString());
}

// Writes the Integer Specific Attributes
private static void writeIntegerAttribute (IntegerAttributeMetadata attribute, XmlTextWriter writer)
{
writer.WriteElementString("MinValue", attribute.MinValue.ToString());
writer.WriteElementString("MaxValue", attribute.MaxValue.ToString());
}

// Writes the Money Specific Attributes
private static void writeMoneyAttribute (MoneyAttributeMetadata attribute, XmlTextWriter writer)
{
writer.WriteElementString("MinValue", attribute.MinValue.ToString());
writer.WriteElementString("MaxValue", attribute.MaxValue.ToString());
writer.WriteElementString("Precision", attribute.Precision.ToString());
}

// Writes the Picklist Specific Attributes
private static void writePicklistAttribute (PicklistAttributeMetadata attribute, XmlTextWriter writer)
{
// Writes the picklist's options
writer.WriteStartElement("Options");

// Writes the attributes of each picklist option
for (int i = 0; i < attribute.Options.Length; i++)
{
writer.WriteStartElement("Option");
writer.WriteElementString("OptionValue", attribute.Options[i].Value.ToString());
writer.WriteElementString("Description", attribute.Options[i].Label.UserLocLabel.Label);
writer.WriteEndElement();
}

writer.WriteEndElement();
}

// Writes the State Specific Attributes
private static void writeStateAttribute (StateAttributeMetadata attribute, XmlTextWriter writer)
{
// Writes the state's options
writer.WriteStartElement("Options");

// Writes the attributes of each picklist option
for (int i = 0; i < attribute.Options.Length; i++)
{
writer.WriteStartElement("Option");
writer.WriteElementString("OptionValue", attribute.Options[i].Value.ToString());
writer.WriteElementString("Description", attribute.Options[i].Label.UserLocLabel.Label);
writer.WriteElementString("DefaultStatus", attribute.Options[i].DefaultStatus.ToString());
writer.WriteEndElement();
}

writer.WriteEndElement();
}

// Writes the Status Specific Attributes
private static void writeStatusAttribute (StatusAttributeMetadata attribute, XmlTextWriter writer)
{
// Writes the status's options
writer.WriteStartElement("Options");

// Writes the attributes of each picklist option
for (int i = 0; i < attribute.Options.Length; i++)
{
writer.WriteStartElement("Option");
writer.WriteElementString("OptionValue", attribute.Options[i].Value.ToString());
writer.WriteElementString("Description", attribute.Options[i].Label.UserLocLabel.Label);
writer.WriteElementString("State", attribute.Options[i].State.ToString());
writer.WriteEndElement();
}

writer.WriteEndElement();
}

// Writes the String Specific Attributes
private static void writeStringAttribute (StringAttributeMetadata attribute, XmlTextWriter writer)
{
writer.WriteElementString("MaxLength", attribute.MaxLength.ToString());
}
#endregion
}
}