MS Project Online API to read calendar information

Copper Contributor

Hi,

I'm trying to retrieve calendar details from MS Project Online using a calendar ID.

I'm currently using this endpoint to get the calendar information:

https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')

However, this endpoint does not provide information about holidays and working days in a week.

Is there an API that can fetch all this data?

Thank you.

 

8 Replies

Hi @Aravind470 

 

To get detailed calendar info from MS Project Online, you'll need to hit a couple of different endpoints. The one you're using gives you the basics, but to get the full picture, like holidays and working days, you need to dig a bit deeper.

 

Here's how you can do it:

 

1. Retrieve Calendar Details:
You are already using this endpoint to get basic calendar details:
https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')

 

2. Retrieve Calendar Work Weeks:
To get information about the work weeks defined in the calendar, you can use:
https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')/WorkWeeks

 

3. Retrieve Calendar Exceptions:
To get the exceptions (holidays, non-working days) for the calendar, use:
https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')/Exceptions

 

This will give you a complete look at the calendar, including work weeks and any exceptions like holidays. Easy peasy! :cool:

 

Regards, Tony

Hi @avillarruel ,

 

I attempted to retrieve the calendar details from Project Online using the following endpoints:

  1. https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')/WorkWeeks
  2. https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')/Exceptions

However, both requests resulted in a 404 Not Found error.

Can you suggest any alternative endpoints or provide guidance on how to correctly fetch the calendar details, including holidays and working days in a week, from Project Online?

I'm using SharePoint Online credentials for authentication.

I'm uploading the image below

Aravind470_0-1717135928219.png

Thank you.

 

@Aravind470 

 

You're getting a 404 error, so let's double-check everything.

 

Check Your URL: Ensure the URL format is correct and consistent:

  • Verify the <sitecollection> and <site> values are correct.
  • Ensure the calendarid is correctly formatted and exists.
  • It should look like this: https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')

Use the Correct Endpoints: Sometimes API versions and structures change. Verify the endpoint paths and usage.

Authentication: Ensure your SharePoint Online credentials are correctly configured and have the necessary permissions.

Alternative Endpoints and Approaches:  You might need to use different or additional endpoints to retrieve the detailed calendar information.

 

1. Get All Calendars:
Start by fetching all calendars to ensure you're using a valid calendar ID.
GET https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars

2. Retrieve Calendar Details:
Use the following to get detailed information about a specific calendar:
GET https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')

3. Work Weeks and Exceptions:
Sometimes the API paths can be tricky. Try these:

Alternative Work Weeks Endpoint:
GET https://<sitecollection>/<site>/pwa/_api/ProjectData/Calendars(guid'calendarid')/WorkWeeks

Alternative Exceptions Endpoint:
GET https://<sitecollection>/<site>/pwa/_api/ProjectData/Calendars(guid'calendarid')/Exceptions

 

Example Code (Updated Fetch Requests)
Here's an updated version of the Fetch requests considering possible adjustments:

 

const siteUrl = 'https://<sitecollection>/<site>';
const calendarId = 'calendarid'; // Ensure this is a valid GUID
const headers = {
'Accept': 'application/json;odata=verbose',
'Authorization': 'Bearer <your_access_token>' // Ensure your access token is valid
};

// Fetch all calendars to verify the calendar ID
fetch(`${siteUrl}/pwa/_api/ProjectServer/Calendars`, { headers })
.then(response => response.json())
.then(data => console.log('All Calendars:', data));

// Fetch basic calendar details
fetch(`${siteUrl}/pwa/_api/ProjectServer/Calendars('${calendarId}')`, { headers })
.then(response => response.json())
.then(data => console.log('Calendar Details:', data));

// Fetch work weeks (alternative endpoint)
fetch(`${siteUrl}/pwa/_api/ProjectData/Calendars(guid'${calendarId}')/WorkWeeks`, { headers })
.then(response => response.json())
.then(data => console.log('Work Weeks:', data));

// Fetch exceptions (alternative endpoint)
fetch(`${siteUrl}/pwa/_api/ProjectData/Calendars(guid'${calendarId}')/Exceptions`, { headers })
.then(response => response.json())
.then(data => console.log('Exceptions:', data));


Permissions. Ensure that:

  • Your account has the necessary permissions to access Project Online APIs.
  • The access token or credentials used are valid and have the required scopes.

