Sunday, October 3, 2010

Compilation Error or Web.config File is Missing - ReflectedSchemas

Recently while writing a program for a friend using Visual Studio 10 Express, all my web pages had the following error on each control:

Element '[control type]' is not a known element. This can occur if there is a compilation error in the web site, or the web.config file is missing

I found that if you delete the files from the following folder, it should solve your problem:
C:\Users\JoshuaYockey\AppData\Roaming\Microsoft\Visual Studio\10.0\ReflectedSchemas
or if you are using Express:
C:\Users\JoshuaYockey\AppData\Roaming\Microsoft\VWDExpress\10.0\ReflectedSchemas

Hope this helps others.

Wednesday, September 22, 2010

Remove Associated Buttons (Add Existing xxxxx to this record)

I recently had a request to remove the "Add Existing [entity name] to this record" button from the associated view for a client.

The below link is a great post on how to do this:

MSCRM 4 - Remove 'Add Existing xxxxx to this record' button by Dave Hawes

The only modification I made to this code was to use
liElements[i].outerHTML='<SPAN></SPAN>'; 

instead of
liElements[i].style.display = 'none';


My next post will discuss how to change the name of these associated buttons.

Friday, September 10, 2010

CRM 4.0 How to capture a Many to Many Event

After spending many hours trying to capture the on create of a many to many relationship, I found only 2 ways to do so:

1. Add a bit field that is set to true on load. This then requires the user to save the form. Create a workflow for when the bit field has changed. Then add to the workflow a condition when bit field is true followed by your process. End with the bit field being set to false.

  • Advantage: Requires minimal code and is supported by Microsoft.

  • Disdvantage: Upon every save (update) a workflow will fire. Not very optimal.



2. Create a trigger. The linker table exists inside the MSCRM database. On that table create a trigger that will perform your task when a new item is added.

  • Advantage: Only fires when new items are added.

  • Disdvantage: Not supported by Microsoft and the trigger will be removed upon Upgrade.

CRM 4.0 Hiding Navigation

Here is a great post on how to hide navigation.

Hiding Unwanted Navigation In CRM by Jeremy Winchell

Thursday, September 2, 2010

CRM Phone Number Formatting onChange with Extensions

Format Phone Numbers in CRM

Format (xxx) xxx-xxxx

var oField = event.srcElement;
if (typeof(oField) != "undefined" && oField != null && oField.DataValue != null)
{
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
if(sTmp.length == 10)
{
oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
}
else
{
alert('Phone must contain 10 numbers.');
}
}


Format xxx-xxx-xxxx

var oField = event.srcElement;
if (typeof(oField) != "undefined" && oField != null && oField.DataValue != null)
{
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
if(sTmp.length == 10)
{
oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
}
else
{
alert('Phone must contain 10 numbers.');
}
}


Format xxx.xxx.xxxx

var oField = event.srcElement;
if (typeof(oField) != "undefined" && oField != null && oField.DataValue != null)
{
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
if (sTmp.length == 10)
{
oField.DataValue = sTmp.substr(0, 3) + "." + sTmp.substr(3, 3) + "." + sTmp.substr(6, 4);
}
else
{
alert('Phone must contain 10 numbers.');
}
}


Other Formatting Issues:
Remove leading digit if number is 1

var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
if (sTmp.substr(0, 1) == "1")
{
sTmp = sTmp.substr(1, sTmp.length-1);
}


Allowing only 7 digits (no area code)

else if (sTmp.length == 7)
{
oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
}
else
{
alert('Phone must contain 7 or 10 numbers.');
}


Allowing extensions (requires area code)

else if (sTmp.length > 10)
{
oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4) + " ext: " + sTmp.substr(10, sTmp.length-10);
}
else
{
alert('Invalid phone number. If using an extension, you must enter area code also.');
}

Monday, May 17, 2010

Using C# to convert Tif to Pdf

Below is code that allows you to create a PDF file from one or more Tif files (even those with multiple pages).

This code references iTextSharp:
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Drawing.Imaging;


Below the code is setting up the document/pdf and adding a tif file to the pdf:

string sMergedFiles = "C:\\PDFTest\\PdfReport.PDF";

string sTiffFiles = "C:\\PDFTest\\TiffFiles\\";

Document document = new Document(PageSize.A4, 50, 50, 50, 50);

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sMergedFiles, FileMode.CreateNew));

document.Open();

//here you can loop thru and call AddTiff2Pdf for multiple files

string tiffFileName = Path.Combine(sTiffFiles, "asdf.tif");

AddTiff2Pdf(tiffFileName, ref writer, ref document);

string tiffFileName = Path.Combine(sTiffFiles, "1234.tif");

AddTiff2Pdf(tiffFileName, ref writer, ref document);

document.Close();


Below is the function used to add the tiff to the pdf:

private void AddTiff2Pdf(string tiffFileName, ref PdfWriter writer, ref Document document)
{

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(tiffFileName);

int numberOfPages = bitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

PdfContentByte cb = writer.DirectContent;

for (int page = 0; page < numberOfPages; page++)
{

bitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, page);

System.IO.MemoryStream stream = new System.IO.MemoryStream();

bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(stream.ToArray());

stream.Close();

img.ScalePercent(72f / bitmap.HorizontalResolution * 100);

img.SetAbsolutePosition(0, 0);

cb.AddImage(img);

document.NewPage();
}
}

Tuesday, May 11, 2010

CRM 4.0: Hiding Save Buttons

Recently had a client that wanted users to only "Save as Completed" activities. That had some users that thought Save & Close would actually "close" the activity. To alleviate the potential error, we decided to remove all Save buttons (Save, Save & Close, and Save and New) from the menu bar and file menu. Below is the code to do this.


var CRMFORMTYPE_CREATE = 1;
var CRMFORMTYPE_UPDATE = 2;
var CRMFORMTYPE_READONLY = 3;
var CRMFORMTYPE_DISABLED = 4;
var CRMFORMTYPE_QUICKCREATE = 5;
var CRMFORMTYPE_BULKEDIT = 6;

if(crmForm.FormType == CRMFORMTYPE_UPDATE)
{
var li = document.getElementsByTagName('LI');
var i = 0;
while(i < li.length)
{
if (li[i].getAttribute('id') == '_MIcrmFormSave' ||
li[i].getAttribute('id') == '_MIcrmFormSubmitCrmForm59truetruefalse' ||
li[i].getAttribute('id') == '_MBcrmFormSave' ||
li[i].getAttribute('id') == '_MBcrmFormSubmitCrmForm59truetruefalse')
{
li[i].outerHTML='';
}

if (li[i].getAttribute('id') == '_MIcrmFormSaveAndClose')
{
document.getElementById('_MIcrmFormSaveAndClose').style.display = "none";
}
if (li[i].getAttribute('id') == '_MBcrmFormSaveAndClose')
{
document.getElementById('_MBcrmFormSaveAndClose').style.display = "none";
}

//alert(li[i].getAttribute('title') + ' | ' + li[i].getAttribute('id'));
i = i + 1;
}
}