Home > Dynamics CRM 2011 > Custom Entity Calendar in CRM 2011

Custom Entity Calendar in CRM 2011

Calendar view in CRM gives information about Appointments, Service Activities. But there is a restriction to show custom entity unless we create an appointment for that.

In recent past I was working on a requirement to show calendar view for a custom entity, first we thought of creating appointment on creation of the record. But this is messing up with the existing appointments. We have different entities to show in calendar. So we thought to go by a custom web page as an IFrame.

Extend ASP.NET Calendar Control

What I did was I have extended ASP.NET calendar control to override DayRender event to use its capability to show the data in it. Then I used that control to show the custom entity data. Apart from overriding the DayRender event I have added few properties to get the custom data to calendar. Below is code snippet sample

DayRender Event – Code Snippet

protected void EventCalendarDayRender(object sender, DayRenderEventArgs e)
CalendarDay d = ((DayRenderEventArgs)e).Day;
TableCell c = ((DayRenderEventArgs)e).Cell;
if (this.EventSource == null)
DataTable dt = this.EventSource;
foreach (DataRow dr in dt.Rows)
{if (EventStartDateColumnName == string.Empty)
throw new ApplicationException("Must set Calendar's EventStartDateColumnName property when EventSource is specified");
if (EventEndDateColumnName == string.Empty)
throw new ApplicationException("Must set Calendar's EventEndDateColumnName property when EventSource is specified");
if (EventHeaderColumnName== string.Empty)
throw new ApplicationException("Must set Calendar's EventHeaderColumnName property when EventSource is specified");
if (!d.IsOtherMonth
&& d.Date >= Convert.ToDateTime(dr[this.EventStartDateColumnName]).Date
&& d.Date <= Convert.ToDateTime(dr[this.EventEndDateColumnName]).Date)
System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
// Show the Event Text
lbl.Text = "<BR />" + dr[EventHeaderColumnName].ToString();
// Set the Tool Tip
if (this.ShowDescriptionAsToolTip && this.EventDescriptionColumnName != string.Empty)
lbl.ToolTip = dr[EventDescriptionColumnName].ToString();
// Set the Back Color of the Label
if (EventBackColorName != null && dr[EventBackColorName] != null && dr[EventBackColorName] != "")
lbl.BackColor = Color.FromName(dr[EventBackColorName].ToString());
// Set the Fore Color
if (EventForeColorName != null && dr[EventForeColorName] != null && dr[EventForeColorName] != "")
lbl.ForeColor = Color.FromName(dr[EventForeColorName].ToString());

Web Page to show Custom Calendar

 Now I refer to custom calendar control on my aspx page. We need to pass custom properties values to show calendar.

Register Custom Control on Page

<%@ Register TagPrefix="EXXRM" 
Assembly="ExtendedCalendar" %>

Add Calendar Control on Page

<EXXRM:ExtCalendar ID="CCGlobalCalendar" runat="server" BackColor="White"          BorderWidth="2px" Font-Names="Verdana" Font-Size="8pt" ForeColor="#003399" Height="400px"         Width="80%" FirstDayOfWeek="Monday" NextMonthText="Next &gt;" PrevMonthText="&lt; Prev"         ShowDescriptionAsToolTip="True" EventDateColumnName="" EventDescriptionColumnName=""         EventHeaderColumnName="" CellPadding="2" DayNameFormat="Full"          EventBackColorName="" EventEndDateColumnName="" EventForeColorName=""          EventStartDateColumnName="">         <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />         <OtherMonthDayStyle ForeColor="#999999" BackColor="Silver"/>         <SelectedDayStyle BackColor="#E6EDF7" Font-Bold="True" ForeColor="#CCFF99" />         <SelectorStyle BorderColor="#E6EDF7" BorderStyle="Solid" BackColor="#99CCCC"              ForeColor="#336666" />         <DayStyle HorizontalAlign="Left" VerticalAlign="Top" Wrap="True" BorderColor="Silver" BorderWidth="1px" BorderStyle="Solid"/>         <DayHeaderStyle BorderWidth="1px" Font-Bold="True" Font-Size="8pt"              BackColor="#A5BFE1" ForeColor="#336666" Height="1px" />         <TitleStyle BackColor="SteelBlue" BorderColor="#3366CC" BorderWidth="1px" Font-Bold="True"             Font-Size="10pt" ForeColor="White" HorizontalAlign="Center"              VerticalAlign="Middle" Height="25px" />         <TodayDayStyle BackColor="#FFDC6D" ForeColor="White" />         <WeekendDayStyle BackColor="AliceBlue" />     </EXXRM:ExtCalendar>

Now we need to bind data to this control. For this blog post I am using defined Fetch XML but based on requirement we can generate EntityCollection. Here are the steps to bind data

Get the records –> Loop through the records to set the properties –> Create DataTable with entity data –> Bind data table to calendar

 Make sure to pass the required properties from data table.

Code to bind data to Calendar Control

string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='contact'>
<attribute name='fullname' /><attribute name='contactid' /><attribute name='modifiedon' />
<attribute name='createdon' />
<order attribute='fullname' descending='false' />
FetchExpression Query = new FetchExpression(fetchXML);
EntityCollection listEntities = GetService().RetrieveMultiple(Query);
DataTable dt = new DataTable();
dt.Columns.Add("Id", Type.GetType("System.Int32"));
dt.Columns.Add("EventStartDate", Type.GetType("System.DateTime"));
dt.Columns.Add("EventEndDate", Type.GetType("System.DateTime"));
dt.Columns.Add("EventHeader", Type.GetType("System.String"));
dt.Columns.Add("EventDescription", Type.GetType("System.String"));
dt.Columns.Add("EventForeColor", Type.GetType("System.String"));dt.Columns.Add("EventBackColor", Type.GetType("System.String"));
int idCount = 1;
foreach (Entity leave in listEntities.Entities)
DataRow dr;
dr = dt.NewRow();
dr["Id"] = idCount++;
dr["EventStartDate"] = Convert.ToDateTime(leave.Attributes["createdon"].ToString());
dr["EventEndDate"] = Convert.ToDateTime(leave.Attributes["modifiedon"].ToString());
dr["EventHeader"] = leave.Attributes["fullname"].ToString();
dr["EventDescription"] = leave.Attributes["fullname"].ToString();
dr["EventForeColor"] = "Navy";
CCGlobalCalendar.EventStartDateColumnName = "EventStartDate";
CCGlobalCalendar.EventEndDateColumnName = "EventEndDate";
CCGlobalCalendar.EventDescriptionColumnName = "EventDescription";
CCGlobalCalendar.EventHeaderColumnName = "EventHeader";CCGlobalCalendar.EventBackColorName = "EventBackColor";CCGlobalCalendar.EventForeColorName = "EventForeColor";
CCGlobalCalendar.EventSource = dt;

That’s it we are there. You can find the sample here

Hope it helps someone 🙂

  1. eric
    April 28, 2012 at 6:20 AM

    Hi Anil, this is cool. but I am getting error when I tried to run against CRM 2011 online instance –
    The binding to use to communicate to the federation service at ‘https://login.live.com/extSTS.srf’ is not specified.

    Source Error:

    Line 29: “;
    Line 30: FetchExpression Query = new FetchExpression(fetchXML);
    Line 31: EntityCollection listEntities = GetService().RetrieveMultiple(Query);

    • April 30, 2012 at 3:22 PM

      Hi Eric,
      You may have to change the GetService method to match with CRM Online authentication.

  2. Digitalcompiler
    October 15, 2012 at 5:34 PM

    hello Anil,

    how to show this calendar on Dashboard of CRM 2011? After we have customized it and we are getting the data from CRM, what we need is to show is on the dashboard. What will be the best way to do so..

  3. October 15, 2012 at 5:51 PM

    First publish it to a website and then you can add an IFrame in your dashboard which points to the published website URL.

    • Digitalcompiler
      October 15, 2012 at 6:30 PM

      Thanks a lot !

      I was asking because i heard it was not recommended to use aspx pages anymore in CRM 2011, is it so?

  4. October 15, 2012 at 6:43 PM

    Its not like “Not Recommended or Unsupported” as far as you use single sign on methodology. Hope you have that exposure.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: