Archive

Posts Tagged ‘crm appointment’

Custom Entity Calendar in CRM 2011

April 24, 2012 6 comments

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)
return;
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());
c.Controls.Add(lbl);
}
}
}

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" 
Namespace="ExtendedCalendar" 
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' />
</entity></fetch>";
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";
dt.Rows.Add(dr);
} 
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 🙂