MS Dynamics CRM activity preview, queue view customizing by http module

27 June, 2009 at 6:39 am | In Dynamics CRM | Leave a Comment

This post is about changing the activity preview in Microsoft Dynamic CRM to custom preview.
CRM_Standard_activity_preview_stefan_scheller 

ASP.NET uses a feature for page processing, called HTTP modules. HTTP modules
participate in the processing of a request by handling application events.
A given request can flow through multiple HTTP modules.
So we are creating our own module and hook on the PreRequestHandlerExecute of the PageLiveCycle.

 asp_net_application_events_stefan_scheller

Creating HTTP Modules is an easy task . You simply need to
add a class that implements the System.Web.IHttpModule interface. You can then register your
module by adding it to the <httpModules> section of the web.config file of your CRM root.

Your web.config should have something like the following structure:

<system.web>

    <httpModules>
        <add name=”ExtensionModulActivity” type=”WebPages.HttpExtensionModul.ExtensionClass,WebPages.HttpExtensionModul” />
    </httpModules>
…   

Example Implementation for an http module – which is registering for the Event and filtering for the page we would like to change:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebPages.HttpExtensionModul
{
    public class ExtensionClass : IHttpModule
    {
 
        public void Dispose()
        {
            ;
        }
 
        public void Init(HttpApplication app)
        {
            //register events
 
            app.PreRequestHandlerExecute += new EventHandler(this.OnPreRequestHandlerExecute);
        }
 
        public void OnPreRequestHandlerExecute(object o, EventArgs args)
        {
             HttpContext context = ((HttpApplication)o).Context;
             IHttpHandler handler = context.Handler;
 
             //Hookup into PreRender event of the Page
 
                 if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith("preview.aspx"))
                 {
                     int type = (context.Request.QueryString["type"] != null) ? int.Parse(context.Request.QueryString["type"]) : -1;
                     Guid id = (context.Request.QueryString["id"] != null) ? new Guid(context.Request.QueryString["id"]) : Guid.Empty;
 
                     if (id != Guid.Empty && type == 4002 ) //4002 = email
                     {
                         context.Response.ContentType = "text/xml";
                         context.Response.Filter = new ActivityPreview(context.Response.Filter, type, id);
                     }
                 } 
        }
    }
}

Example Implementation of a stream class, to add the custom text

namespace WebPages.HttpExtensionModul
{
    public class ActivityPreview : MemoryStream
    {
        private Stream _stream;
        private long _position;
 
        private int _entityId;
        private Guid _objectId;
        
 
    public ActivityPreview(Stream stream, int entityId, Guid objectId)
    {
        _stream = stream;
        _entityId = entityId;
        _objectId = objectId;
        
    }
 
    public int EntityId
    {
        get { return _entityId; }
    }
 
    public Guid ObjectId
    {
        get { return _objectId; }
    }
 
 
    public override void Write(byte[] buffer, int offset, int count)
    {
        string html = System.Text.Encoding.UTF8.GetString(buffer, offset, count);
        html = "<preview>The new module works we can show here our custom text :-)  </preview>";
        buffer = System.Text.Encoding.UTF8.GetBytes(html);
        _stream.Write(buffer, 0, buffer.Length);
 
    } 
}
}

 

The good thing about this customisation that we do not change any CRM code or files, so even with the next CRM generation the code will probably work with little or no changes. And it is working directly with queue view, advanced find, history, activities view – anywhere the preview.aspx is originally requested.

The result – our own module is called as expected:
changed_activity_preview_stefan_scheller

Attention: Check your sub applications after the http module is working, probably you have to remove the module there.
Just use <remove name=”ExtensionModulActivity”/>, here is a good post about removing.

No Comments Yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.