pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
763 stars 304 forks source link

Get recurring events with paging throws an error that query string parameters StartDateTime and EndDateTime are required. #3136

Open bhoomesh-spe opened 1 month ago

bhoomesh-spe commented 1 month ago

What version of PnPjs library you are using

3.x

Minor Version Number

3.15.0

Target environment

SharePoint Framework

Additional environment details

SharePoint Online, SPFx React

Question/Request

Hello Everyone,

I have implemented the below code to get the current user outlook event using PnP Graph API in the SPFx React project.

export const getCalendarsData = async () => {
  const graph: GraphFI = getGraph();
  try {
    const events = await graph.me.events.paged();
    return events;
  } catch (ex) {
    throw ex;
  }
}

Currently I got the current logged in user's outlook event as expected.

But the event start time and end time is in the UTC time zone. I will need the same in the current user local time zone.

I have checked and found that I need to add something like the below in my PnP Graph API call to get the current user events from Outlook.

"Prefer":outlook.timezone="Pacific Standard Time"

How can I pass the Prefer to my PnP Graph API call?

Can anyone help with the same?

Thanks

juliemturner commented 1 month ago

Did you try: https://pnp.github.io/pnpjs/queryable/behaviors/#injectheaders

bhoomesh-spe commented 1 month ago

@juliemturner - Thanks for the response.

I have implemented as you had suggested and it is working as expected. Thanks for the same.

Now, I have faced one issue when I tried to get recurring events it threw me the below error. This request requires a time window specified by the query string parameters StartDateTime and EndDateTime.

Please check the code snippet for more clarification.

export const getAllRecurringEventInstances = async (currentUserMailBoxTimeZone, event) => {
    const graph: GraphFI = getGraph();
    const allEvents = [];
    const startDateTime = event?.recurrence?.range?.startDate + "T" + event?.start.dateTime.split('T')[1];
    const endDateTime = event?.recurrence?.range?.endDate + "T" + event?.end.dateTime.split('T')[1];
    let events = await graph.me.events.getById(event.id).instances(startDateTime, endDateTime).using(InjectHeaders({
        'Prefer': 'outlook.timezone="' + currentUserMailBoxTimeZone + '"'
    })).top(999).paged();
    allEvents.push(...events.value);

    while (events.hasNext) {
        events = await events.next();
        allEvents.push(...events.value);
    }

    return allEvents;
};

Can you please help me on the same?

Thanks

patrick-rodgers commented 1 month ago

@bhoomesh-spe - can you do a quick test without the paged() and see if it works as expected?

This looks like we are stripping those params when we create the paged collection.