Forum Discussion

Aravind470's avatar
Aravind470
Copper Contributor
May 30, 2024

MS Project Online API to read calendar information

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.

 

  • avillarruel's avatar
    avillarruel
    Copper Contributor

    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

    • Aravind470's avatar
      Aravind470
      Copper Contributor

      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

      Thank you.

       

      • avillarruel's avatar
        avillarruel
        Copper Contributor

        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

Resources