Saturday, November 22, 2008

Get attributes display name into DropDownList in CRM 4.0

In CRM 4.0 if you want the label name of a field also called attribute display name, you can get it by the Metadata Service.

Here is the code:

MetadataService service = GetMetadataService(crmServerUrl, orgName);

RetrieveEntityRequest req = new RetrieveEntityRequest();
req.EntityItems = EntityItems.IncludeAttributes;
req.LogicalName = "new_employee"; // enter the name of the entity
RetrieveEntityResponse res = (RetrieveEntityResponse)service.Execute(req);

foreach (AttributeMetadata attribute in res.EntityMetadata.Attributes)
ListItem item = new ListItem();
item.Value = attribute.LogicalName;
item.Text = attribute.DisplayName.LocLabels[0].Label;

if you want only specific fields you can add in the foreach statment

if (attribute.LogicalName == "new_fullname") \\ where new_fullname is your field's name

Hope this was useful


Friday, November 21, 2008

Show user exact age in JavaScript (like 28.34)

If you are working on Human Resources project and needs this info or you just want to be more accurate when displaying age.

Here is the code:

function DisplayAge()
// that's the user's birthday
month = 11;
date = 18;
year = 1980;

today = new Date();

dateStr = today.getDate();
monthStr = today.getMonth()+1;
yearStr = today.getFullYear();
theYear = yearStr - year;
theMonth = monthStr - month;
theDate = dateStr - date;

var age = theYear + theMonth/12 + theDate/365;


Thursday, November 20, 2008

Import Excel file to Dataset or xml

Just a simple code for reading an excel file

using System.Data.OleDb;

public void ReadXLS()
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Data\Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
OleDbConnection myConnection = new OleDbConnection(connectionString);
OleDbDataAdapter myAdapter = new OleDbDataAdapter("Select * From [TABLE$]", myConnection);
DataSet dataset = new DataSet();
dataGridView1.DataSource = dataset.Tables[0].DefaultView;
catch {}

Let users send you email throw GMAIL in your site

You can create a contact us form for example and allow visitors on your site to fill some details and get that data into your gmail account.

using System.Net;
using System.Net.Mail;

protected void SendMail()

MailMessage email = new MailMessage();

MailAddress adressToSend = new MailAddress("your gmail adress");

email.From = new MailAddress("user email adress");
email.Subject = "email subject";
email.Body = " email body ";

SmtpClient client = new SmtpClient("", 587);
client.EnableSsl = true;
NetworkCredential myCreds = new NetworkCredential("your gmail username", "your gmail password");
client.Credentials = myCreds;

catch {}


Hope that was helpful


Pass parameters in QueryString in other languages

In ASP.NET you might have a problem with passing strings in languages other then English.

so what you need to do is:

Add to the web.config file under "system.web" this lines:

<globalization fileEncoding="windows-1255" requestEncoding="windows-1255" responseEncoding="windows-1255"/>

Now windows-1255 is the hebrew encoding, you need to find what's the windows encoding for your language.

Hope that was helpful.


Use the ENTER Key In a TextBox

In most cases you'll want to let the users on the site to click ENTER in your Textbox,
and that will the trigger the same action as if they pressed the button next to the Textbox.

Paste that code on Page_Load.

TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode)
{if ((event.which == 13) || (event.keyCode == 13))
{document.getElementById('" + Button1.UniqueID + "').click();return false;}}
else {return true}; ");

the only 2 parameters that you need to change are:

TextBox1 = your text box.
Button1 = this is the button next to your text box.

So here is what it does bassicly:

It adds to the TextBox an attribute that says:
Add an event OnKeyDown (when you press your keyboard)
then it checks if I pressed on ENTER (which is 13 in ASCI)
and finally it adds the event that our button has which is CLICK to the Textbox ENTER key.

Hope that was helpful

I tried to make it as simple as I could.