Friday, September 19, 2008 4:50 PM gperera

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.
Filed under: , ,

Comments

# re: CRM 4 Preview view render custom content

Wednesday, February 18, 2009 9:01 AM by Zahir

perhaps you should enclose the custom html in cdata tags.

# re: CRM 4 Preview view render custom content

Sunday, February 22, 2009 9:36 PM by gperera

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

# re: CRM 4 Preview view render custom content

Monday, February 23, 2009 3:31 AM by Zahir

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.

# re: CRM 4 Preview view render custom content

Monday, February 23, 2009 3:00 PM by gperera

Awesome, thanks for that tip Zahir

# re: CRM 4 Preview view render custom content

Monday, March 02, 2009 7:53 PM by Tim Perry

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

# re: CRM 4 Preview view render custom content

Wednesday, March 04, 2009 12:34 AM by gperera

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.

# re: CRM 4 Preview view render custom content

Wednesday, June 24, 2009 8:39 AM by Stefan

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

Thx, Stefan

# re: CRM 4 Preview view render custom content

Wednesday, June 24, 2009 9:36 PM by gperera

Hi Stefan

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

enjoy!

Leave a Comment

(required) 
(required) 
(optional)
(required)