pythonazureapimicrosoft-graph-apimicrosoft-forms

Call MS Graph API to collect Online MS Form responses


Is there a solution to collect responses from a shared online MS Form via MS Graph API?

I used the MS Graph API for other purposes i.e. to have access to personal/shared files in OneDrive.
I couldn't find a straight forward solution to have access to the online forms. There are some requests from Microsoft and they responded the option is on the backlog. I was wondering is there an alternative solution to have access to a shared online MS Form via API?


Solution

  • Unfortunately, there is no graph api to do the same.

    There are two alternatives I can think of :

    Option 1 :

    You could maintain your own data source of the form response by using LogicAPP or using Power Automate (Formerly Called as MS Flow)

    enter image description here

    This trigger will be invoked whenever there is a new response recorded for the form along with the data that has been filled out in the response.

    With that you could export this response in a CSV File stored in OneDrive,Sharepoint, Azure storage etc. (appending to already existing response). So that you consume this CSV over the Graph API as and when required.

    or add the responses to a Sharepoint Online List as List items. You could get the list items through Graph API.

    https://learn.microsoft.com/en-us/graph/api/resources/listitem?view=graph-rest-1.0

    Option 2 :

    Note : This approach might be suitable if you performing an operation in a non-web application.

    Since you are trying to achieve your goal through Python. I thought you could use Selenium for WebBrowser.

    The API that fetches the Answer responses is below :

    GET https://forms.office.com/formapi/api/<tenantid>/users/<userid>/light/forms('<formid>')/responses?$expand=comments&$top=7&$skip=0 
    

    Sample response for the above request :

    enter image description here

    The catch here is that, there is no means to obtain the access token for the above API at this point of time. (As far as I have researched)

    So workaround, that I have done is to make use of the Selenium.

    For instance - use the ChromeDriver and Selenium library to automate the below process :

    You could refer this article https://www.browserstack.com/guide/python-selenium-to-run-web-automation-test to understand how we can do the browser automation using python-chrome.

    Step 1 : Automate the login into https://forms.office.com. This step will create the necessary cookie for the session to access the forms api

    Step 2: Now hit the api in same session

    GET https://forms.office.com/formapi/api/<tenantid>/users/<userid>/light/forms('<formid>')/responses?$expand=comments&$top=7&$skip=0 
    

    and you will get the output as the response : enter image description here

    Which can be assessed & used subsequently for processing.