<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Kids, don't try this at home! : LINQ</title><link>http://weblogs.asp.net/gayanperera/archive/tags/LINQ/default.aspx</link><description>Tags: LINQ</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Dynamics CRM Workflow - Automatically Email any Report generated in CRM as a PDF attachment</title><link>http://weblogs.asp.net/gayanperera/archive/2009/07/02/dynamics-crm-workflow-send-pdf-invoice-automatically.aspx</link><pubDate>Thu, 02 Jul 2009 03:43:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7138379</guid><dc:creator>gperera</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gayanperera/rsscomments.aspx?PostID=7138379</wfw:commentRss><comments>http://weblogs.asp.net/gayanperera/archive/2009/07/02/dynamics-crm-workflow-send-pdf-invoice-automatically.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;Time for another Dynamics CRM goodie...&lt;BR&gt;&lt;BR&gt;&lt;IMG style="WIDTH: 651px; HEIGHT: 230px" title="Send an invoice to a customer with a pdf attached using a dynamics crm workflow" alt="Send an invoice to a customer with a pdf attached using a dynamics crm workflow" align=middle src="http://weblogs.asp.net/blogs/gayanperera/auto-pdf-invoice-and-send-email.gif" width=651 height=230 mce_src="http://weblogs.asp.net/blogs/gayanperera/auto-pdf-invoice-and-send-email.gif"&gt;&lt;BR&gt;&lt;BR&gt;We try to automate as many things as we can, automatically sending invoices&amp;nbsp;is one of them. Workflow as you know in Dynamics CRM is very powerful, specially the ability to create custom activities and hook into the pipeline. We took advantage of this, we created a custom workflow activity that takes in any entity, Email and a Report, automatically turn it into a PDF, attach it to the email and send the email.&lt;BR&gt;&lt;BR&gt;Take a look at this &lt;A href="http://www.magnetism.co.nz/_backup/clients/media/crmreportintopdf.html" target=_blank mce_href="http://www.magnetism.co.nz/_backup/clients/media/crmreportintopdf.html"&gt;5 minute video&lt;/A&gt; to see how it works. If you're interested in using this in your organization contact me via&amp;nbsp;&lt;A href="http://www.magnetism.co.nz/contact_us.aspx" target=_blank mce_href="http://www.magnetism.co.nz/contact_us.aspx"&gt;this link&lt;/A&gt;. &lt;BR&gt;&lt;BR&gt;
&lt;H1&gt;Under The Hood&lt;/H1&gt;&lt;BR&gt;&lt;STRONG&gt;Report2Pdf&lt;/STRONG&gt;&lt;BR&gt;This class does the heavy lifting, it has a method called Download, connects to the report server, configures the parameters and renders the report as a PDF then returns a byte array.&lt;BR&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Report2Pdf&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;byte&lt;/SPAN&gt;[] Download(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; rsUrl, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; rseUrl, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.Net.&lt;SPAN style="COLOR: #2b91af"&gt;NetworkCredential&lt;/SPAN&gt; credentials, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; report, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Report2PdfParameter&lt;/SPAN&gt;[] inputParameters, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; culture)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;Workflow Activity&lt;/STRONG&gt;&lt;BR&gt;Straight forward, read the configuration data, makes a call to the Report2Pdf.Download method, creates an activitymimeattachment then executes a SendEmailRequest.&lt;BR&gt;&lt;BR&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;byte&lt;/SPAN&gt;[] data = &lt;SPAN style="COLOR: #2b91af"&gt;Report2Pdf&lt;/SPAN&gt;.Download(config.Url, config.ExecutionUrl,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NetworkCredential&lt;/SPAN&gt;(config.UserName, config.Password, config.Domain),&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; report.Location, rps.ToArray(), config.Culture);&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;LINQ and Dynamics CRM&lt;/STRONG&gt;&lt;BR&gt;Thanks to Amanda and the team at &lt;A title="XrmLinq - Linq to Dynamics CRM" href="http://www.xrmlinq.com/" target=_blank mce_href="http://www.xrmlinq.com"&gt;XrmLinq&lt;/A&gt; for giving us access to their library. This has made data access so much easier. Something that would take atleast&amp;nbsp;10-20 lines of code and a lot of&amp;nbsp;effort messing around with FetchXml&amp;nbsp;has now been reduced to 3 lines and LINQ!&lt;BR&gt;
&lt;DIV style="FONT-FAMILY: Courier New; BACKGROUND: white; COLOR: black; FONT-SIZE: 10pt"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;BR&gt;var&lt;/SPAN&gt; config = (&lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; c &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; xrm.ReportServerConfigurations&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; c.ReportServerConfigurationId == report.ConfigurationId&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; c).SingleOrDefault();&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;BR&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7138379" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gayanperera/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/gayanperera/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/gayanperera/archive/tags/Dynamics+CRM/default.aspx">Dynamics CRM</category></item><item><title>CRM 4 FilteredViews, LINQ &amp; WCF</title><link>http://weblogs.asp.net/gayanperera/archive/2008/07/20/crm-4-filteredviews-linq-amp-wcf.aspx</link><pubDate>Sun, 20 Jul 2008 08:20:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6423243</guid><dc:creator>gperera</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gayanperera/rsscomments.aspx?PostID=6423243</wfw:commentRss><comments>http://weblogs.asp.net/gayanperera/archive/2008/07/20/crm-4-filteredviews-linq-amp-wcf.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;We decided to use FilteredViews instead of FetchXml for an internal project, but we ran into couple of problems. You can't access data from&amp;nbsp;the FilteredViews using ASP.NET since it runs under NETWORK SERVICE (by default (app pool)), FilteredViews filter the data by Users.&lt;br&gt;&lt;br&gt;If you set the &amp;lt;identity&amp;gt; settings on the WCF service you'll notice it has no effect, you need to tell WCF to run in&amp;nbsp;asp compatibility mode, check &lt;a href="http://msdn.microsoft.com/en-us/library/bb332338.aspx" class="" title="Hosting and Consuming WCF Services" mce_href="http://msdn.microsoft.com/en-us/library/bb332338.aspx"&gt;this link&lt;/a&gt; for more details.&lt;br&gt;&lt;br&gt;&lt;b&gt;Impersonation needed for FilteredViews&lt;/b&gt;&lt;br&gt;- EXECUTE AS doesn't work, some forums suggested we enable DB_CHAINING, TRUSTWORTHY &amp;amp;&amp;nbsp;grant NETWORK SERVICE Impersonate for each User, we decided to take a different route since these database changes are unsupported by Microsoft.&lt;br&gt;&lt;br&gt;- Configuring the Application Pool to run as a different user gave a "Service Unavailable" error, changing the User that runs when an anonymous request comes into IIS didn't seem to work either.&lt;br&gt;&lt;br&gt;&lt;b&gt;Solution&lt;/b&gt;&lt;br&gt;&lt;b&gt;web.config&lt;/b&gt;&lt;br&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;authentication&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;mode&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;Windows&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; /&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;identity&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;impersonate&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;true&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;userName&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;...&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;password&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;...&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;/&amp;gt;&lt;br&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;system.serviceModel&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;serviceHostingEnvironment&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;aspNetCompatibilityEnabled&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;true&lt;/font&gt;&lt;font size="2"&gt;"&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;serviceHostingEnvironment&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;br&gt;&amp;lt;/&lt;font color="#a31515" size="2"&gt;system.serviceModel&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;font color="#000000"&gt;&lt;br&gt;&lt;b&gt;*.svc.cs &lt;/b&gt;&lt;br&gt;Set this attribute on your service class&lt;br&gt;&lt;font size="2"&gt;[&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;AspNetCompatibilityRequirements&lt;/font&gt;&lt;font size="2"&gt;(RequirementsMode=&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;AspNetCompatibilityRequirementsMode&lt;/font&gt;&lt;font size="2"&gt;.Allowed)]&lt;br&gt;&lt;br&gt;That'll allow you to use FilteredViews in your asp.net application, as you can see there are downsides to this solution. Impersonating username/password is inside the web.config file (unencrypted), only allows us to filter by that user.&lt;br&gt;&lt;br&gt;Check out &lt;a href="http://www.codeplex.com/LinqtoCRM" class="" title="LINQ to CRM" mce_href="http://www.codeplex.com/LinqtoCRM"&gt;LinqtoCRM on codeplex&lt;/a&gt;, this tool will be great once all the LINQ funtionality is implemented.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#000000"&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6423243" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gayanperera/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gayanperera/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/gayanperera/archive/tags/WCF/default.aspx">WCF</category><category domain="http://weblogs.asp.net/gayanperera/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/gayanperera/archive/tags/Dynamics+CRM/default.aspx">Dynamics CRM</category><category domain="http://weblogs.asp.net/gayanperera/archive/tags/SQL/default.aspx">SQL</category></item></channel></rss>