Showing posts with label Timeline. Show all posts
Showing posts with label Timeline. Show all posts

Sunday, August 3, 2014

Programmatically add tasks to timeline in SharePoint 2013 Tasks list

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.

image

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

  1. var web = properties.Web as SPWeb;
  2. var doc = new XmlDocument();
  3. var taskList = web.Lists[properties.ListId];
  4. var root = taskList.RootFolder;
  5.  
  6. if (root.Properties["Timeline_Timeline"] == null)
  7. {
  8. //Add timeline related properties
  9. root.SetProperty("TimelineDefaultView", "Timeline");
  10. root.SetProperty("TimelineAllViews", "Timeline");
  11. //Add default xml to Timeline_Timeline property
  12. 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>
  13. <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>");
  14. root.Update();
  15. }
  16.  
  17. doc.LoadXml(root.Properties["Timeline_Timeline"].ToString());
  18.  
  19. //Create required xml nodes
  20. var tElement = doc.CreateElement("t");
  21. tElement.SetAttribute("id", "{" + properties.ListItemUniqueId.ToString() + "}");
  22. tElement.SetAttribute("uid", properties.ListItemId.ToString());
  23. tElement.SetAttribute("uidSrc", "1");
  24. tElement.SetAttribute("onTL", "1");
  25. tElement.SetAttribute("fmt", "0");
  26. tElement.SetAttribute("ch", "4294967295");
  27.  
  28. var mElement = doc.CreateElement("m");
  29. mElement.SetAttribute("id", "{" + properties.ListItemUniqueId.ToString() + "}");
  30. mElement.SetAttribute("uid", properties.ListItemId.ToString());
  31. mElement.SetAttribute("uidSrc", "1");
  32. mElement.SetAttribute("onTL", "1");
  33. mElement.SetAttribute("fmt", "2");
  34. mElement.SetAttribute("y", "35");
  35. mElement.SetAttribute("x", "0");
  36.  
  37. //Add created nodes to xml document
  38. doc.SelectNodes("/TLViewData/tskSet/t")[doc.SelectNodes("/TLViewData/tskSet/t").Count - 1].AppendChild(tElement);
  39. doc.SelectNodes("/TLViewData/mlSet/m")[doc.SelectNodes("/TLViewData/mlSet/m").Count - 1].AppendChild(mElement);
  40. root.SetProperty("Timeline_Timeline", doc.OuterXml);
  41. root.Update();
  42.  
  43. base.ItemAdded(properties);

After deploying the solution, tasks are added to the timeline automatically.

Monday, July 22, 2013

Timeline Feature in SharePoint 2013

In SharePoint 2013 we are given a great new feature, which is the ability to display tasks in a timeline. This is somewhat similar to what we have in MS Project Server. This feature is very helpful to track our tasks as they are visually organized with their staring and end dates. In this post I will discuss where and how we can use timelines in SharePoint.
We can have SharePoint timelines in 2 places.
1. Tasks List
When we create a new task list, an empty timeline is created by default.
image
Let’s add few tasks
image
Quite surprisingly, tasks are not displayed in the timeline. It took sometime to figure out that those tasks need to be added manually. I think it would’ve been better if it’s automatically added when we create a new task. Anyway this is what we have out of the box. In a next blog post I’ll explain a workaround to automatically add tasks to the timeline.
To add tasks to timeline, we need to either go to open menu (…) and click on “Add to Timeline” or go to ribbon and click on “Add to Timeline” button.
image image
Once we add task(s) to timeline it will display as below
image
But that is not all. If you check carefully you would notice that there is a new “Timeline” tab in the ribbon. Using the options in that tab we can do various modifications to the presentation of timeline.
image
Let me give an effective scenario of using the timeline tab to enhance the experience. Let’s say that there is a large task and few other tasks scheduled in same time frame. Then it would be better if I just have labels for smaller tasks rather than displaying them as new bars. To do that select the task, go to the timeline tab and click on “Display as Callout”.
image image
That looks more organized. Isn’t it.
2. Timeline WebPart
Tasks list is not the only place where we can use timeline feature. There is a separate timeline webpart. We can provide a tasks list which reside either in same site or a different sub site as the data source. Timeline webpart resides in the Content Rollup category.
image
After adding the webpart, we need fill required parameters to set the data source. The Type and Source dropdowns are populated according to the url provided. Since I’m having a few tasks lists in the given url, It populates the type as “List” and it gives me a list of Task Lists in source dropdown. I’ll select one tasks list from that.
image
This is what I’ll finally get after I complete all those configurations.
image
As you can see, I have the timeline tab in the ribbon here as well. So I can do various modifications to the presentation.
I believe that this will be a must have feature for a project site.