Wednesday, May 20, 2009

Creating your custom solution for MS CRM 4.0 - Programmatically fetch Isv Config and Sitemap for an organization

The CRM SDK provides ExportXmlRequest in order to export any customizations programatically. For example, the isv config for an organization can be exported as :
 CrmServiceRef.ExportXmlRequest exportRequest = new CrmServiceRef.ExportXmlRequest();

// Define the nodes to retrieve, the site map and the isv configuration.

exportRequest.ParameterXml = @"<importexportxml>
<entities></entities>
<nodes>
<node>isvconfig</node>
</nodes>
<securityroles></securityroles>
<settings></settings>
<workflows></workflows>
</importexportxml>";

// Execute the request.
CrmServiceRef.ExportXmlResponse isvconfigXml = (CrmServiceRef.ExportXmlResponse)crmService.Execute(exportRequest);
Alternatively, in order to read the contents of the ISV Config or Sitemap for any organization using code, the "organizationid" attribute for the organization class can be used as well. This is explained as follows:

In order to read the isv.config content :
1. Use the Discovery service to get the organization id for the organization.
2. Query the "isvconfig" entity (use QueryByAtrribute based on the attribute "organizationid") and fetch the "configxml" attribute. This returns the xml content for the isv.config file of a particular organization.

To access the sitemap xml content:
1. Use the Discovery service to get the organization id for the organization.
2. Fetch the organization instance using CrmService.Retrieve based on organization id.
3. Read the "sitemapxml" attribute of the organization retrieved.
The code sample here illustrates this:
CrmServiceRef.ColumnSet cols = new CrmServiceRef.ColumnSet();
cols.Attributes = new string[] { strpSITEMAPXML };

CrmServiceRef.organization org = (CrmServiceRef.organization)crmService.Retrieve(CrmServiceRef.EntityName.organization.ToString(),organizationid, cols);
CrmServiceRef.ColumnSet cols = new CrmServiceRef.ColumnSet();
if (org != null)
{
String strSitemapBackupPath = @"C:\Sitemap Backup\orgSitemap.xml";
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter(strSitemapBackupPath))
{
// Write the customization XML to the file
sw.Write(org.sitemapxml);
}
}

1 comment:

  1. Acetech has many years of experience in custom software development. Find out more about custom software development at http://www.acetechindia.com

    ReplyDelete