Debugging Tips

  • Verify Calendar ID: Make sure it exists.
  • Check Permissions: Your account should have the right access.
  • Use Postman: Test the API calls manually to see if you get more detailed error messages.

 

Hopefully, this helps! If you still run into issues, let me know!

 

Regards, Tony

Hi Avillarul,
I tried the following endpoints, but I am still getting a 404 error:
1. GET https://<sitecollection>/<site>/pwa/_api/ProjectData/Calendars(guid'calendarid')/WorkWeeks
2. GET https://<sitecollection>/<site>/pwa/_api/ProjectData/Calendars(guid'calendarid')/Exceptions

The following endpoints are working for me:
1. GET https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars
2. GET https://<sitecollection>/<site>/pwa/_api/ProjectServer/Calendars('calendarid')

However, I need to retrieve the workweeks, working time, and exceptions for a calendar. I have confirmed that the credentials are correct, as the API for getting calendars is working. Is there any way to retrieve the working time and workweeks? Any assistance on this would be greatly appreciated

@Aravind470 

 

Alright, let's try another approach since those endpoints aren't working for you.

 

Given that you can successfully retrieve the list of calendars and details for a specific calendar, let's explore different ways to get the detailed information you need about workweeks and exceptions.

 

Alternative Approach: Use the Project Server Client-Side Object Model (CSOM)

The CSOM API might offer the functionality you're looking for. You can use it via REST or by scripting in PowerShell.

 

Using REST API with CSOM

Get Work Weeks and Exceptions: Unfortunately, the specific endpoints for workweeks and exceptions you tried aren't available in the REST API directly. However, you can use the CSOM API.

 

Example: PowerShell Script Using CSOM

Here's an example using PowerShell to retrieve the information. This script assumes you have the necessary permissions and that you have installed the Microsoft.SharePoint.Client and Microsoft.ProjectServer.Client assemblies.

 

Install the required modules: First, ensure you have the necessary modules installed:
Install-Module -Name Microsoft.Online.SharePoint.PowerShell

 

PowerShell Script:


# Load necessary assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.ProjectServer.Client.dll"

# Connect to SharePoint Online
$siteUrl = "https://<sitecollection>/<site>"
$username = "email address removed for privacy reasons"
$password = Read-Host -Prompt "Enter your password" -AsSecureString

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)

# Get Project Context
$projectContext = New-Object Microsoft.ProjectServer.Client.ProjectContext($siteUrl)
$projectContext.Credentials = $ctx.Credentials

# Load the calendar
$calendarId = [Guid]::Parse("calendarid") # Replace with your calendar ID
$calendar = $projectContext.EnterpriseCalendars.GetById($calendarId)
$projectContext.Load($calendar)
$projectContext.ExecuteQuery()

# Get Work Weeks and Exceptions
$workWeeks = $calendar.WorkWeeks
$exceptions = $calendar.Exceptions

$projectContext.Load($workWeeks)
$projectContext.Load($exceptions)
$projectContext.ExecuteQuery()

# Display Work Weeks
foreach ($workWeek in $workWeeks) {
Write-Output "Work Week: $($workWeek.Name)"
}

# Display Exceptions
foreach ($exception in $exceptions) {
Write-Output "Exception: $($exception.Name)"
}

 

Using REST API via PowerShell Script

 

If you need to stick with REST API and you don't have direct endpoints, you might need to:

  1. Use PowerShell or CSOM: Direct access to calendars' workweeks and exceptions might require client-side scripting.
  2. Combine Data: If you retrieve the general calendar data and workweek/exceptions data through different means, you might need to combine and process this information locally.

 

In summary, direct REST endpoints for workweeks and exceptions might not be available. Using the CSOM API via PowerShell or client-side scripting is a solid alternative to get the detailed information you need. This approach should provide you with the ability to retrieve workweeks and exceptions programmatically.

If you're comfortable with PowerShell, give the script a try. If not, consider using CSOM with another client-side language you prefer.

 

Let me know if you need more help! And good luck!, Tony

Hi Avillarruel,

I tried using CSOM , but I'm still only receiving basic calendar information. I am not getting details on work weeks and work time.

Thanks.

Ouch! It looks like you are running into some permission or config issues...
Alright, I'll look into it. Thanks a lot for your help!