Wednesday, 17 December 2008

Complex Queries in Crm 4

Hi All,

I am not normally a big fan of FetchXml. It is messy and since it is often passed in as a string, it can be prone to syntax errors. But over the years, people have been using Fiddler to extract the SOAP headers to the Crm server in order to catch the FetchXml query. There is nothing new here and it is quite handy if you accept the fact that data will also be returned in xml format, meaning it will have to be iterated to get the needed information. Which can really be a pain. I have spent time writing methods to parse the result xml into a dictionary that I can easily access. But even this can be painful at times.

So the other day I discovered a class called FetchXmlToQueryExpressionRequest. This class can be used to convert any fetchXml to a QueryBase object, that can be fed into a RetrieveMultipleRequest. The beauty of this lies in the fact that it is the best of both worlds. You can use advanced find to build complex queries without having to confuse yourself with linked entities etc AND you can then retrieve the data as a BusinessEntityCollection.

Here's some sample code:

string fetch = @"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='contact'>
<attribute name='mobilephone'/>
<attribute name='lastname'/>
<attribute name='firstname'/>
<attribute name='emailaddress1'/>
<attribute name='contactid'/>
<order attribute='lastname' descending='false'/>
</entity>
</fetch>";
";


FetchXmlToQueryExpressionRequest fetchReq = new FetchXmlToQueryExpressionRequest();

FetchReq.FetchXml = fetch;

FetchXmlToQueryExpressionResponse res = (FetchXmlToQueryExpressionResponse)service.Execute(fetchReq);

RetrieveMultipleRequest req = new RetrieveMultipleRequest();

req.Query = res.Query;

req.ReturnDynamicEntities = true;

RetrieveMultipleResponse recordSet = (RetrieveMultipleResponse)service.Execute(req);

Have a nice day
Paul Reyneke

Thursday, 4 December 2008

The specified organization is disabled

I ran into a little problem on my development server today. I was playing round with a windows service that I wrote to run timed events. At one point the Windows service ran into an infinite loop and "End Task" wouldn't kill it, so the only way I could break out of it was to restart ISS. All of a sudden the windows service would no longer start. After a while I tried to log into the crm incarnation of the organization I'm working on and I kept on getting a "The specified organization is disabled" error. This made me panick a bit because I thought I'd corrupted the DB. After more time trying to figure it out, I also tried each of the other organizations (there are 10+ of these) on the crm server and they all had the same problem, "The specified organization is disabled".

Googling the problem kept on coming back to an issue with disabling the default organization form within Deployment Manager. But since I had not done this, I just ignored it's advice and kept on trying to find BD corruption. But after a while I had a look at Deployment manager, and sure enough, the default organization was disabled. This puzzled me a bit, until I remembered that I did infact disable it weeks ago. So I had a look at this link:

http://support.microsoft.com/kb/946618/enus$

It was clear that, by pure bad luck, I had not restarted IIS since the default organization got disabled, which was weeks ago. So, re-enabled the default organization, restarted IIS and everything was working again. But 4 hours was wasted on this problem.

Moral of the story, the most obvious cause/solution is often the most likely.