Originally posted on: http://geekswithblogs.net/djacobus/archive/2013/11/22/154708.aspx
Developing the artifacts for my last post, I used the spCalendarView as the basis for a web part to view a rollup of events(Calendar) within a SharePoint site collection. It isn’t the only option and I thought I would follow up that blog post with a demonstration of using the same code base except use Full Calendar JQuery Plug-in as the code base. Therefore, I will use the same project on CodePlex and add another web part which uses Full Calendar to display the rollup. I am using a server side approach to get the events into Full Calendar because I already have the plumbing in place to acquire the data. I think that other technologies would be better suited here, that being said, lets make Full Calendar work for this project! I will take the SharePoint 2010 Colored Calendar CodePlex project and add this web part. Lets Start with the Solution on CodePlex:
The current project structure:
We are going to add another web part to this solution called FullCalendar.
We need to add some additional artifacts:
the FullCalendar css and scripts:
Now we add the code from our Custom Calendar Web Part to get the data and hook it into The FullCalendar JQuery plug-in.
1: using System;
2: using System.ComponentModel;
3: using System.Web;
4: using System.Text;
5: using System.Web.UI;
6: using System.Web.UI.WebControls;
7: using System.Web.UI.WebControls.WebParts;
8: using Microsoft.SharePoint;
9: using Microsoft.SharePoint.WebControls;
10: using Microsoft.SharePoint.Utilities;
11: using System.Data;
12:
13: namespace Demo.Calendar.FullCalendar
14: {
15: [ToolboxItemAttribute(false)]
16: publicclass FullCalendar : WebPart
17: {
18: private String PartTitle = "SITE CALENDAR";
19: [WebBrowsable(true),
20: WebDisplayName("Web Part Title"),
21: WebDescription(""),
22: Personalizable(PersonalizationScope.Shared),
23: Category("Settings"),
24: DefaultValue("MY TOOLS")
25: ]
26: publicstring _PartTitle
27: {
28: get { return PartTitle; }
29: set { PartTitle = value; }
30: }
31:
32: private String WebLocation = "/";
33: [WebBrowsable(true),
34: WebDisplayName("Relative path to starting site"),
35: WebDescription(""),
36: Personalizable(PersonalizationScope.Shared),
37: Category("Settings"),
38: DefaultValue("/Departments")
39: ]
40: publicstring _WebLocation
41: {
42: get { return WebLocation; }
43: set { WebLocation = value; }
44: }
45: privatebool ExpandDefault = false;
46: [WebBrowsable(true),
47: WebDisplayName("Expand part by default?"),
48: WebDescription(""),
49: Personalizable(
50: PersonalizationScope.Shared),
51: Category("Settings"),
52: DefaultValue(false)
53: ]
54: publicbool _ExpandDefault
55: {
56: get { return ExpandDefault; }
57: set { ExpandDefault = value; }
58: }
59:
60: // Specifies the calendar for the add event.
61: static SPSite site = SPContext.Current.Site;
62: privatestring strListName = "RollupCalendar";
63: [WebBrowsable(true),
64: WebDisplayName("Top Level Calendar Name"),
65: WebDescription("Top Level Calendar Name"),
66: Personalizable(PersonalizationScope.Shared),
67: Category("Settings"),
68: DefaultValue("Calendar Tools")
69: ]
70: publicstring _strListName
71: {
72: get { return strListName; }
73: set { strListName = value; }
74: }
75: protectedoverridevoid CreateChildControls()
76: {
77: SPSite site = SPContext.Current.Site;
78: Microsoft.SharePoint.WebControls.CssRegistration cssLink = new Microsoft.SharePoint.WebControls.CssRegistration();
79: cssLink.Name = site.Url + "/Style Library/Demo/MyCalendar.css";
80: this.Page.Header.Controls.Add(cssLink);
81: cssLink = new Microsoft.SharePoint.WebControls.CssRegistration();
82: cssLink.Name = site.Url + "/Style Library/Demo/fullcalendar.css";
83: this.Page.Header.Controls.Add(cssLink);
84: cssLink = new Microsoft.SharePoint.WebControls.CssRegistration();
85: cssLink.Name = site.Url + "/css/ui-lightness/jquery-ui-1.10.3.custom.css";
86: this.Page.Header.Controls.Add(cssLink);
87:
88:
89: ScriptLink scriptt = new ScriptLink();
90: //scriptt.Name = "~sitecollection/scripts/jquery-1.9.1.js";
91: //if (!this.Page.Header.Controls.Contains(scriptt))
92: // this.Page.Header.Controls.Add(scriptt);
93:
94:
95: scriptt = new ScriptLink();
96: scriptt.Name = "~sitecollection/script/jquery-ui-1.10.3.custom.min.js";
97: if (!this.Page.Header.Controls.Contains(scriptt))
98: this.Page.Header.Controls.Add(scriptt);
99: scriptt = new ScriptLink();
100: scriptt.Name = "~sitecollection/scripts/fullcalendar.min.js";
101: scriptt.Defer = true;
102: if (!this.Page.Header.Controls.Contains(scriptt))
103: this.Page.Header.Controls.Add(scriptt);
104: scriptt = new ScriptLink();
105: scriptt.Name = "~sitecollection/scripts/Demo2.js";
106: scriptt.Defer = true;
107: if (!this.Page.Header.Controls.Contains(scriptt))
108: this.Page.Header.Controls.Add(scriptt);
109:
110: using (SPSite sitex = new SPSite(SPContext.Current.Site.ID))
111: {
112: using (SPWeb web = sitex.OpenWeb(SPContext.Current.Web.ID))
113: {
114:
115:
116: StringBuilder events = new StringBuilder(" ");
117: DateTime StartDate = DateTime.Now.AddMonths(-3);
118: string strStartDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(StartDate);
119: DateTime EndDate = DateTime.Now.AddMonths(3);
120: string strEndDate = SPUtility.CreateISO8601DateTimeFromSystemDateTime(EndDate);
121: //string camlquery = "<Where><And>"
122: // + "<Lt><FieldRef Name='EventDate' /><Value Type='DateTime'>" + strEndDate + "</Value></Lt><Gt><FieldRef Name='EventDate' /><Value Type='DateTime'>" + strStartDate + "</Value></Gt>"
123: // + "</And></Where>";
124:
125: //SPSiteDataQuery qry = new SPSiteDataQuery();
126:
127: //qry.Lists = "<Lists ServerTemplate=\"106\" />";
128: //qry.Query = camlquery;
129: //qry.Webs = "<Webs Scope='Recursive' />";
130: //qry.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='EventDate' /><FieldRef Name='ID' /><FieldRef Name='EncodedAbsUrl' /><FieldRef Name='FileRef' /><FieldRef Name='Modified' /><FieldRef Name='fAllDayEvent' /><FieldRef Name='EndDate' /><ProjectProperty Name=\"Title\" />";
131:
132:
133:
134:
135:
136: //System.Data.DataTable results = web.GetSiteData(qry);
137: System.Data.DataTable results = GetQueryData(web);
138: foreach (System.Data.DataRow item in results.Rows)
139: {
140: ////here is where we need to add classes based on id
141: //string iTem = item["Title"].ToString();
142: //string backgroundColor = string.Empty;
143: //string textColor = string.Empty;
144: //string borderColor = string.Empty;
145: //switch (iTem)
146: //{
147: // case "SubSite1": backgroundColor = "green"; textColor = "yellow"; borderColor = "green";
148: // break;
149: // case "Demo15": backgroundColor = "magenta"; textColor = "yellow"; borderColor = "black";
150: // break;
151: // default: backgroundColor = "yellow"; textColor = "white"; borderColor = "black";
152: // break;
153: //}
154:
155:
156: string eventString = string.Format("id: {0}, title: '{1}', start: '{2}', end: '{3}', allDay: false", item["ID"].ToString(), item["ProjectProperty.Title"].ToString() + " - " + item["Title"].ToString(), item["EventDate"].ToString(), item["EndDate"].ToString());
157: events.Append("{" + eventString + "},");
158:
159:
160: }
161: LiteralControl script = new LiteralControl("<script type='text/javascript'>$(document).ready(function(){$('#MyCalendar').fullCalendar({ header: { left: 'today', center: 'prev,title,next', right: 'basicDay,basicWeek,month' }, events: [" + events.ToString().Substring(0, events.ToString().Length - 1) + "] });});</Script>");
162:
163: LiteralControl mainDiv = new LiteralControl("<div id='MyCalendar'></div>");
164: this.Controls.Add(mainDiv);
165: this.Controls.Add(script);
166:
167: }
168: }
169: }
170:
171: private DataTable GetQueryData(SPWeb web)
172: {
173: var query = new SPSiteDataQuery();
174:
175: //no need to grab all calendars thus no servertemplate
176: //query.Lists = "<Lists ServerTemplate=\"106\" />";
177: //no need to grad lists by id need to know all lists before hand
178: // query.Lists = @"<Lists>
179: // <List ID='{080BA3CB-9D7F-4E6B-AE02-2E5BFB79EF91}' />
180: // <List ID='{B65A63B6-B857-423A-AE22-AA7957E78157}' />
181: // </Lists>";
182: //we are going to grab by content type thus nomral calendars will not be caught in the spsitedataquery
183: SPContentType cTypeCollection = web.ContentTypes["RollUpCalendarContentType"];
184:
185:
186:
187:
188:
189: stringwhere = string.Format(
190: @"<Where>
191: <BeginsWith>
192: <FieldRef Name='ContentTypeId'/>
193: <Value Type='Text'>{0}</Value>
194: </BeginsWith>
195: </Where>", cTypeCollection.Id);
196:
197: // Set the query string.
198: query.Query = where;
199:
200:
201:
202: query.Webs = "<Webs Scope='SiteCollection' />";
203: query.ViewFields = "<FieldRef Name='Title' />";
204: query.ViewFields += "<FieldRef Name='ID' />";
205: query.ViewFields += "<FieldRef Name='EventDate' />";
206: query.ViewFields += "<FieldRef Name='EndDate' />";
207: query.ViewFields += "<FieldRef Name='Location' />";
208: query.ViewFields += "<FieldRef Name='Description' />";
209: query.ViewFields += "<FieldRef Name='fAllDayEvent' />";
210: query.ViewFields += "<FieldRef Name='fRecurrence' />";
211: query.ViewFields += "<FieldRef Name='MilCategory' />";
212: query.ViewFields += "<FieldRef Name='ContentType'/>";
213: query.ViewFields += "<ProjectProperty Name='Title' />";
214: query.RowLimit = 100;
215:
216:
217: return web.GetSiteData(query);
218: }
219: }
220: }
Lines 156 through 165 are where we really make the change to add our data to the FullCalendar Plugin. We need to put our event data into a form that FullCalendar understands which happens on line 156.
The api to form this event can be found on the FullCalendar web site. It is not hard to figure out! So that was it! we now have a FullCalendar Displaying our data! Which gives us a complete community to help us with our print and display issues!
You can see that I tried many options (commented out code) to try and color the events but I ended up just using JQuery to provide the color changes:
$(document).ready(function () { $(".fc-event").css('background-color', 'white'); //all event bacgrounds $("span.fc-event-title:contains('SubSite1')").parent().css('background-color', 'red'); $("span.fc-event-title:contains('SubSite1')").parent().parent().css('background-color', 'red'); $("span.fc-event-title:contains('Demo20')").parent().css('background-color', 'yellow').css('color', 'black'); $("span.fc-event-title:contains('HumanResources')").parent().css('background-color', 'cyan').css('color', 'black'); });Which is similar to the JavaScript we used for the spCalendarView web part. This should provide a good starting point to add the event handling to be able to edit calendars from this web part and add events to other calendars.