In a previous post I described about the new Timeline feature in SharePoint 2013. But tasks are not added to the timeline automatically, which is a huge drawback. As a result we need to create the task and it to the timeline manually as below.
From this post I will explain how to add tasks to timeline automatically. There are two options to derive at a solution. They are,
- Using JavaScripts
- Using an Event Receiver
In this article I will show a way to implement the task using Item Added event receiver configured to Tasks list type. Following are two logical steps included in the solution.
1. Set required properties in the tasks list
In the RootFolder of a particular Tasks list, there are three properties relevant to the Timeline control. When we create the list, those properties are not automatically set. As a result we have to set those three properties, namely,
- TimeLineDefaultView
- TimeLineAllViews
- TimeLine_TimeLine
2. Get xml document in TimeLine_TimeLine property, and add required nodes
We have to add xml nodes to “tskSet” and “mlSet” sections to represent the task which is to be displayed in Timeline control.
Following is the sample code I used to execute above steps
- var web = properties.Web as SPWeb;
- var doc = new XmlDocument();
- var taskList = web.Lists[properties.ListId];
- var root = taskList.RootFolder;
- if (root.Properties["Timeline_Timeline"] == null)
- {
- //Add timeline related properties
- root.SetProperty("TimelineDefaultView", "Timeline");
- root.SetProperty("TimelineAllViews", "Timeline");
- //Add default xml to Timeline_Timeline property
- root.SetProperty("Timeline_Timeline",@"<TLViewData><fmtSet><fmt id='0' clr='FFEE2222' thm='0001' t1='0' t2='1' type='0' /><fmt id='1' clr='FFEE2222' thm='0001' t1='2' t2='3' type='1' /><fmt id='2' clr='FFEE2222' thm='0001' t1='4' t2='5' type='2' /><fmt id='3' clr='FFEE2222' thm='0001' t1='6' t2='7' type='3' /></fmtSet><fltSet><ft id='{00000000-0000-0000-0000-000000000000}' uid='4294967295' uidSrc='1' onTL='0' fmt='1' y='4294967282' x='0' h='20' /></fltSet><tskSet><t id='{00000000-0000-0000-0000-000000000000}' uid='4294967295' uidSrc='1' onTL='0' fmt='0' ch='4294967295' /></tskSet><options dateFormat='255' panZoomT='9' ProjSummFmt='3' showDates='1' showProjSummDates='0' showToday='1' showTS='1' timelineHeight='133' timelineWidth='-1' timescaleT='8' todayT='10' /><mlSet>
- <m id='{00000000-0000-0000-0000-000000000000}' uid='4294967295' uidSrc='1' onTL='0' fmt='2' y='35' x='0' /></mlSet><txtSet><style id='0' type='0' clr='FFEE2222' thm='0001' sz='8' font='Segoe UI' bold='0' ital='0' und='0' strk='0' /><style id='1' type='1' clr='FFEE2222' thm='0001' sz='8' font='Segoe UI' bold='0' ital='0' und='0' strk='0' /><style id='2' type='2' clr='FF999999' thm='0001' sz='8' font='Segoe UI' bold='0' ital='0' und='0' strk='0' /><style id='3' type='3' clr='FFB3B3B3' thm='0001' sz='8' font='Segoe UI Light' bold='0' ital='0' und='0' strk='0' /><style id='4' type='4' clr='FF525051' thm='0001' sz='10' font='Segoe UI' bold='0' ital='0' und='0' strk='0' /><style id='5' type='5' clr='FFB3B3B3' thm='0001' sz='8' font='Segoe UI Light' bold='0' ital='0' und='0' strk='0' /><style id='6' type='6' clr='FF999999' thm='0001' sz='9' font='Segoe UI' bold='0' ital='0' und='0' strk='0' /><style id='7' type='7' clr='FF999999' thm='0001' sz='8' font='Segoe UI' bold='0' ital='0' und='0' strk='0' /><style id='8' type='8' clr='FF999999' thm='0001' sz='8' font='Segoe UI' bold='0' ital='0' und='0' strk='0' /><style id='9' type='9' clr='FFFFA614' thm='0001' sz='8' font='Segoe UI Semibold' bold='1' ital='0' und='0' strk='0' /><style id='10' type='10' clr='FFFFA72B' thm='0001' sz='10' font='Segoe UI Semibold' bold='0' ital='0' und='0' strk='0' /></txtSet></TLViewData>");
- root.Update();
- }
- doc.LoadXml(root.Properties["Timeline_Timeline"].ToString());
- //Create required xml nodes
- var tElement = doc.CreateElement("t");
- tElement.SetAttribute("id", "{" + properties.ListItemUniqueId.ToString() + "}");
- tElement.SetAttribute("uid", properties.ListItemId.ToString());
- tElement.SetAttribute("uidSrc", "1");
- tElement.SetAttribute("onTL", "1");
- tElement.SetAttribute("fmt", "0");
- tElement.SetAttribute("ch", "4294967295");
- var mElement = doc.CreateElement("m");
- mElement.SetAttribute("id", "{" + properties.ListItemUniqueId.ToString() + "}");
- mElement.SetAttribute("uid", properties.ListItemId.ToString());
- mElement.SetAttribute("uidSrc", "1");
- mElement.SetAttribute("onTL", "1");
- mElement.SetAttribute("fmt", "2");
- mElement.SetAttribute("y", "35");
- mElement.SetAttribute("x", "0");
- //Add created nodes to xml document
- doc.SelectNodes("/TLViewData/tskSet/t")[doc.SelectNodes("/TLViewData/tskSet/t").Count - 1].AppendChild(tElement);
- doc.SelectNodes("/TLViewData/mlSet/m")[doc.SelectNodes("/TLViewData/mlSet/m").Count - 1].AppendChild(mElement);
- root.SetProperty("Timeline_Timeline", doc.OuterXml);
- root.Update();
- base.ItemAdded(properties);
After deploying the solution, tasks are added to the timeline automatically.
5 comments:
Do you have a way to highlight the items in the code that we need to change per our server?
Hi,
I need your help in fixing this. Can you please share steps on how to achieve this?
Hi,
How to set programmatically "Display as Callout " view for timeline items, by Default "Display as bar" view is applied. Can any one please share the code how to achive my view?
Hi I believe I found a simpler solution...
I am using SharePoint 2013 hosted (office 365) and MS Project.
I noticed that when you are on MS Project, you can highlight all tasks, right click, and select ADD TO TIMELINE.
Upon clicking save, it uploaded to sharepoint with the option checked and all tasks showed up.
Bruno
Could you please break the steps down to achieving this. I believe there's some assumption making I would need to do but I rather steer from making assumptions.
Post a Comment