Skip to main content

Chapter 3 - GraphAPI Calendar Events Demo

Create a folder CreateAPIEventsDemo and create react webpart

install types for graph api as below. which adds dev dependencies to our JSON files.

"npm install @microsoft/microsoft-graph-types --save-dev"

now go to components-.ts file 

We are adding context 

context:WebPartContext
-------------------------------------------
Created a new file in components folder ICalendarEventsState.ts
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';

export interface ICalendarEventsDemoState{
    events:MicrosoftGraph.Event[];
}

We have created ICalendarEventsDemoState interface with events as property with array of events from MicrosoftGraph.

for our convenience we import graphapi types import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';

----------------
In Main.ts file
public render(): void {
    const elementReact.ReactElement<ICalendarEventsDemoProps> = React.createElement(
      CalendarEventsDemo,
      {
        description: this.properties.description,
        context: this.context
      }
    );

    ReactDom.render(elementthis.domElement);
  }
I have add context property.
-------------------------
Now in .tsx file, 
Import
import { MSGraphClient } from '@microsoft/sp-http';
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';

We need to add our State interface to our .tsx file.
and add constructore method

constructor(props:ICalendarEventsDemoProps) {
    super(props);
    this.state = {
      events = []
    }
  }

Then creating a componentDidMount for Rest API to get values of events.
public componentDidMount():void {
    this.props.context.MSGraphClient.getClient().then((client:MSGraphClient):void => {
      client.api('me/calendar/events')
      .version("v1.0")
      .select("*")
      .get((error:anyeventsResponserawResponse?:any)=>{
        if(error){
          console.error("Message is: "+error);
        }
        const calendarEventsMicrosoftGraph.Event[] = eventsResponse.value;
        this.setState({events:calendarEvents})
      })
    })
  }

Now go to render method
 public render(): React.ReactElement<ICalendarEventsDemoProps> {
    return (
      <div>
        <ul>
          {
            this.state.events.map((itemkey)=>{
              <li key={item.id}>
                {item.subject},
                {item.organizer.emailAddress.name},
                {item.start.dateTime.substr(0,10)},{item.start.dateTime.substr(12,5)},
                {item.end.dateTime.substr(0,10)},{item.end.dateTime.substr(12,5)}
              </li>
            })
          }
        </ul>

        <style>
          {`
          table{
            border:1px solid black;
            backgroud:aqua;
          }
          `}
        </style>
        
        <table>
          <tr>
            <td>Subject</td>
            <td>Organizer name</td>
            <td>Start Date</td>
            <td>Start Time</td>
            <td>End Date</td>
            <td>End Time</td>
          </tr>
          {
            this.state.events.map((item,key)=>{
              <tr>
                <td>{item.subject}</td>
                <td>{item.organizer.emailAddress.name}</td>
                <td>{item.start.dateTime.substr(0,10)}</td>
                <td>{item.start.dateTime.substr(12,5)}</td>
                <td>{item.end.dateTime.substr(0,10)}</td>
                <td>{item.end.dateTime.substr(12,5)}</td>
              </tr>
            })
          }
        </table>
      </div>
    );
  }
------------------------------------------------
Before we build and deploy, we need to assign permissions to graph api to run it.
In config folder- go to package-solution.json

Note in graph explore we did consent permission in graph explore purpose only. 

Add webapirequests with Calendars.Read to make permissions to read all the events from calendar through our Webpart.

{
  "$schema""https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
  "solution": {
    "name""graph-api-events-demo-client-side-solution",
    "id""867b0c19-4114-4149-ab46-5326e711a47c",
    "version""1.0.0.0",
    "includeClientSideAssets"true,
    "isDomainIsolated"false,
    "webApiPermissionRequests": [
      {
        "resource""Microsoft Graph",
        "scope":"Calendars.Read"
      }
    ],
    "developer": {
      "name""",
      "websiteUrl""",
      "privacyUrl""",
      "termsOfUseUrl""",
      "mpnId"""
    }
  },
  "paths": {
    "zippedPackage""solution/graph-api-events-demo.sppkg"
  }
}

-----------------------------------------------------

.tsx code:

import * as React from 'react';
import styles from './CalendarEventsDemo.module.scss';
import { ICalendarEventsDemoProps } from './ICalendarEventsDemoProps';
import { escape } from '@microsoft/sp-lodash-subset';

import { MSGraphClient } from '@microsoft/sp-http';
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
import { ICalendarEventsDemoState } from './ICalendarEventsState';

export default class CalendarEventsDemo extends React.Component<ICalendarEventsDemoProps,ICalendarEventsDemoState, {}> {

  constructor(props:ICalendarEventsDemoProps) {
    super(props);
    this.state = {
      events : []
    }
  }
  public componentDidMount():void {
    this.props.context.msGraphClientFactory.getClient().then((client:MSGraphClient):void => {
      client.api('me/calendar/events')
      .version("v1.0")
      .select("*")
      .get((error:anyeventsResponserawResponse?:any)=>{
        if(error){
          console.error("Message is: "+error);
        }
        const calendarEventsMicrosoftGraph.Event[] = eventsResponse.value;
        this.setState({events:calendarEvents})
      })
    })
  }

