The MetadataService is not immune to the myriad of error conditions that may arise while you are customizing the schema in the user interface. Developers need to ensure that their applications can respond gracefully to errors issued by the MetadataService. As a SOAP Web service, the MetadataService returns errors as System.Web.Services.Protocols.SoapException, as shown in the following code. Figure 8-14 shows the console output.
MetadataService metadataService = GetMetadataService("contoso"); //Create Request DeleteAttributeRequest request = new DeleteAttributeRequest(); request.EntityLogicalName = "new_project"; request.LogicalName = "new_cost"; //Attempt the Execute the Request try { metadataService.Execute(request); } catch (System.Web.Services.Protocols.SoapException ex) { XmlDocument doc = new XmlDocument(); doc.LoadXml(ex.Detail.InnerXml); Console.WriteLine("Error XML: " + doc.OuterXml); Console.WriteLine(); string code = doc["error"]["code"].InnerText; string description = doc["error"]["description"].InnerText; string type = doc["error"]["type"].InnerText; Console.WriteLine("Error Code: " + code); Console.WriteLine(); Console.WriteLine("Error Description: " + description); Console.WriteLine(); Console.WriteLine("Error Type: " + type); }
CRM’s error codes are published in the SDK. You can view a complete list of codes at http://msdn.microsoft.com/en-us/library/bb930493.aspx. These codes include those for the MetadataService.