Showing posts with label fetchxml. Show all posts
Showing posts with label fetchxml. Show all posts


Retrieve Marketinglists attached to Campaign Activity

Today I've been doing some coding again and I've found an interesting situation. For my code I do need to find which marketing lists belong to a specific Campaign Activity. Apparently more people on the internet faced the same issue, but I haven't found anybody who supplied the answer on how to do that.

I've managed to find an approach which works for both the fetchXml as well as a QueryExpression. Here is the code for both of these options:

FetchXml:


StringBuilder sbFetchXml = new StringBuilder();
sbFetchXml.Append("<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"true\">");
sbFetchXml.Append("<entity name=\"list\">");
sbFetchXml.Append("<attribute name=\"listname\"/><attribute name=\"listid\"/>");
sbFetchXml.Append("<order attribute=\"listname\" descending=\"true\"/>");
sbFetchXml.Append("<link-entity name=\"campaignactivityitem\" from=\"itemid\" to=\"listid\" visible=\"false\" intersect=\"true\">");
sbFetchXml.Append("<link-entity name=\"campaignactivity\" from=\"activityid\" to=\"campaignactivityid\" alias=\"aa\">");
sbFetchXml.Append("<filter type=\"and\">");
sbFetchXml.AppendFormat("<condition attribute=\"activityid\" operator=\"eq\" uitype=\"campaignactivity\" value=\"{0}\"/>", campaignActivityId);
sbFetchXml.Append("</filter>");
sbFetchXml.Append("</link-entity>");
sbFetchXml.Append("</link-entity>");
sbFetchXml.Append("</entity>");
sbFetchXml.Append("</fetch>");

string strXmlResult = service.Fetch(sbFetchXml.ToString());


QueryExpression:

ConditionExpression condActivityId = new ConditionExpression();
condActivityId.AttributeName = "activityid";
condActivityId.Operator = ConditionOperator.Equal;
condActivityId.Values = new object[] { campaignActivityId };

FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] { condActivityId };

LinkEntity leCampaignActivity= new LinkEntity();
leCampaignActivity.LinkFromEntityName = EntityName.campaignactivityitem.ToString();
leCampaignActivity.LinkFromAttributeName = "campaignactivityid";
leCampaignActivity.LinkToEntityName = EntityName.campaignactivity.ToString();
leCampaignActivity.LinkToAttributeName = "activityid";
leCampaignActivity.LinkCriteria = filter;

LinkEntity leCampaignActivityItem = new LinkEntity();
leCampaignActivityItem.LinkFromEntityName = EntityName.list.ToString();
leCampaignActivityItem.LinkFromAttributeName = "listid";
leCampaignActivityItem.LinkToEntityName = EntityName.campaignactivityitem.ToString();
leCampaignActivityItem.LinkToAttributeName = "itemid";
leCampaignActivityItem.LinkEntities = new LinkEntity[] {leCampaignActivity};

QueryExpression query = new QueryExpression();
query.EntityName = EntityName.list.ToString();
query.ColumnSet = new AllColumns();
query.LinkEntities = new LinkEntity[] { leCampaignActivityItem };

BusinessEntityCollection bec = service.RetrieveMultiple(query);

For me this worked, I hope this helps you as well!



Using Advanced Find for FetchXML builder v4.0

Some time ago I have written an article about how to use the Advanced Find as FetchXML builder. Today I tried using the javascript:prompt() method on a CRM 4.0 deployment and found out that the code doesn't work anymore. This alert script does still work though.


javascript:alert(resultRender.FetchXml.value);

A slightly different code which might be easier to remember is:

javascript:alert(document.all.FetchXml.value);

With the popup selected you can press copy+c and paste the result in a notepad window.



FetchXML into a DataSet

Once upon a time in the Netherlands there was this developer who was working on MSCRM for quite some time. He started to notice that he had to search again and again for the same questions. Luckily he heard about the term 'blogging' and so he started to post the most frequent questions on a blog. The goal was to be able to quickly retrieve the information that he had found earlier. The fact that other people also read his blog and save themselves time to figure everything out themselves is just an added value to the blog.

Now with this knowledge you will understand why I post this piece of code:


private DataSet FetchDataSet(string fetchXml) {
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
//service.UnsafeAuthenticatedConnectionSharing = true; //enable in secure migration programs

string strResult = service.Fetch(fetchXml);
DataSet ds = new DataSet();
System.IO.StringReader reader = new System.IO.StringReader(strResult);
ds.ReadXml(reader);
return ds;
}

