Wednesday, March 24, 2010

Move a section from one tab to another tab

//get the section you want to move
var sectionToReplace = crmForm.all.ppp_keywords.parentElement.parentElement.parentElement;
//this code will place the above section below the specified section
crmForm.all.ppp_dealeractions.parentElement.parentElement.parentElement.appendChild(aa);

Hide a section on the CRM Form

crmForm.all.ppp_keywords.parentElement.parentElement.parentElement.style.display = 'none';

Sunday, March 21, 2010

IFRAME content in different domain

In one of my requirement I had to include an IFRAME on the CRMForm. The IFRAME url points to a website in a different domain. The website had a JavaScript code which would populate value on two fields in the CRM Form.

Since the website and CRM Form were on different domains the JavaScript within the website could not access the fields on the CRM Form. So I had to write the below code on the website and CRMForm which made the domains same.

document.domain = "domain.com";

Post to a page

var HPtoken = null;
var xmlHttpReq = false;
var self = this;

if (window.XMLHttpRequest)
{
self.xmlHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', 'http://somewebsite.com', true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function()
{
if (self.xmlHttpReq.readyState == 4)
{
alert(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(null);

Thursday, March 11, 2010

SQL Connection

// This example needs the
// System.Data.SqlClient library

#region Building the connection string

string Server = "localhost";
string Username = "my_username";
string Password = "my_password";
string Database = "my_database";

string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;

#endregion


#region Try to establish a connection to the database

SqlConnection SQLConnection = new SqlConnection();

try
{
SQLConnection.ConnectionString = ConnectionString;
SQLConnection.Open();

// You can get the server version
// SQLConnection.ServerVersion
}
catch (Exception Ex)
{
// Try to close the connection
if (SQLConnection != null)
SQLConnection.Dispose();

// Create a (useful) error message
string ErrorMessage = "A error occurred while trying to connect to the server.";
ErrorMessage += Environment.NewLine;
ErrorMessage += Environment.NewLine;
ErrorMessage += Ex.Message;

// Show error message (this = the parent Form object)
MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);

// Stop here
return;
}

#endregion


#region Execute a SQL query

string SQLStatement = "SELECT * FROM ExampleTable";

// Create a SqlDataAdapter to get the results as DataTable
SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(SQLStatement, SQLConnection);

// Create a new DataTable
DataTable dtResult = new DataTable();

// Fill the DataTable with the result of the SQL statement
SQLDataAdapter.Fill(dtResult);

// Loop through all entries
foreach (DataRow drRow in dtResult.Rows)
{
// Show a message box with the content of
// the "Name" column
MessageBox.Show(drRow["Name"].ToString());
}

// We don't need the data adapter any more
SQLDataAdapter.Dispose();

#endregion


#region Close the database link

SQLConnection.Close();
SQLConnection.Dispose();

#endregion

Sunday, February 28, 2010

Get Object Type Code + C#

To get the Object type code dynamically using C#

public int GetObjectTypeCode(string entityName)
{
CrmMetaDataService.CrmAuthenticationToken token = new CrmMetaDataService.CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "NZ";

// Create the metadata Web service.
MetadataService metadataService = new MetadataService();
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
metadataService.PreAuthenticate = true;

RetrieveEntityRequest entityRequest = new RetrieveEntityRequest();
entityRequest.RetrieveAsIfPublished = false;
entityRequest.LogicalName = entityName;
entityRequest.EntityItems = EntityItems.EntityOnly;
RetrieveEntityResponse entityResponse = (RetrieveEntityResponse)metadataService.Execute(entityRequest);

// Access the retrieved entity
EntityMetadata retrievedEntityMetadata = entityResponse.EntityMetadata;
return retrievedEntityMetadata.ObjectTypeCode.Value;

}

Thursday, February 25, 2010

The key specified to compute a hash value is expired, only active keys are valid. Expired Key : CrmKey

I got the following message when i tried to logon to MS CRM.



My "Microsoft CRM Asychronous Processing Service" was stopped and when i started it i was able to log-on to MS CRM