Tuesday, August 05, 2008

Add Account Company logo on Microsoft CRM 3.0



Normaly Company want to know the logo of the Account, want to feel close to final client, by default Microsoft CRM dont give you a field to add a photo of your Account or contact.
So i make this litle code, using external tool like Access, page of ASP.net, and configuring a iframe.
This trick save me a lot of times.

1ºStep
Create a new Database Where i collect information on Access.

Create a new Table add the following columns
Id=Guid of Account (Text)
Foto=Binary of the Photo(The image will be save on you Access file)



2º Step
On Microsoft CRM Configuration, select Account, and Form.
There create a Iframe please select the option to object code to the url, like "ID={Guid}"
Example:
(you Put)Iframe=/photo/photo.aspx
When navigatin the result will be something like this
http://[server]/photo/photo.aspx?id={Guid}.....
This is very usefull, explore and you will like the final result.
After This, publish you configurations.



3º Step
Ok now we can start with asp.net code
:)

Let start with Page life Cycle
On_load

BtDel.Attributes.Add("onclick", "javascript:if(confirm('Delete Photo?')== false) return false;");
MemoryStream stream = new MemoryStream();
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;data source=" + Server.MapPath("Foto.mdb"));
try
{
connection.Open();
OleDbCommand command = new OleDbCommand("select Foto from Foto where ID="+ Request.Params["id"].ToString(), connection);
byte[] image = (byte[])command.ExecuteScalar();
if (image != null)
{
Upload.Visible = false;
BtAdd.Visible = false;
BtDel.Visible = true;
stream.Write(image, 0, image.Length);
DynamicImage1.ImageBytes = stream.ToArray();
}
else
{
DynamicImage1.ImageFile = "http://[server]/_imgs/grid_Temp.gif";
}

}
catch( NullReferenceException ex )
{
DynamicImage1.ImageFile = "http://[Server]/_imgs/grid_Temp.gif";
connection.Close();
stream.Close();
Upload.Visible = false;
BtAdd.Visible = false;
BtDel.Visible = false;
Console.WriteLine( "Caught error: {0}.", e);
}

finally
{
connection.Close();
stream.Close();
}


Now Event Handler to add Photo

int len = Upload.PostedFile.ContentLength;
byte[] pic = new byte[len];
Upload.PostedFile.InputStream.Read(pic, 0, len);
// Insert the image and comment into the database
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;data source=" + Server.MapPath("Foto.mdb"));
try
{
connection.Open();
OleDbCommand cmd = new OleDbCommand("insert into Foto "
+ "(ID, foto) values (" + Request.Params["id"].ToString() +",@pic)", connection);
cmd.Parameters.Add("@Pic", System.Data.OleDb.OleDbType.Binary,pic.Length).Value = pic;
cmd.ExecuteNonQuery();
Page.Response.Redirect(Page.Request.Url.ToString(), true);

}
finally
{
connection.Close();
}

Now Event Handler to Delete Photo

OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;data source=" + Server.MapPath("Foto.mdb"));
try
{
connection.Open();
OleDbCommand cmd = new OleDbCommand("Delete from foto where ID=" + Request.Params["id"].ToString(), connection);
cmd.ExecuteNonQuery();
Page.Response.Redirect(Page.Request.Url.ToString(), true);
}
finally
{
connection.Close();
}


Ok The essencial is here,
Hope you like this article, works for me.
:)
Post a Comment