Hi I need your advice, I customize some CRM 4.0 plugin to CRM 2013. I am newbie and I did not any exprience with CRM 4.0 My Question is ; I am trying customize a plugin to CRM 2013 . In this plugin there many coding for connection with database and I want to know am I neeed so much coding in CRM 2013 I attached some coding snip from CRM 4.0 and what I use for CRM 2013. Please advice me am I need all coding also for CRM 2013.
CRM 4.0 Database connection coding:
public static string connectToDB(string orgaName) {
string confDataConString;
using (RegistryKey mscrmKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\MSCRM")) {
if (mscrmKey == null) {
throw new InvalidOperationException(@"RegKey gibts nich.");
}
confDataConString = (string)mscrmKey.GetValue("configdb", String.Empty);
if (String.IsNullOrEmpty(confDataConString)) {
throw new InvalidOperationException(@"confingDB gibts nich.");
}
}
using (SqlConnection con = new SqlConnection(confDataConString)) {
using (SqlCommand cmd = new SqlCommand("SELECT ConnectionString FROM Organization WHERE UniqueName=@orgName", con)) {
cmd.Parameters.Add(new SqlParameter("orgName", orgaName));
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) {
DataTable organization = new DataTable();
adapter.Fill(organization);
if (organization.Rows.Count == 0) {
throw new InvalidOperationException(String.Format("Keine org die {0} heisst", orgaName));
}
if (organization.Rows.Count > 1) {
throw new InvalidOperationException(String.Format("Es gibt zu viele {0}´s", orgaName));
}
return (string)organization.Rows[0]["ConnectionString"];
}
}
}
}
public static SqlConnection getDBConnection(string orgaName) {
string DBConnection = connectToDB(orgaName);
string[] DBConn = DBConnection.Split(';');
string s = "";
for (int i = 1; i < DBConn.Length; i++) {
s = s + DBConn[i] + ";";
}
DBConnection = s;
SqlConnection con = new SqlConnection(DBConnection);
return con;
}
public static SqlConnection getCRMDBConnection(string orgaName) {
string DBConnection = connectToDB(orgaName);
string[] DBConn = DBConnection.Split(';');
string s = "";
for (int i = 1; i < DBConn.Length; i++) {
s = s + DBConn[i] + ";";
}
DBConnection = s;
SqlConnection con = new SqlConnection(DBConnection);
return con;
}
public static CrmService GetCrmService(string orgName, string server) {
CrmAuthenticationToken took = new CrmAuthenticationToken();
took.AuthenticationType = 0; //->Active Directory
took.OrganizationName = orgName;
CrmService serv = new CrmService();
serv.CrmAuthenticationTokenValue = took;
serv.Credentials = System.Net.CredentialCache.DefaultCredentials;
serv.CrmAuthenticationTokenValue = took;
serv.Url = string.Format("http://{0}/mscrmservices/2007/crmservice.asmx", server);
return serv;
}
and what I use in CRM 2013 instead of above coding:
public void Execute(IServiceProvider serviceProvider)
{
string frage = "";
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
.
.
.
.
public static SqlConnection getDBConnection()
{
string connectionstring = "Initial Catalog=TQUGROUP_MSCRM;Data Source=TQU-SQL;Integrated Security=SSPI;";
SqlConnection con = new SqlConnection(connectionstring);
return con;
}
public static int GetIntFromDB(string frage, SqlConnection con)
{
SqlCommand Kommando = new SqlCommand();
string Kommandotext = frage;
Kommando.Connection = con;
Kommando.CommandText = Kommandotext;
con.Open();
int ergebnis = 0;
try
{
ergebnis = System.Convert.ToInt32(Kommando.ExecuteScalar());
}
catch (Exception e)
{
// Log("SQL Abfrage " + frage + " hat keinen Wert zurückgegeben.");
// Log("Fehler: " + e.Message);
}
con.Close();
// Log("SQL Abfrage " + frage + " hat " + ergebnis + " zurückgegeben.");
return ergebnis;
}
Any insight would be greatly appreciated
*This post is locked for comments
I have the same question (0)