Contents tagged with LINQ
-
Dynamics CRM Workflow - Automatically Email any Report generated in CRM as a PDF attachment
Time for another Dynamics CRM goodie...
We try to automate as many things as we can, automatically sending invoices 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.
Take a look at this 5 minute video to see how it works. If you're interested in using this in your organization contact me via this link.
Under The Hood
Report2Pdf
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.
public class Report2Pdf{
public static byte[] Download(string rsUrl, string rseUrl,
System.Net.NetworkCredential credentials, string report,
Report2PdfParameter[] inputParameters, string culture)
{
Workflow Activity
Straight forward, read the configuration data, makes a call to the Report2Pdf.Download method, creates an activitymimeattachment then executes a SendEmailRequest.
byte[] data = Report2Pdf.Download(config.Url, config.ExecutionUrl,
new NetworkCredential(config.UserName, config.Password, config.Domain),
report.Location, rps.ToArray(), config.Culture);
LINQ and Dynamics CRM
Thanks to Amanda and the team at XrmLinq for giving us access to their library. This has made data access so much easier. Something that would take atleast 10-20 lines of code and a lot of effort messing around with FetchXml has now been reduced to 3 lines and LINQ!
var config = (from c in xrm.ReportServerConfigurationswhere c.ReportServerConfigurationId == report.ConfigurationId
select c).SingleOrDefault();
-
CRM 4 FilteredViews, LINQ & WCF
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 the FilteredViews using ASP.NET since it runs under NETWORK SERVICE (by default (app pool)), FilteredViews filter the data by Users.
If you set the <identity> settings on the WCF service you'll notice it has no effect, you need to tell WCF to run in asp compatibility mode, check this link for more details.
Impersonation needed for FilteredViews
- EXECUTE AS doesn't work, some forums suggested we enable DB_CHAINING, TRUSTWORTHY & grant NETWORK SERVICE Impersonate for each User, we decided to take a different route since these database changes are unsupported by Microsoft.
- 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.
Solution
web.config
<authentication mode="Windows" />
<identity impersonate="true" userName="..." password="..."/>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
</system.serviceModel>
*.svc.cs
Set this attribute on your service class
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
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.
Check out LinqtoCRM on codeplex, this tool will be great once all the LINQ funtionality is implemented.