Retrieve the Metadata
This sample shows how to use the MetadataService Web Service. It creates an XML file that provides a snapshot of the database using the Metadata APIs. All relationships and attributes for all of the entities are retrieved.
Example
[C#]
using System;
using MetadataServiceSdk;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
namespace Microsoft.Crm.Sdk.HowTo
{
/// <summary>
/// This sample shows how to retrieve all the metadata,
/// and then write it to a file.
/// </summary>
public class HowToMetaData
{
public HowToMetaData()
{
}
public static bool Run()
{
// Standard MetaData Service Setup
MetadataService service = new MetadataService();
service.Credentials
= System.Net.CredentialCache.DefaultCredentials;
#region Setup Data Required for this Sample
bool success = false;
#endregion
try
{
// Retrieve the MetaData
Metadata metadata
= service.RetrieveMetadata(MetadataFlags.All);
// 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");
// Declare variables outside of the loop.
EntityMetadata currentEntity;
AttributeMetadata currentAttribute;
RelationshipMetadata currentRelationship;
// Iterate through all entities and add their attributes
// and relationships to the file
for(int i = 0; i < metadata.Entities.Length; i++)
{
// Get current Entity
currentEntity = metadata.Entities[i];
// Start Entity Node
metadataWriter.WriteStartElement("Entity");
// Write Entity's Information
metadataWriter.WriteElementString("Name",
currentEntity.Name);
metadataWriter.WriteElementString("DisplayName",
currentEntity.DisplayName);
metadataWriter.WriteElementString("DisplayCollectionName",
currentEntity.DisplayCollectionName);
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.Name);
metadataWriter.WriteElementString("Type",
currentAttribute.Type.ToString());
metadataWriter.WriteElementString("DisplayName",
currentAttribute.DisplayName);
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 Default Value if it is set
if (currentAttribute.DefaultValue != null)
{
metadataWriter.WriteElementString("DefualtValue",
currentAttribute.DefaultValue.ToString());
}
// Write Description if it is set
if (currentAttribute.Description != null)
{
metadataWriter.WriteElementString("Description",
currentAttribute.Description);
}
// Write Type Specific Attribute Information using
// helper functions below
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("ReferencesFrom");
for (int j = 0; j < currentEntity.ReferencesFrom.Length; j++)
{
// Get Current ReferencesFrom
currentRelationship
= currentEntity.ReferencesFrom[j];
// Start ReferencesFrom Node
metadataWriter.WriteStartElement("From");
metadataWriter.WriteElementString("Name",
currentRelationship.Name);
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("ReferencesTo");
for (int j = 0; j < currentEntity.ReferencesTo.Length; j++)
{
// Get Current ReferencesFrom
currentRelationship = currentEntity.ReferencesTo[j];
// Start ReferencesFrom Node
metadataWriter.WriteStartElement("To");
metadataWriter.WriteElementString("Name",
currentRelationship.Name);
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.Entities.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)
{
writer.WriteElementString("MinValue",
attribute.NextValue.ToString());
// 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].OptionValue.ToString());
writer.WriteElementString("Description",
attribute.Options[i].Description);
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].OptionValue.ToString());
writer.WriteElementString("Description",
attribute.Options[i].Description);
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)
{
writer.WriteElementString("MinValue",
attribute.NextValue.ToString());
// 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].OptionValue.ToString());
writer.WriteElementString("Description",
attribute.Options[i].Description);
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
}
}
[Visual Basic .NET]
Imports System
Imports MetadataServiceSdk
Imports System.Xml.Serialization
Imports System.Xml
Imports System.IO
Namespace Microsoft.Crm.Sdk.HowTo
' <summary>
' This sample shows how to retrieve all the metadata
' and then write it to a file.
' </summary>
Public Class HowToMetaData
Public Sub New()
End Sub 'New
Public Shared Function Run() As Boolean
' Standard MetaData Service Setup
Dim service As New MetadataService()
service.Credentials
= System.Net.CredentialCache.DefaultCredentials
'---- Setup Data Required for this Sample -----------------------
Dim success As Boolean = False
'----------------------------------------------------------------
Try
' Retrieve the MetaData
Dim metadata As Metadata
= service.RetrieveMetadata(MetadataFlags.All)
' Create an instance of StreamWriter to write text to a file.
' The using statement also closes the StreamWriter.
Dim sw As New StreamWriter("testfile.xml")
Try
' Create Xml Writer
Dim metadataWriter As New XmlTextWriter(sw)
' Start Xml File
metadataWriter.WriteStartDocument()
' Metadata Xml Node
metadataWriter.WriteStartElement("Metadata")
' Declare variables outside of the loop.
Dim currentEntity As EntityMetadata
Dim currentAttribute As AttributeMetadata
Dim currentRelationship As RelationshipMetadata
' Iterate through all entities and add their attributes
' and relationships to the file.
Dim i As Integer
For i = 0 To metadata.Entities.Length - 1
' Get current Entity
currentEntity = metadata.Entities(i)
' Start Entity Node
metadataWriter.WriteStartElement("Entity")
' Write Entity's Information
metadataWriter.WriteElementString("Name",
currentEntity.Name)
metadataWriter.WriteElementString("DisplayName",
currentEntity.DisplayName)
metadataWriter.WriteElementString("DisplayCollectionName",
currentEntity.DisplayCollectionName)
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)
'---- Attributes ---------------------------------------
' Write Entity's Attributes
metadataWriter.WriteStartElement("Attributes")
Dim j As Integer
For j = 0 To currentEntity.Attributes.Length - 1
' Get Current Attribute
currentAttribute = currentEntity.Attributes(j)
' Start Attribute Node
metadataWriter.WriteStartElement("Attribute")
' Write Attribute's Information
metadataWriter.WriteElementString("Name",
currentAttribute.Name)
metadataWriter.WriteElementString("Type",
currentAttribute.Type.ToString())
metadataWriter.WriteElementString("DisplayName",
currentAttribute.DisplayName)
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 Default Value if it is set
If Not (currentAttribute.DefaultValue Is Nothing) Then
metadataWriter.WriteElementString("DefualtValue",
currentAttribute.DefaultValue.ToString())
End If
' Write Description if it is set
If Not (currentAttribute.Description Is Nothing) Then
metadataWriter.WriteElementString("Description",
currentAttribute.Description)
End If
' Write Type Specific Attribute Information using helper functions below
Dim attributeType As Type
= currentAttribute.GetType()
If attributeType Is GetType(DecimalAttributeMetadata) Then
writeDecimalAttribute(CType(currentAttribute,
DecimalAttributeMetadata), metadataWriter)
ElseIf attributeType Is GetType(FloatAttributeMetadata) Then
writeFloatAttribute(CType(currentAttribute,
FloatAttributeMetadata), metadataWriter)
ElseIf attributeType Is GetType(IntegerAttributeMetadata) Then
writeIntegerAttribute(CType(currentAttribute,
IntegerAttributeMetadata), metadataWriter)
ElseIf attributeType Is GetType(MoneyAttributeMetadata) Then
writeMoneyAttribute(CType(currentAttribute,
MoneyAttributeMetadata), metadataWriter)
ElseIf attributeType Is GetType(PicklistAttributeMetadata) Then
writePicklistAttribute(CType(currentAttribute,
PicklistAttributeMetadata), metadataWriter)
ElseIf attributeType Is GetType(StateAttributeMetadata) Then
writeStateAttribute(CType(currentAttribute,
StateAttributeMetadata), metadataWriter)
ElseIf attributeType Is GetType(StatusAttributeMetadata) Then
writeStatusAttribute(CType(currentAttribute,
StatusAttributeMetadata), metadataWriter)
ElseIf attributeType Is GetType(StringAttributeMetadata) Then
writeStringAttribute(CType(currentAttribute,
StringAttributeMetadata), metadataWriter)
End If
' End Attribute Node
metadataWriter.WriteEndElement()
Next j
' End Attributes Node
metadataWriter.WriteEndElement()
'-------------------------------------------------------
'---- References From ----------------------------------
metadataWriter.WriteStartElement("ReferencesFrom")
For j = 0 To currentEntity.ReferencesFrom.Length - 1
' Get Current ReferencesFrom
currentRelationship = currentEntity.ReferencesFrom(j)
' Start ReferencesFrom Node
metadataWriter.WriteStartElement("From")
metadataWriter.WriteElementString("Name",
currentRelationship.Name)
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()
Next j
metadataWriter.WriteEndElement()
'-------------------------------------------------------
'---- References To ------------------------------------
metadataWriter.WriteStartElement("ReferencesTo")
For j = 0 To currentEntity.ReferencesTo.Length - 1
' Get Current ReferencesFrom
currentRelationship = currentEntity.ReferencesTo(j)
' Start ReferencesFrom Node
metadataWriter.WriteStartElement("To")
metadataWriter.WriteElementString("Name",
currentRelationship.Name)
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()
Next j
metadataWriter.WriteEndElement()
'-------------------------------------------------------
' End Entity Node
metadataWriter.WriteEndElement()
Next i
' End Metadata Xml Node
metadataWriter.WriteEndElement()
metadataWriter.WriteEndDocument()
' close xml writer
metadataWriter.Close()
Finally
sw.Close()
End Try
'---- check success ------------------------------------------
If metadata.Entities.Length > 0 Then
success = True
End If
'-------------------------------------------------------------
Catch e As System.Web.Services.Protocols.SoapException
' Add your error handling code here...
End Try
Return success
End Function 'Run
#Region "Specific Attribute Helper Functions"
' Writes the Decimal Specific Attributes
Private Shared Sub writeDecimalAttribute(attribute As DecimalAttributeMetadata,
writer As XmlTextWriter)
writer.WriteElementString("MinValue",
attribute.MinValue.ToString())
writer.WriteElementString("MaxValue",
attribute.MaxValue.ToString())
writer.WriteElementString("Precision",
attribute.Precision.ToString())
End Sub 'writeDecimalAttribute
' Writes the Float Specific Attributes
Private Shared Sub writeFloatAttribute(attribute As FloatAttributeMetadata,
writer As XmlTextWriter)
writer.WriteElementString("MinValue",
attribute.MinValue.ToString())
writer.WriteElementString("MaxValue",
attribute.MaxValue.ToString())
writer.WriteElementString("Precision",
attribute.Precision.ToString())
End Sub 'writeFloatAttribute
' Writes the Integer Specific Attributes
Private Shared Sub writeIntegerAttribute(attribute As IntegerAttributeMetadata,
writer As XmlTextWriter)
writer.WriteElementString("MinValue",
attribute.MinValue.ToString())
writer.WriteElementString("MaxValue",
attribute.MaxValue.ToString())
End Sub 'writeIntegerAttribute
' Writes the Money Specific Attributes
Private Shared Sub writeMoneyAttribute(attribute As MoneyAttributeMetadata,
writer As XmlTextWriter)
writer.WriteElementString("MinValue",
attribute.MinValue.ToString())
writer.WriteElementString("MaxValue",
attribute.MaxValue.ToString())
writer.WriteElementString("Precision",
attribute.Precision.ToString())
End Sub 'writeMoneyAttribute
' Writes the Picklist Specific Attributes
Private Shared Sub writePicklistAttribute(attribute As PicklistAttributeMetadata,
writer As XmlTextWriter)
writer.WriteElementString("MinValue",
attribute.NextValue.ToString())
' Writes the picklist's options
writer.WriteStartElement("Options")
' Writes the attributes of each picklist option
Dim i As Integer
For i = 0 To attribute.Options.Length - 1
writer.WriteStartElement("Option")
writer.WriteElementString("OptionValue",
attribute.Options(i).OptionValue.ToString())
writer.WriteElementString("Description",
attribute.Options(i).Description)
writer.WriteEndElement()
Next i
writer.WriteEndElement()
End Sub 'writePicklistAttribute
' Writes the State Specific Attributes
Private Shared Sub writeStateAttribute(attribute As StateAttributeMetadata,
writer As XmlTextWriter)
' Writes the state's options
writer.WriteStartElement("Options")
' Writes the attributes of each picklist option
Dim i As Integer
For i = 0 To attribute.Options.Length - 1
writer.WriteStartElement("Option")
writer.WriteElementString("OptionValue",
attribute.Options(i).OptionValue.ToString())
writer.WriteElementString("Description",
attribute.Options(i).Description)
writer.WriteElementString("DefaultStatus",
attribute.Options(i).DefaultStatus.ToString())
writer.WriteEndElement()
Next i
writer.WriteEndElement()
End Sub 'writeStateAttribute
' Writes the Status Specific Attributes
Private Shared Sub writeStatusAttribute(attribute As StatusAttributeMetadata,
writer As XmlTextWriter)
writer.WriteElementString("MinValue",
attribute.NextValue.ToString())
' Writes the status's options
writer.WriteStartElement("Options")
' Writes the attributes of each picklist option
Dim i As Integer
For i = 0 To attribute.Options.Length - 1
writer.WriteStartElement("Option")
writer.WriteElementString("OptionValue",
attribute.Options(i).OptionValue.ToString())
writer.WriteElementString("Description",
attribute.Options(i).Description)
writer.WriteElementString("State",
attribute.Options(i).State.ToString())
writer.WriteEndElement()
Next i
writer.WriteEndElement()
End Sub 'writeStatusAttribute
' Writes the String Specific Attributes
Private Shared Sub writeStringAttribute(attribute As StringAttributeMetadata,
writer As XmlTextWriter)
writer.WriteElementString("MaxLength",
attribute.MaxLength.ToString())
End Sub 'writeStringAttribute
#End Region
End Class 'HowToMetaData
End Namespace 'Microsoft.Crm.Sdk.HowTo