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);
}
Step 8 : Add the below method outside of the Main – This is to get the deviceid
credentials for the particular running machine. This is required for CRM
online.
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.
To do this points please go through this complete post.
Try this out and Post your comments...... Thanks.........!!!!!!!!!!!!!