Attention: We are retiring the ASP.NET Community Blogs. Learn more >

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.

10 Comments

  • perhaps you should enclose the custom html in cdata tags.

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

  • 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.

  • Awesome, thanks for that tip Zahir

  • 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 -&gt; Read only data (could be a table) with 6 bits of information.

    Tab 2 -&gt; data for summary in fields 1, 11, and 23.

    Tab 3 -&gt; Data for summary in fields 5, 6, and 9

    Thanks - Tim

  • 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.

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

    Thx, Stefan

  • Hi Stefan

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

    enjoy!

  • 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

  • Hello,

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

    Thanks!

Comments have been disabled for this content.