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.