I'm just typing this way to often and I prefer to use copy and paste.

Furthermore a link to a previous post on how to quickly create a fetchXML:
http://ronaldlemmen.blogspot.com/2006/11/using-advanced-find-for-fetchxml.html

And a link to another post on how to make sure that you do fetch all records and not only the first 5000:
http://ronaldlemmen.blogspot.com/2006/08/fetch-all-records.html



Using the Advanced Find for FetchXML builder

Some time ago I talked about the program "FetchXML builder" to build FetchXML queries: http://ronaldlemmen.blogspot.com/2006/09/fetchxml-builder.html, but take a look at the next solution.

You can just open the Advanced find page and build the query as you like. Then run the query to see if it returns the data as you wish it should. If you are satisfied with the results and you want to know what query was sent into the CRM Framework, then press F11 to get the address bar and enter this script and press enter.


javascript:alert(resultRender.FetchXml.value);

If you get a warning about leaving the page, just press 'ok' and then the query which is sent to the framework opens up in a popup. Unfortunately you cannot select the text to copy and paste. But with Windows XP and Windows Server 2003 you can copy all text on the popup by clicking somewhere on the popup (not on the button "OK" ofcourse) and pressing ctrl+c. Now in notepad you can paste the text of the FetchXML.

Good luck!

Update: Thanks to Piotr in the comment section I have learned the javascript prompt command. Try this instead of the alert:

javascript:prompt("my query:", resultRender.FetchXml.value);



FetchXML Builder

Some time ago Microsoft organized the code contest. Users have been asked to share their greatest assets with the community. One of the winners is James Downey. He has created the FetchXML Builder: a free user-friendly tool for building and testing FetchXML statements.

Today I have been able to use this tool in my development project. It works very intuitive, at least for people having fetchxml knowledge. I would recommend everybody to download the tool and use it to reduce the time needed for figuring out how to set up the xml. Go to the CRM Sandbox download page and scroll to "FetchXML Builder".

James, thanks for sharing!



Fetch all records

Have you ever tried to write a code which will get you all records from a specific entity? It's harder then you think it is! Everybody who is a bit aware of the CRM SDK thinks it should be a fetch statement like this:

<fetch mapping='logical'><entity name='account'><attribute name='accountid'/></entity>

WRONG!
This would only give you the first 5000 records in the database! It is written down in the SDK with small letters, but it could drive you crazy..

There are two solutions for this issue.
1) Add a registery setting to specify not to implement MaxRowsPerPage
2) Modify the fetch statement and merge several results

Here are the details for each solution
1st solution
Search in the SDK for the word "TurnOffFetchThrottling". You should add this as DWORD registery setting to HKLM\Software\Microsoft\MSCRM. Set the value to 1. You will now not have the 5000 records limit.

2nd solution
Modify your fetch statement to include paging and count numbers. Store all the data in an DataSet and perform that series of code over and over again as long as there is data coming.

Here's the script you should use to get all accountid's (for clarity and the ease of use I have added a function called "FetchDataSet").


private DataSet FetchAllAccountIds(){
int i=1;
bool bFinished = false;
DataSet dsAllData = new DataSet();
while (bFinished == false)
{
StringBuilder sbFetch = new StringBuilder();
sbFetch.AppendFormat("<fetch mapping='logical' page='{0}' count='5000'>", i);
sbFetch.Append("<entity name='account'>");
sbFetch.Append("<attribute name='accountid'/>");
sbFetch.Append("<attribute name='new_12_accountid'/>");
sbFetch.Append("</entity>");
sbFetch.Append("</fetch>");
DataSet dsTempResult = FetchDataSet(sbFetch.ToString());
dsAllData.Merge(dsTempResult);
if (dsTempResult.Tables[0].Rows[0]["morerecords"].ToString() == "0")
{
bFinished = true;
}
else
{
i++;
}
}
return dsAllData;
}

private DataSet FetchDataSet(string fetchXml)
{
string strResult = service.Fetch(fetchXml);
DataSet ds = new DataSet();
System.IO.StringReader reader = new System.IO.StringReader(strResult);
ds.ReadXml(reader);
return ds;
}


I hope this saves you some time!

Thanks to Andrew Krivosheyenko for the Regedit solution!