Using JavaScript Gantt Chart in SharePoint Web Parts for Effective Project Management: Part 2
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (180).NET Core  (28).NET MAUI  (224)Angular  (113)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (41)Black Friday Deal  (1)Blazor  (234)BoldSign  (14)DocIO  (24)Essential JS 2  (110)Essential Studio  (199)File Formats  (72)Flutter  (135)JavaScript  (226)Microsoft  (121)PDF  (83)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (960)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)WPF  (162)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (10)Business intelligence  (55)Button  (4)C#  (163)Chart  (145)Chart of the week  (57)Cloud  (15)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (73)Development  (676)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (23)File Manager  (7)Gantt  (20)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (519)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (45)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (113)UI  (400)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (624)What's new  (337)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Using JavaScript Gantt Chart in SharePoint Web Part for Effective Project Management Part 2

Using JavaScript Gantt Chart in SharePoint Web Parts for Effective Project Management: Part 2

This is a follow-up to our previous blog, Using JavaScript Gantt Chart in SharePoint Web Part for Effective Project Management: Part 1, where we explained integrating the Syncfusion JavaScript Gantt Chart in a SharePoint web part.

Now, in this blog, we’ll see how to bind SharePoint list data to the JavaScript Gantt Chart and save the data changes made in the Gantt Chart back to the SharePoint list.

There are only two steps to do this:

  1. Data binding: Read and bind the SharePoint list data to the JavaScript Gantt Chart.
  2. Edit operation: Update the edited data from the Gantt Chart to the SharePoint list.

Let’s get started!

Data binding

A list is a collection of data displayed in rows and columns. SharePoint stores and displays information in the form of a list. These are like databases that we use to store information and build reports.

Step 1: Prepare SharePoint list data

