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

Wednesday, April 03, 2013

Serialize and Deserialize Entities

This sample shows how to do standard serialization in Microsoft.NET Framework using Microsoft Dynamics CRM objects.

 

//CRM4.0 Serialize and Deserialize Entities
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;

namespace Microsoft.Crm.Sdk.HowTo
{
///
/// The following sample demonstrates how to serialize a Microsoft Dynamics CRM contact into XML.
/// and then how to deserialize the XML back into a Microsoft Dynamics CRM contact. The sample performs
/// the following steps:
///
/// - Creates a contact to serialize.
/// - Retrieves the contact from Microsoft Dynamics CRM.
/// - Serializes the contact and writes the resulting XML to a file.
/// - Loads the XML file and deserializes it back into a Microsoft Dynamics CRM contact.
/// - Updates the contact in Microsoft Dynamics CRM.
///
/// NOTE: This process does not create Microsoft Dynamics CRM v1.x compatible XML.
///

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

public static bool Run(string crmServerUrl, string orgName)
{
// Set up the CRM Service.
CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

#region Setup Data Required for this Sample

bool success = false;

#endregion

try
{
#region Create a temporary contact for this sample

// Create a contact entity that will be serialized into XML.
contact contactCreate = new contact();
contactCreate.firstname = "Jesper";
contactCreate.lastname = "Aaberg";
contactCreate.jobtitle = "Doctor";

// Create the entity in Microsoft Dynamics CRM and get its ID.
Guid contactId = service.Create(contactCreate);

#endregion

#region Retrieve the contact from CRM

// Create the column set object that indicates the fields to be retrieved.
ColumnSet columns = new ColumnSet();
columns.Attributes = new string [] {"contactid",
"firstname",
"lastname",
"jobtitle"};

// Retrieve the contact from Microsoft Dynamics CRM
// using the ID of the record that was just created.
// The EntityName indicates the EntityType of the object being retrieved.
contact contact = (contact)service.Retrieve(EntityName.contact.ToString(),
contactId,
columns);

#endregion

#region Serialize the contact into XML and save it

// Serialize the contact into XML and write it to the hard drive.
XmlSerializer serializer = new XmlSerializer(typeof(contact));

// Create a unique file name for the XML.
string filename = "Contact_" + contact.contactid.Value.ToString("B") + ".xml";

// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter writer = new StreamWriter(filename))
{
// Write the XML to disk.
serializer.Serialize(writer, contact);
}

#endregion

#region Deserialize the CRM contact from XML

// Declare a Microsoft Dynamics CRM contact.
contact restoredContact;

using (StreamReader reader = new StreamReader(filename))
{
// Deserialize the contact object.
object tmp = serializer.Deserialize(reader);

// Cast the object into a Microsoft Dynamics CRM contact.
restoredContact = tmp as contact;
}

#endregion

#region Update contact in CRM

// Update the contact in Microsoft Dynamics CRM to verify that the deserialization worked.
restoredContact.jobtitle = "Plumber";
service.Update(restoredContact);

#endregion

#region Delete Data Required for this Sample
service.Delete(EntityName.contact.ToString(), contactId);
#endregion

#region Unit Test: Check if all is ok (test suite)

// If some key things are OK, return true.
if (filename != null &&
filename.Length > "Contact_.XML".Length &&
restoredContact != null)
{
return true;
}
else
{
return false;
}

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

return success;
}
}
}