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: , , , ,

Finding the pipeline in which plugin is executing

The InvocationSource property is an integer value that you can use to determine whether the current plug-in is running in a child pipeline or Parent pipeline.

MessageInvocationSource Values

Field : Child
Value : 1
Description : Specifies a child pipeline

Field : Parent
Value : 0
Description : Specifies a parent pipeline

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: , , , , , ,

Multiple Column Sort in Dynamics CRM


Take a look at one of your views.
Select a Column so that the system is sorting on that column
Now move to another column and using the SHIFT, CNTL, MOUSE CLICK you can setup a secondary sort
Now move to another column and do it again..
Guess what: You now have three columns set to sort
So the question of the day then becomes. Which is the primary ?

Labels: , , , ,

ASP.NET Application Deployment Resources

Deployment is the process of distributing a completed application or component to be installed on other computers. Microsoft® ASP.NET is designed to make Web application deployment easy.
Explains how to distribute all files associated with your ASP.NET application to a production server.
Explains how to compile and distribute component assemblies to your ASP.NET application's \Bin directory.
Explains the global assembly cache and how to distribute shared component assemblies to it.
Explains how to distribute compiled assemblies that contain classes that implement the IHttpHandler and IHttpModule interfaces. This includes required configuration settings for including new HTTP handlers and HTTP modules in your application.

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: , , , , , , , , , ,

Wednesday, September 30, 2009

Month Name from Month Number

Getting Month Name from the Month Number is quite easy by extending DateTime properties in .net.

For Example, if you are having a month number, say 2, which you need to convert it into corresponding Month Name(February)




string strMonth = "2";

DateTime date = new DateTime(1, strMonth, 1);

string strMonthName = date.ToString("MMM");



This will return you the month name "Feb" ,but only first three characters.

If you need the month name in full, slightly modify the above syntax



string strMonth = "2";

DateTime date = new DateTime(1, strMonth, 1);

string strMonthName = date.ToString("MMMM"); // Four Characters will give you the full month name.



Happy Coding..

Labels: , , , , , , ,

Thursday, September 24, 2009

Offline Mode of your WebSite

A simple .htm file will make your application offline in no time.

Just place App_Offline.htm file on root of your web application, all requests to your web site will be redirected automatically to this file.

Now your Website is completely Offline!

Labels: , , , , , ,

Handy Extension Methods for ASP.NET MVC's UrlHelper

Mickael Chambaud posted three extension methods he created for UrlHelper: Image(), Stylesheet() and Script(). They make it pretty easy to keep your images, stylesheets and scripts organized in a single location – without the need for you to remember where they are placed. And if you need to move things around for some reason, you only have to change the extension methods.

It takes only a few minutes and will probably save you a lot of massive Search & Replace in the future!


public static string Image(this UrlHelper helper, string fileName)
{
return helper.Content("~/Content/Images/" + fileName));
}

public static string Stylesheet(this UrlHelper helper, string fileName)
{
return helper.Content("~/Content/Stylesheets/" + fileName);
}

public static string Script(this UrlHelper helper, string fileName)
{
return helper.Content("~/Content/Scripts/" + fileName);
}


So instead of doing this:


<link href="../../../Content/StyleSheets/Main.css" rel="stylesheet" type="text/css" />


You can do this :


<link href="<%= UrlHelper.Stylesheet("Main.css")%>" rel="stylesheet" type="text/css" />


Happy Coding...

Labels: , , , , , ,

Credit Card Expiration Date DropDownList Sample Code

Below is the sample code for populating Credit Cart Expiration Date DropDownList.


//Populate the credit card expiration month drop down
for (int i = 1; i <= 12; i++) { DateTime month = new DateTime(2000, i, 1); ListItem li = new ListItem(month.ToString("MMM (M)"), month.ToString("MM")); ExpirationDateMonthDropDown.Items.Add(li); } //Populate the credit card expiration year drop down (go out 12 years) for (int i = 0; i <= 11; i++) { String year = (DateTime.Today.Year + i).ToString(); ListItem li = new ListItem(year, year); ExpirationDateYearDropDown.Items.Add(li); }



Happy Coding...

Labels: , , , , , ,

Display Numerals in Arabic Format

When you are working in multilingual website, especially in arabic language, the following code may be useful to show numbers in arabic format. Because keep in mind, there is no automatic digit localization in ASP.NET. This code will help if you are in situation where you dont want change the regional settings of the PC.


public string ConvertToArabicNumerals(string input)
{
System.Text.UTF8Encoding utf8Encoder = new UTF8Encoding();

System.Text.Decoder utf8Decoder = utf8Encoder.GetDecoder();

System.Text.StringBuilder convertedChars = new System.Text.StringBuilder();

char[] convertedChar = new char[1];

byte[] bytes = new byte[]{217,160};

char[] inputCharArray = input.ToCharArray();

foreach (char c in inputCharArray)

{

if(char.IsDigit(c))

{

bytes[1] = Convert.ToByte(160 + char.GetNumericValue(c));

utf8Decoder.GetChars(bytes, 0, 2, convertedChar, 0);

convertedChars.Append(convertedChar[0]);

}

else

{

convertedChars.Append(c);

}

}

return convertedChars.ToString();
}



Happy Coding...

Labels: , , , , ,

Wednesday, September 23, 2009

Team Foundation PowerToys

The Team Foundation PowerToys (TFPT) application provides extra functionality for use with the Team Foundation version control system. The Team Foundation PowerToys application is not supported by Microsoft.

Five separate operations are supported by the TFPT application: unshelve, rollback, online, getcs, and uu. They are all invoked at the command line using the tfpt.exe application. Some of the TFPT commands have graphical interfaces.

Read More on PowerToys..

Labels: , , , , , ,

Tuesday, September 22, 2009

Chart Control for .net 3.5

Microsoft has launched a new ASP.NET server control - <asp:chart /> - that can be used for free with ASP.NET 3.5 to enable rich browser-based charting scenarios.

Once installed the <asp:chart/> control shows up under the "Data" tab on the Toolbox, and can be easily declared on any ASP.NET page as a standard server control.



<asp:Chart id="chart1" runat="server"/>


<asp:chart /> supports a rich assortment of chart options - including pie, area, range, point, circular, accumulation, data distribution, ajax interactive, doughnut, and more. You can statically declare chart data within the control declaration, or alternatively use data-binding to populate it dynamically. At runtime the server control generates an image (for example a .PNG file) that is referenced from the client HTML of the page using a <img/> element output by the <asp:chart/> control. The server control supports the ability to cache the chart image, as well as save it on disk for persistent scenarios. It does not require any other server software to be installed, and will work with any standard ASP.NET page.

You can download the chart controls from

Microsoft Chart Controls

Tool support from

Tool Support for the Chart Controls

Samples

Chart Control Samples

Documentation

Chart Control Documentation

Labels: , , , ,