Recently i
was working in CRM 2011 customizations, after completion of the customization ,
i have Exported the solution for my backup purpose. This i planned to automate
daily. So i have created one Asp.net program to Automate this backup process.
The
following sample shows how to export the solution from CRM online and save it
in specified location.
Step 1 :
Create a “Console Application” in Visual studio under Visual C# templates.
Step 2 :
Give the Project and Solution Name, Make sure that .Net Framework must be 4.
Step 3 :
Click “Ok”.
Step 4:
Add the following References to the project from sdk\bin folder
CRM References –
Microsoft.Crm.Sdk.Proxy , Microsoft.Xrm.Sdk
.Net References – System.ServiceModel,
System.Runtime.Serialization,System.Security
Step 5 :
Right Click the “ExportCRMonlineSolution” project and click “Add Existing
Item”.
Select “deviceidmanager.cs” from
the sdk folder. (sdk\samplecode\cs\helpercode)
Your
“Solution Explorer” should look like this.
Step 6
: Add the following Namespace to the
project.
using System.ServiceModel;
using
System.IO;
using
System.ServiceModel.Description;
using
System.Net;
using
Microsoft.Crm.Sdk.Messages;
using
Microsoft.Xrm.Sdk;
using
Microsoft.Xrm.Sdk.Client;
using
Microsoft.Crm.Services.Utility;
Step 7 : Now Add the below code in the Main
string
userName = "<CRM online username>";
string passWord = "<CRM
online password>";
string SolName = "<solution
which is available in online CRM>";
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = userName;
credentials.UserName.Password = passWord;
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
Uri organizationUri = new
Uri("<crm
online organization service url>");
Uri homeRealmUri = null;
# region Getting Current datetime information -- This is
not mandatory we can skip
string fileDate = DateTime.Now.Date.ToShortDateString();
string formatedDate = fileDate.Replace("/", "-");
string fileTime = DateTime.Now.ToShortTimeString().Replace(" ", "-");
string formatedTime = fileTime.ToString().Replace(":", "-");
string finalformat = formatedDate+"_"+formatedTime;
#endregion
using (OrganizationServiceProxy
serviceProxy = new OrganizationServiceProxy(organizationUri,
homeRealmUri, credentials, GetDeviceCredentials()))
{
String outputDir = @"C:\temp\";
ExportSolutionRequest expsolreq = new ExportSolutionRequest();
expsolreq.Managed = false;
expsolreq.SolutionName = SolName;
ExportSolutionResponse expsolres = (ExportSolutionResponse)serviceProxy.Execute(expsolreq);
byte[]
exportXml = expsolres.ExportSolutionFile;
string filename = SolName + "_" + finalformat + ".zip";
File.WriteAllBytes(outputDir +
filename, exportXml);
Console.WriteLine("Solution Exported to {0}", outputDir +
filename);
}
private static ClientCredentials GetDeviceCredentials()
{
return Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
}
Step : 9 Your complete project looks likes the below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.IO;
using System.ServiceModel.Description;
using System.Net;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Services.Utility;
namespace ExportSolution
{
public class Program
{
static public void Main(string[]
args)
{
string userName = "<CRM online username>";
string passWord = "<CRM
online password>";
string SolName = "<solution
which is available in online CRM>";
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = userName;
credentials.UserName.Password = passWord;
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
Uri organizationUri = new
Uri("<crm
online organization service url>");
Uri homeRealmUri = null;
# region Getting Current datetime
information -- This is not mandatory we can skip
string fileDate = DateTime.Now.Date.ToShortDateString();
string formatedDate = fileDate.Replace("/", "-");
string fileTime = DateTime.Now.ToShortTimeString().Replace(" ", "-");
string formatedTime = fileTime.ToString().Replace(":", "-");
string finalformat = formatedDate+"_"+formatedTime;
#endregion
using (OrganizationServiceProxy
serviceProxy = new OrganizationServiceProxy(organizationUri,
homeRealmUri, credentials, GetDeviceCredentials()))
{
String outputDir = @"C:\temp\";
ExportSolutionRequest expsolreq = new ExportSolutionRequest();
expsolreq.Managed = false;
expsolreq.SolutionName = SolName;
ExportSolutionResponse expsolres = (ExportSolutionResponse)serviceProxy.Execute(expsolreq);
byte[] exportXml =
expsolres.ExportSolutionFile;
string filename = SolName + "_" + finalformat + ".zip";
File.WriteAllBytes(outputDir +
filename, exportXml);
Console.WriteLine("Solution Exported to {0}", outputDir +
filename);
}
}
private static ClientCredentials GetDeviceCredentials()
{
return Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
}
}
}
Step 10 : Now Build the solution, also check that
Framework should be in 4.
Step 11 : Press F5 to run the program. Solution zip
file must be saved in the mentioned output directory.
Also we can do
- Create a publisher.
- Retrieve the default publisher.
- Create a solution.
- Retrieve a solution.
- Add an existing solution component.
- Remove a solution component.
- Install or upgrade a solution.
- Delete a solution.
Try this out and Post your comments...... Thanks.........!!!!!!!!!!!!!
I'm having trouble getting this code to work, exception error on the
ReplyDeleteusing (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, GetDeviceCredentials()))
Can you help me understand where / what the SolName value should be as i'm struggling to find it.
Hi,
ReplyDeleteYou have to add "DeviceidManager.cs" from sdk. Also make sure you have added, "Microsoft.Xrm.Sdk"and other required references.
Please try this or send me ur project to my mailid. (karthikmathis@gmail.com").
I think great site for these post and i am read the most of contents have useful for my Carrier.Thanks for these useful information.Any information are commands like to share him.
ReplyDeleteCRM Software in India
CRM Software in Chennai