May 30 2024 05:23 AM
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.
May 30 2024 10:23 AM - edited Jun 30 2024 04:44 AM
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!
Regards, Tony
May 30 2024 11:11 PM
Hi @avillarruel ,
I attempted to retrieve the calendar details from Project Online using the following endpoints:
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
Thank you.
May 31 2024 05:09 AM
You're getting a 404 error, so let's double-check everything.
Check Your URL: Ensure the URL format is correct and consistent:
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:
Debugging Tips
Hopefully, this helps! If you still run into issues, let me know!
Regards, Tony
Jun 03 2024 03:21 AM
Jun 03 2024 05:51 AM
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:
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
Jun 04 2024 10:02 PM - edited Jun 04 2024 10:11 PM
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.
Jun 05 2024 05:59 AM
Jun 05 2024 09:30 PM