You can bind an existing list from your SharePoint page or create a new list and bind it to the JavaScript Gantt Chart. To do so, follow these steps:

  1. Create the list data with a unique field through which the Syncfusion JavaScript Gantt Chart can identify child and parent records. For instance, let’s use the data in the following code example where the TaskID field is a unique field and ParentID in some records denotes that they are child records to the TaskID value that equals the ParentID field value.
    var projectData = [
        { 'TaskID': 1, 'TaskName': 'Planning', 'StartDate': new Date('02/23/2017'), 'Duration': 5 },
     
        { 'TaskID': 2, 'TaskName': 'Plan timeline', 'StartDate': new Date('02/23/2017'), 'Duration': 5,  'parentID': 1 },
     
        { 'TaskID': 3, 'TaskName': 'Plan budget', 'StartDate': new Date('02/23/2017'), 'Duration': 0,  'parentID': 1,  },
     
        { 'TaskID':4, 'TaskName': 'Design', 'StartDate': new Date('02/10/2017'), 'Duration': 3 },
     
        { 'TaskID':5, 'TaskName': 'Software Specification','StartDate': new Date('02/10/2017'), 'Duration': 3, 'parentID': 4 }
    ]

    The field names TaskID and ParentID can be whatever we choose, but we have to map those field names in the taskFields property of the JavaScript Gantt Chart. We will see this later in the blog.

  2. Add the Gantt Chart with empty data from the SharePoint web part. For empty data binding, we can pass an empty array to the dataSource property of Gantt Chart in the render method of the web part file (GanttChartWebPart.ts).
    Refer to the following code example.
    public render(): void {
    <section class="${styles.ganttChart} ${!!this.context.sdks.microsoftTeams ? styles.teams : ''}">
        <div id="Gantt-${this.instanceId}"></div>
    </section>`;
     
    this.ganttInstance = new Gantt({
       editSettings: {allowEditing: this.properties.allowEditing,    allowTaskbarEditing: this.properties.allowEditing},
       taskFields: {
          id: this.properties.taskID,
          name: this.properties.taskName,
          startDate: this.properties.startDate,
          duration: this.properties.duration,
          progress: this.properties.progress,
          parentID: this.properties.parentID
       },
       dataSource: this.properties.dataSource,
    ..
    }
    this.ganttInstance.appendTo('#Gantt-'+this.instanceId);
     
    protected async onInit(): Promise<void> {
     this.properties.dataSource = [];
    }

Step 2: Design properties panel to read SharePoint list data

Now, let’s design the properties panel to choose the SharePoint list data source and other input values needed for the Gantt Chart’s taskFields property to map the parent and child data from the list data source.

  1. In the getPropertyPaneConfiguration method, create a dropdown with values of list names available in SharePoint.
    protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
      return {
       …
        groups: [
        {
           groupName: strings.BasicGroupName,
           groupFields: [
             PropertyPaneDropdown('selectedList', {
              label: 'Connect to List Source',               
              options:  this._siteLists.map((list: string) => {
                 return <IPropertyPaneDropdownOption>{
                    key: list, text: list
                 }
              }),
             }
           ]
        }]
      }
    }
  2. Fetch the dropdown values using the SharePoint API service. I have used the SPHttpClient API to fetch the list names in SharePoint.
    protected async onInit(): Promise<void> {
      this._siteLists = await this._getSiteLists();
      …
    }
    
    public async _getSiteLists(): Promise<string[]> {
      const endpoint: string = `${this.context.pageContext.web.absoluteUrl}/_api/web/lists?$select=Title&$filter=Hidden eq false&$orderby=Title&$top=10`;
     
      const rawResponse: SPHttpClientResponse = await this.context.spHttpClient.get(
        endpoint,
        SPHttpClient.configurations.v1);
     
      return (await rawResponse.json()).value.map(
         (list: {Title: string}) => {
           return list.Title;
         }
      );
    }
  3. Then, add other input fields in the properties panel to map the taskFields property of the Gantt Chart instance in the render method. Refer to the following code example.
    groupFields: [
       PropertyPaneTextField('taskID', {
          value: this.properties.taskID,
          label: "Task ID"
       }),
       PropertyPaneTextField('taskName', {
          value: this.properties.taskName,
          label: "Task Name"
       }),
       PropertyPaneTextField('startDate', {
          value: this.properties.startDate,
          label: "Start Date"
       }),
       …
    ]
    
    public render(): void {
    <section class="${styles.ganttChart} ${!!this.context.sdks.microsoftTeams ? styles.teams : ''}">
       <div id="Gantt-${this.instanceId}"></div>
    </section>`;
    …
    this.ganttInstance = new Gantt({
       editSettings: {allowEditing: this.properties.allowEditing,    allowTaskbarEditing: this.properties.allowEditing},
       taskFields: {
         id: this.properties.taskID,
         name: this.properties.taskName,
         startDate: this.properties.startDate,
         duration: this.properties.duration,
         progress: this.properties.progress,
         parentID: this.properties.parentID
      },
      …
    })

After doing so, the look and appearance of the properties panel will be like the following image.

Designing the Properties Panel to Read SharePoint List Data
Designing the Properties Panel to Read SharePoint List Data

Step 3: Bind SharePoint list data to the JavaScript Gantt Chart

Finally, let’s bind data from the chosen list in the dropdown of the properties panel to the Gantt Chart. Here, we will retrieve the selected list data and bind it to the Gantt Chart with the help of the click event of the Apply button in the properties panel. Using the SPHttpClient API, get data from the SharePoint list.

Refer to the following code example.

PropertyPaneButton("", {
    text: "Apply",
    onClick: () => {
        this._getSiteListItems();
    }
})
…

public async _getSiteListItems(): Promise<void> {
  const endpoint: string = `${this.context.pageContext.web.absoluteUrl}/_api/web/lists/${this.properties.selectedList}List/items`;
 
  const rawResponse: SPHttpClientResponse = await this.context.spHttpClient.get(
    endpoint,
    SPHttpClient.configurations.v1);
  var response = await rawResponse.json();
  this.properties.dataSource = response.value.map(
    (list: {Id: number, TaskID: number, TaskName: string, StartDate: DateTime, Duration: number, Progress: number, ParentID: number}) => {
      return {Id: list.Id, TaskID: list.TaskID, TaskName: list.TaskName, StartDate: list.StartDate, Duration: list.Duration, Progress: list.Progress, ParentID: list.ParentID}
    }
  );
}

Refer to the following output image.

Binding SharePoint Data to the JavaScript Gantt Chart
Binding SharePoint Data to the JavaScript Gantt Chart

Editing operation

To allow users to edit the data in the JavaScript Gantt Chart, refer to editing tasks in JavaScript Gantt Chart documentation.

When a user edits data in the Gantt Chart, update the data changes back to the SharePoint list. Do this by using the actionComplete event of the Gantt Chart and sending post requests to the SharePoint list using the SPHttpClient API.

Refer to the following code example.

this.ganttInstance = new Gantt({
   dataSource: this.properties.dataSource,
   actionComplete: (args: ActionCompleteArgs) => {
     if (args.requestType == 'save'){
       for (var i = 0; i < args.modifiedTaskData.length; i++) {
         var url = this.context.pageContext.web.absoluteUrl + '/_api/web/lists/' + this.properties.selectedList + 'List/items(' + args.modifiedTaskData[i]['Id'] + ')';
         this.context.spHttpClient.post(url, SPHttpClient.configurations.v1,  {
           headers: {
             'Accept': 'application/json;odata=nometadata',
             'Content-type': 'application/json;odata=nometadata',
             'odata-version': '',
             'IF-MATCH': '*',
             'X-HTTP-Method': 'MERGE'
           },
           body:  JSON.stringify(args.modifiedTaskData[i])
         })
       } 
     }
 }
})

Now the edited data in the JavaScript Gantt Chart will be updated on the SharePoint page.

GitHub reference

For more details, check out the complete code example for integrating the Syncfusion JavaScript Gantt Chart in a SharePoint web part on GitHub.

Conclusion

Thanks for reading! In this blog, we have seen how to bind SharePoint list data to the Syncfusion JavaScript Gantt Chart and save the data changes made in the Gantt Chart back to the SharePoint list. Try the steps in the part 1 and 2 blogs to effectively manage your projects.

In addition to the Gannt Chart, there are more than 80 JavaScript UI controls, such as Grid, Scheduler, Charts, and others that can also be integrated with SharePoint web parts. Try them out and leave your feedback in the comments section below.

If you want to try Syncfusion components, you can download our free trial. Also, check out our demos and documentation for detailed explanations and the facts you need to proceed further.

You can also reach us through our support forumssupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed