Thursday, December 30, 2010

Add Tooltip to form fields

To add a tooltip at some field on form, add following snippet in the form On Load event.

var element = document.getElementById("firstname_c");
element.title = "This is the First Name field.";

Happy Coding...

Labels: , , , ,

Wednesday, December 29, 2010

Get selected item(s) in CRM grid

The following will retrieve an array containing the Id values of the selected records in the grid:
// get array of selected records
var a = document.all['crmGrid'].InnerGrid.SelectedRecords;
var selectedItems = new Array(a.length);
for (var i=0; i < a.length; i++)
{
selectedItems[i] = a[i][0];
}
alert(selectedItems);

//To get all of the records in the grid (ie. “All Records on Current Page”):
// array of all records on current page
var iTotal = document.all['crmGrid'].InnerGrid.NumberOfRecords;
var o = document.all['crmGrid'].InnerGrid;
var allItems = new Array;
var ii = 0;
for (var i=0; i < iTotal; i++)
{
allItems[ii] = o.rows[i].oid;
ii++;
}
alert(allItems);

Happy coding...

Labels: , , , , , , ,

CrmDateTime conversion to DateTime

1.Convert.ToDateTime(crmdatetime.Value)
2.DateTime.Parse(crmdatetime.Value)

For more information on the CrmDateTime, look in the SDK:

Check here

Labels: , , , , , , ,

Tuesday, December 28, 2010

Dynamic generation of Word(.DOCX) file

Just came across a problem few days back when creating dynamic word(.docx) file. After creation, the com object was still sitting there in the process list, which gave me some weird messages everytime and it always creates the new instance of the word object. I tried a lot and atlast found this solution in MSDN. We need to release the com object finally after completion of our work. Use the below function in your code and call this after you have completed the dynamic generation process.


private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch { }
finally
{
o = null;
}
}


Microsoft.Office.Interop.Word.ApplicationClass oWord = new Microsoft.Office.Interop.Word.ApplicationClass();

*****
Your logic for dynamic generation of word file(in my case)
*****

NAR(oWord);

You are done. Now it should work as expected.

Happy coding..

Labels: , , , , , , , , ,

Friday, November 20, 2009

Change the default entity in Lookup Window

For instance, in Opportunity form of Microsoft CRM if you want to change the “Potential Customer” field’s default entity from Account to Contact. You need to use the following piece of code in onLoad event of the Opportunity Form.

if ( crmForm.all.customerid != null )
{
    crmForm.all.customerid .setAttribute(”defaulttype”, “2″);
}

Remember to enable the event and then save and publish the opportunity entity.
 Following are codes of basic entities of Microsoft CRM 3 ;
  • Account    1
  • Contact     2
  • Lead         4
Thanks to Umar for pointing out this.

Happy Coding...

Labels: , , , , , ,

Display a Google Map for an Account

Find the below code to add google map for an account in Dynamics CRM 4.0.

Make a new tab with an iFrame, add a custom bit value (yes or no radio buttons) called "shmap" and add this code as an onchange event.


if (crmForm.all.address1_postalcode.DataValue != null){
crmForm.all.IFRAME_gmap.src = "http://maps.google.com/maps?q=" +
crmForm.all.address1_line1.DataValue + "+" + crmForm.all.address1_city.DataValue + ",+" + crmForm.all.address1_postalcode.DataValue;
}
else
{
crmForm.all.IFRAME_gmap.src = "about:blank"
}

Thanks to Glen..

Happy Coding...

Labels: , , , , ,

Friday, October 23, 2009

Progress Bar in Dynamics CRM 4.0

To create a progress bar in Dynamics CRM 4.0 like the one below





You need the following


2.step.gif  
 

3.statusbar.gif - You can get this from the /_imgs/ folder in CRM

For step.gif, right click the .gif file and save it on your system.

crmprogressbar.js

function crmProgressBar(id) {
this.bar = $("#" + id);

this.bar.css({
'height': '23px',
'width': '357px',
'background': 'transparent url(img/statusbar.gif) no-repeat'
});

this.bar.find("div").css({
'height': '19px',
'width': '1px',
'background': 'transparent url(img/step.gif) repeat-x',
'position': 'relative',
'top': '2px',
'left': '3px'
});

this.step = function(percentage) {
var width = parseInt((percentage / 100) * 351);
if (width > 351) { width = 351; }

this.bar.find("div").css({ 'width': width + 'px' });
}
}

To implement follow the steps below.

