Kids, don't try this at home!

Tips & Tricks for C#, ASP.NET and CRM

CRM 4 Preview view render custom content

We needed to display related entities in the preview view of a custom entity grid, there is no supported way of doing this, so we created a httpmodule, trapped the calls to \_grid\preview.aspx and altered the rendered html.



Here is a snippet of code. You can download the skeleton solution from here.

private void context_BeginRequest(object sender, EventArgs e)

{

    if (_isEnabled)

    {

        HttpApplication app = sender as HttpApplication;

        if (app.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith("preview.aspx"))

        {

            int type = (app.Request.QueryString["type"] != null) ? int.Parse(app.Request.QueryString["type"]) : -1;

            Guid id = (app.Request.QueryString["id"] != null) ? new Guid(app.Request.QueryString["id"]) : Guid.Empty;

 

            if (type > 0 && id != Guid.Empty)

            {

                app.Response.ContentType = "text/xml";

                app.Response.Filter = new PreviewFilter(app.Response.Filter, type, id);

            }

        }

    }

}


PreviewFilter.cs

public class PreviewFilter : Stream

{

    private Stream _stream;

    private long _position;

 

    private int _entityId;

    private Guid _objectId;

 

    public PreviewFilter(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>Hello World!</preview>"; // enjoy!

 

        buffer = System.Text.Encoding.UTF8.GetBytes(html);

        _stream.Write(buffer, 0, buffer.Length);

    }

}


Compile, copy to the \bin\ directory on the server, open up web.config and add it to the HttpModules section.

Comments

Zahir said:

perhaps you should enclose the custom html in cdata tags.

# February 18, 2009 9:01 AM

gperera said:

Not sure I understand you correctly, can you explain what you mean?

# February 22, 2009 9:36 PM

Zahir said:

Try creating a custom table using this method -- CRM will strip your html tags from the output.

If you enclose your custom html block in a cdata tag (but still within a preview tag), it will render correctly.

The preview->cdata->myhtml pattern is used by CRM itself.

# February 23, 2009 3:31 AM

gperera said:

Awesome, thanks for that tip Zahir

# February 23, 2009 3:00 PM

Tim Perry said:

Could you use something similar to add a summary view to a tab on a "Main Application Form" of a Custom Entity? I need a custom entity with a read-only summary on the first tab that is linked to the actual CRM fields on other tabs.....

Tab 1 -> Read only data (could be a table) with 6 bits of information.

Tab 2 -> data for summary in fields 1, 11, and 23.

Tab 3 -> Data for summary in fields 5, 6, and 9

Thanks - Tim

# March 2, 2009 7:53 PM

gperera said:

You could, but I think it's better if you create an iframe, embed a custom .aspx that grab data from those fields using the crm sdk.

# March 4, 2009 12:34 AM

Stefan said:

This sound like a great idea, can you provide the original rar file? The download seems broken.

Thx, Stefan

# June 24, 2009 8:39 AM

gperera said:

Hi Stefan

Sorry about the broken link, I've uploaded the original file.

enjoy!

# June 24, 2009 9:36 PM

sas_1239 said:

Hi,

i tried to use this, my crm fails to load with the error pointing to the new entry in the web.config. Could you please send me your Crm's web.config file after registering this new HttpModule.

Thanks,

san

# July 8, 2010 9:50 AM

Denis said:

Hello,

Really interesting! The link seems broken. Could you provide another link for the download?

Thanks!

# March 14, 2011 10:45 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)