using System; using System.Diagnostics; using System.Xml; using System.Data; using System.IO; namespace Msdn.Outlook.Calendaring { /// /// A sample SharePoint task list calendar provider /// public class TaskListCalendarProvider : CalendarProvider { // Local variable that holds the site of the task // list to link, along with the name of the task // list. string _initData = ""; /// /// Sets the value of the the _initData local variable /// /// public override void Configure(string initData) { _initData = initData; } /// /// Gets a list of events formatted so they can be /// displayed in Outlook /// /// Event collection public override EventCollection GetEvents() { EventCollection events = new EventCollection(); DataSet dsTasks = GetTasks(); if (dsTasks.Tables.Count > 1) { DataView dvTasks = new DataView(dsTasks.Tables["row"], "", "ows_StartDate", DataViewRowState.CurrentRows); DataTable dtTasks = dvTasks.ToTable(); DateTime prevStartDate = DateTime.MinValue; int hours2add = 0; foreach (DataRow drTask in dtTasks.Rows) { DateTime startDateTime = DateTime.Parse(drTask["ows_StartDate"].ToString()); if (startDateTime != prevStartDate) { hours2add = 0; prevStartDate = startDateTime; } else { hours2add = hours2add + 1; } startDateTime = startDateTime.AddHours(hours2add); DateTime endDateTime; try { endDateTime = DateTime.Parse(drTask["ows_DueDate"].ToString()); } catch { endDateTime = startDateTime; } if (endDateTime == startDateTime) endDateTime = startDateTime.AddMinutes(60); Event e = new Event(); try { e.Author = drTask["ows_AssignedTo"].ToString().Split('#')[1]; } catch { e.Author = "Unassigned"; } try { e.Description = GetDescription(drTask); } catch { e.Description = ""; } e.Location = " ** SharePoint Task List: " + _initData.Split(';')[1] + " at Site: " + _initData.Split(';')[0] + " **"; e.Start = startDateTime.ToUniversalTime(); e.End = endDateTime.ToUniversalTime(); e.Title = drTask["ows_Title"].ToString() + " (" + drTask["ows_AssignedTo"].ToString().Split('#')[1] + ")"; events.Add(e); } } return events; } /// /// Returns string to be displayed in the task description /// field in Outlook /// /// A datarow object containing source data for description /// String value representing the description to display private string GetDescription(DataRow drTask) { string assignedTo = "Unassigned"; string status = "Unknown"; string estHours = "0"; string description = ""; string linkUrl = ""; try { assignedTo = drTask["ows_AssignedTo"].ToString().Split('#')[1]; } catch { } try { status = drTask["ows_Status"].ToString(); } catch { } try { estHours = drTask["EstHours"].ToString(); } catch { } try { description = drTask["ows_Body"].ToString().Replace("
", "").Replace("
", ""); } catch { } try { linkUrl = _initData.Split(';')[0] + "/Lists/" + _initData.Split(';')[1] + "/DispForm.aspx?ID=" + drTask["ows_ID"].ToString(); linkUrl = linkUrl.Replace(" ", "%20"); } catch { } return string.Format("Assigned To: {0}\nStatus: {1}\nDescription: {2}\n\nLink to Task: {3}", assignedTo, status, description, linkUrl); } /// /// Return a dataset containing all items in the specified task list /// /// DataSet containing all items in attached SharePoint task list public DataSet GetTasks() { // Get handle to web service ListsService.Lists lsvc = new ListsService.Lists(); // Assume current user has at least read permissions to // source task list lsvc.Credentials = System.Net.CredentialCache.DefaultCredentials; string url = _initData.Split(';')[0] + "/_vti_bin/lists.asmx"; string listName = _initData.Split(';')[1]; lsvc.Url = url; XmlDocument xmlDoc = new XmlDocument(); XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", ""); XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", ""); XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", ""); DataSet dsTasks = new DataSet(); ndQueryOptions.InnerXml = "FALSETRUE"; // Retrieve the current task list from SharePoint XmlNode ndListItems = lsvc.GetListItems(listName, null, ndQuery, ndViewFields, "2000", ndQueryOptions, null); StringReader sr = new StringReader(ndListItems.OuterXml); dsTasks.ReadXml(sr); sr.Close(); if (dsTasks == null || dsTasks.Tables.Count == 0) return null; else return dsTasks; } } }