Monday, October 22, 2007

How to Retrieve system user list efficiently

Do you need a list of all users in CRM 3.0?

if so do the following:

Here is the Fetch XML that I used to pull all the system users

<fetch mapping='logical'><entity name='systemuser'><all-attributes/></entity></fetch>

To retrieve the list simply use this C# code:

string result = service.Fetch(fetchxml);


XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(result);

To loop through the list and fill a drop down list for example:

XmlNodeList userList = xdoc.SelectNodes("resultset/result");


foreach(XmlNode n in userList)
{
ListItem itm = new ListItem();
itm.Text = n["fullname"].InnerText;
itm.Value = n["systemuserid"].InnerText;
ddlUserList.Items.Add(itm);
}

It is that simple.

Oshri Cohen

Labels: , , , , , ,

Friday, October 19, 2007

How to debug a Callout efficiently.

Debugging a Callout must be the single most tedious task that i have ever had to do while developing for CRM 3.0.

The need to continuously restart IIS and the associated services is very time consuming.

I used a combination of a webservice i wrote and the callout to ease the development and debugging.
Essentially, on every event the callout executes a webservice method that accepts the same parameters.

1) Create a webservice, ideally it shoudl be hosted as a virtual directory within the crm 3.0 website.

2) Override a method, in the callout file, for the purpose of this tutorial I will use the PostCreate method.
3) The PostCreate method accepts the following parameters:
Microsoft.Crm.Callout.CalloutUserContext userContext, Microsoft.Crm.Callout.CalloutEntityContext entityContext, string postImageEntityXml
It is a real pain to transfer complex classes such as the userContext via webservice, so i decided to serialize each class using the xmlSerializer and transfer every parameter to my webserice as a string.

Before we continue we will need the webservice.

The webservice i as simple as it gets, you will need a WebMethod named PostCreate that accepts the same number of paremeters, just on this end every parameter is a string.

4) Deserialize the string objects back into their original type (I will show you the code later.)

5) Compile the projects and add the webservice to the callout.

6) In each Callout method you will need to call the webserivce before the base method is called.

I will post a generic callout class shortly so that all you need to do is add it to your project and start working.

*** If you are worried about performance, don't be after the first callt he webservice get's cached and every subsequent call has little performance penalty.

Here is the link to the Complete project:

Simboliq Callout project with webservice

Oshri Cohen

Labels: , , , , , ,

building fast and efficient crm 3.0 queries server side

CRM 3.0 comes with built-in support for building custom queries to access data with the power and flexibility of SQL.

In CRM this is done using a Fetch xml statement.

While looking around the net for any half decent tutorial i found this piece of very well written software, i know find it indispensable, FetchXML Builder.
I won't explain how to use the software given that there already is some documentation on the site and it is pretty self explanatory.

Once you have created your query, use the following code to "fetch" the result:

string resultsSet = service.Fetch(fetch);

it will return among other things a "result" element within the main "resultset" element.

To iterate throguh the result use this code:

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(ResultsSet);

foreach(System.Xml.XmlNode node in xdoc.SelectNodes("resultset/result"))
{

}

Oshri Cohen

Labels: , ,

How to Identify the currently logged in user using the MSCRM service

I needed to build a custom CRM page that would be hosted within the crm 3.0 environment, it need to be accessed via the navigation pane. Given that requirement the page need to find out which user was logged in.

using the the webservice do the following:

WhoAmIRequest whoAmI = new WhoAmIRequest();
WhoAmIResponse Iam = (WhoAmIResponse)service.Execute(whoAmI);
Guid userid = Iam.UserId;


The WhoAmIResponse includes also includes the business unit belonging to the user.

Oshri Cohen

Labels: , , , , , , , ,

Tuesday, October 16, 2007

How to hide CRM 3.0 menu items

A requirement for one of my clients required me to hide the" save and new" from an entity.

This is very simple to do, essentially the CRM developers named these buttons with a static name that can be accessed within any javascript event.

here is the reference to the most common buttons:


Save = "_MBcrmFormSave"
Save and Close = "_MBcrmFormSaveAndClose"
Save and New = "_MBcrmFormSubmitCrmForm59truetruefalse"
Print = "_MBcrmFormPrint"
Save as completed = "_MBSaveAsCompleted"


to hide the button simply call the following function:

document.getElementById('[name of the button]').Style.Display='None' or 'Inline';

Setting the Style.Display to 'None' will hide the button.

Setting the Style.Display to 'Inline'will Show the button.

*** Please make sure that the menu item actually exists, use this statement for the test:

var mnuItem = document.getElementById('[name of the button]');
if( mnuItem != null)

{
//hide or show here
}

***

I would appreciate if anyone knows the ID name for other buttons to post it as a comment, if you do so i will append it to the post and of course credit you for it.


Have a great Day

Oshri Cohen

Labels: , , , , ,

Monday, October 15, 2007

How to Include JS files in CRM 3.0

I encountered a problem while customizing a CRM 3.0 implementation for a client.
The problem was that the project required a lot of JavaScript and if you have been working with CRM 3.0 you already knows how frustrating it is to develop using JS within crm.

In order to remedy that problem, I thought of using a custom .JS file and include it in the forms that I was customizing. Microsoft clearly states modifying the CRM source pages is not supported or recommended and may not survive an upgrade or a patch.

Here is my solution that may help us all in future CRM 3.0 client side development.

1.
Create your .JS file.

2.
Place it either in a new Virtual Directory within the CRM website or as a separate website all together.

3.
Select the entity that you want to customize and enable the OnLoad event.

4.
Put the following code within the event:

var script = document.createElement("script");
script.type = "text/javascript";
script.src= "[the location of your script either relative or absolute]";
socument.getElementsByTagName("head")[0].appendChild(script);



5.
At this point the .JS file has been appended to the page and has been subsequently loaded by the browser.

***Please note that any modifications to the JS file will require you to clear the browsers Temporary files, otherwise it will not load the latest version. ***

*** this has not been tested for CRM within outlook. ***

Have a great day.

Oshri Cohen

Labels: , , , , ,