Forum Discussion
MS Project Online API to read calendar information
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
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
- avillarruelJun 03, 2024Copper Contributor
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.PowerShellPowerShell 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:
- Use PowerShell or CSOM: Direct access to calendars' workweeks and exceptions might require client-side scripting.
- 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
- Aravind470Jun 05, 2024Copper Contributor
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.- avillarruelJun 05, 2024Copper ContributorOuch! It looks like you are running into some permission or config issues...