Tag Cloud

CRM 2011 (161) CRM 4.0 (144) C# (116) JScript (109) Plugin (92) Registry (90) Techpedia (77) PyS60 (68) WScript (43) Plugin Message (31) Exploit (27) ShellCode (26) FAQ (22) JavaScript (21) Killer Codes (21) Hax (18) VB 6.0 (17) Commands (16) VBScript (16) Quotes (15) Turbo C++ (13) WMI (13) Security (11) 1337 (10) Tutorials (10) Asp.Net (9) Safe Boot (9) Python (8) Interview Questions (6) video (6) Ajax (5) VC++ (5) WebService (5) Workflow (5) Bat (4) Dorks (4) Sql Server (4) Aptitude (3) Picklist (3) Tweak (3) WCF (3) regex (3) Config (2) LINQ (2) PHP (2) Shell (2) Silverlight (2) TSql (2) flowchart (2) serialize (2) ASHX (1) CRM 4.0 Videos (1) Debug (1) FetchXml (1) GAC (1) General (1) Generics (1) HttpWebRequest (1) InputParameters (1) Lookup (1) Offline Plug-ins (1) OutputParameters (1) Plug-in Constructor (1) Protocol (1) RIA (1) Sharepoint (1) Walkthrough (1) Web.config (1) design patterns (1) generic (1) iframe (1) secure config (1) unsecure config (1) url (1)

Pages

Monday, April 08, 2013

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
}
}