  public render(): React.ReactElement<ICalendarEventsDemoProps> {
    return (
      <div>
        <ul>
          {
            this.state.events.map((itemkey)=>{
              <li key={item.id}>
                {item.subject},
                {item.organizer.emailAddress.name},
                {item.start.dateTime.substr(0,10)},{item.start.dateTime.substr(12,5)},
                {item.end.dateTime.substr(0,10)},{item.end.dateTime.substr(12,5)}
              </li>
            })
          }
        </ul>

        <style>
          {`
          table{
            border:1px solid black;
            backgroud:aqua;
          }
          `}
        </style>
        
        <table>
          <tr>
            <td>Subject</td>
            <td>Organizer name</td>
            <td>Start Date</td>
            <td>Start Time</td>
            <td>End Date</td>
            <td>End Time</td>
          </tr>
          {
            this.state.events.map((item,key)=>{
              <tr>
                <td>{item.subject}</td>
                <td>{item.organizer.emailAddress.name}</td>
                <td>{item.start.dateTime.substr(0,10)}</td>
                <td>{item.start.dateTime.substr(12,5)}</td>
                <td>{item.end.dateTime.substr(0,10)}</td>
                <td>{item.end.dateTime.substr(12,5)}</td>
              </tr>
            })
          }
        </table>
      </div>
    );
  }
}
----------------------------------
Main ts file

import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
  IPropertyPaneConfiguration,
  PropertyPaneTextField
from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';

import * as strings from 'CalendarEventsDemoWebPartStrings';
import CalendarEventsDemo from './components/CalendarEventsDemo';
import { ICalendarEventsDemoProps } from './components/ICalendarEventsDemoProps';

export interface ICalendarEventsDemoWebPartProps {
  descriptionstring;
}

export default class CalendarEventsDemoWebPart extends BaseClientSideWebPart<ICalendarEventsDemoWebPartProps> {

  public render(): void {
    const elementReact.ReactElement<ICalendarEventsDemoProps> = React.createElement(
      CalendarEventsDemo,
      {
        description: this.properties.description,
        context: this.context
      }
    );

    ReactDom.render(elementthis.domElement);
  }

  protected onDispose(): void {
    ReactDom.unmountComponentAtNode(this.domElement);
  }

  protected get dataVersion(): Version {
    return Version.parse('1.0');
  }

  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                })
              ]
            }
          ]
        }
      ]
    };
  }
}
---------------------------------

components folder interface props .ts

import { WebPartContext } from "@microsoft/sp-webpart-base";

export interface ICalendarEventsDemoProps {
  descriptionstring;
  context:WebPartContext
}
------------------------------

state file .ts

import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';

export interface ICalendarEventsDemoState{
    events:MicrosoftGraph.Event[];
}
----------------------------------------

Comments

Popular posts from this blog

Powerapps overcome 2000 item limit from any datasource

First go through delegation concept https://tejasadventure.blogspot.com/2020/05/power-apps-understanding.html In powerapps, we observe by default 500 item limit has set with in the app level for any data source. In order to overcome, we have option to set the limit of 2000 item limit maximum in the app. Now using code, MaximoFacility is my data source name contains 3000 items. ColFacilityDropdown is the collection storing the count of items from data source. We expect to see more than 2000 items. Based on StartWith function filtering the data with respective to the charectors and numbers as mentioned below. Code: Place the below code in a button on select property. Add label with code CountRows(ColFacilityDropdown) ClearCollect(ColFacilityDropdown,Filter(Filter(MaximoFacility, Status = "ACTIVE"), StartsWith( Title, "A" ))); Collect(ColFacilityDropdown,Filter(Filter(MaximoFacility, Status = "ACTIVE"), StartsWith( Title, "B" ))); Collect(ColFacilit...

Power Apps Understanding

https://tejasadventure.blogspot.com/2019/10/power-apps-we-have-1.html https://tejasadventure.blogspot.com/2019/11/power-apps-how-to-configure-using-blank.html   https://tejasadventure.blogspot.com/2019/11/power-apps-field-controls.html Report We can use people picker fields, look up fields  file attachment back next save cancel download the report and this report can be used in other system of powerapp. Add new report button - asks add from existing report Report all item view can design in the way like sharepoint list views for example. we can group it Group AND or Group OR by apply filters Canvas apps: Arrange user experience and interface design allow creativity and business use case guide how the way app wants to look like. Sources can be around 200+ ways. Majorly SharePOint, Power BI From data, From Sample From CDS From blank canvas via appSource Model driven apps: Model driven apps uses power of CDS rapidly to configure your for...

PowerApps multiselect cascading dropdown and save an item to SharePoint List

 I have one scenario as below List 1: Division List has title field List 2: District List has Title and Division Field(LookUp from Division List) List 3: Facility List has Title, District Field(LookUp field from District List) List 4: County List has Title, Facility Field(LookUp field from Facility List) Main List: Spill Report is the main list  Division Field( Look up from Division List) District Field(Look up field from District List) Facility Field(Look up field from Facility List) County Field(Look up field from County List) List Screenshots provided below can be refered for clarification. ----------------------------------------------------------------------------------------------------- PowerApps Canvas Apps In Power Apps Canvas App, We need to first design the app with the 4 respective fields Since those fields are multiselect, then it is to combo box. Generally power apps are not supported for multiselect cascasding dropdown. Refere microsoft documentation, Know Limit...