1.Create a new html file
2.Create a new javascript file and copy the above code into it
3.Include a reference to the jQuery javascript library
4.Include a reference to the javascript in your html file
5.Add a "div" tag to the html file and give it an "id"
6.Add another "div" tag inside the "div" you created in step 4. and put a blank space
7.To initialize the progress bar; create a new variable to hold the progress bar, then create a new instance of the progress bar by specifying the "id" of the div you created in step 4.
eg: var progressBar1 = new crmProgressBar("id-of-div");
8.To step/increment the progress bar use the step() instance method
eg: progressBar1.step(10); // will increment to 10%;



<div id="p1">
<div>
 </div>
</div>

<script type="text/javascript">
var i = 5;
var cpb = null;

$(document).ready(function() {
cpb = new crmProgressBar("p1");
increment();
});

function increment() {
if (i <= 100) {
i += 5;
cpb.step(i);
setTimeout(increment, 1000);
}
}

</script>


Happy Coding..

You can find gperera's entry on Blog

Labels: , , , , ,

Tuesday, October 20, 2009

Backup, Restore & Publish Dynamics CRM Customizations Programmatically

Thanks to gperera in giving out this wonderful class.

Find the excerpts..

Here is a simple class you can use to backup, restore and publish dynamics crm customizations programmatically. Please keep in mind that dynamics crm customizations are additive, which means, if you import a set of customizations lets say a new attribute on the account entity and you restore a backup of the old customizations the new attribute on the account entity that was imported will not be deleted.

CrmCustomizations customizations = new CrmCustomizations(service);

You can backup to an xml file or a zip file by calling the Backup method. Backup method takes care of creating the xml or zip file by looking at the output file extension.

bool backedup = customizations.Backup(@".\customizations_backup.xml");

 or to a zip file

backedup = customizations.Backup(@".\customizations_backup.zip");

To restore a backup call the Restore method. You can pass it a .zip file //or a .xml file path.

bool restored = customizations.Restore(@".\customizations.xml");

Once you have restored you need to publish the customizations, to publish call the Publish method. It will publish all customizations.

bool published = customizations.Publish();

Find the class file below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.SdkTypeProxy;
using System.IO;
using System.IO.Compression;

public class CrmCustomizations
{
private CrmService _service = null;
public CrmCustomizations(CrmService service)
{
_service = service;
}

public bool Backup(string filePath)
{
bool backedup = false;

if (_service != null)
{
// grab all the customizations first
ExportAllXmlRequest request = new ExportAllXmlRequest();
ExportAllXmlResponse response = _service.Execute(request) as ExportAllXmlResponse;

byte[] data = Encoding.ASCII.GetBytes(response.ExportXml);

// prepare the backup file
if (File.Exists(filePath)) { File.Delete(filePath); }
FileStream fs = File.Create(filePath);

// check if we need to create a zip file
bool createZip = Path.GetExtension(filePath).ToLower().Equals(".zip");
if (createZip)
{
GZipStream gzs = new GZipStream(fs, CompressionMode.Compress, true);
gzs.Write(data, 0, data.Length);
gzs.Close();
}
else
{
fs.Write(data, 0, data.Length);
}
fs.Close();

backedup = true;
}

return backedup;
}

public bool Restore(string filePath)
{
bool restored = false;

if (_service != null && File.Exists(filePath))
{
string customizationXml = ReadData(filePath);
if (!string.IsNullOrEmpty(customizationXml))
{
ImportAllXmlRequest request = new ImportAllXmlRequest { CustomizationXml = customizationXml };
//_service.Execute(request);

restored = true;
}
}

return restored;
}

public bool Publish()
{
bool published = false;

if (_service != null)
{
PublishAllXmlRequest request = new PublishAllXmlRequest();
_service.Execute(request);

published = true;
}

return published;
}

private string ReadData(string filePath)
{
FileStream fs = File.Open(filePath, FileMode.Open);
byte[] data = new byte[fs.Length];

bool isZip = Path.GetExtension(filePath).ToLower().Equals(".zip");
if (isZip)
{
GZipStream gzs = new GZipStream(fs, CompressionMode.Decompress, true);
// decompress
MemoryStream stream = new MemoryStream();
byte[] b = new byte[4096];
while (true)
{
int n = gzs.Read(b, 0, b.Length);
if (n > 0) { stream.Write(b, 0, n); }
else { break; }
}

data = stream.ToArray();

stream.Close();
gzs.Close();
}
else
{
fs.Read(data, 0, data.Length);
}

fs.Close();

return Encoding.ASCII.GetString(data);
}
}

Happy coding...

Labels: , , , , , , , , , ,