Fill out of a SharePoint Survey List from PowerApps

PowerApps and SharePoint are very well integrated and you can quickly build some great PowerApp forms binded to SharePoint list in just a couple of minute.

Unfortunatly there are some restrictions like for example you cannot bind a survey list type to PowerApps.

But hey it is your lucky day I will guide you through how you can build a PowerApps application that dynamically retrieves all the surveys in a SharePoint site and lets you respond to a survey.

The PowerApps application will have two screens, one to display all the available surveys and one to fill the survey.

In order to retrieve the available survey we will have a Flow named GetAvailableSurveys that is triggered from PowerApps that queries the SharePoint list API. For a fact a survey type list has a BaseTemplate equals to 102 so the REST API call is just

URL_OF_YOUR_SHAREPOINT_SITE/_api/web/lists?$filter=BaseTemplate eq 102

 

This is the Flow corresponding block

Now we just have to parse the json and reply to the PowerApp. We will use the Response action block in Flow

Here the body is set to the node "value" of the result from the GetAvailableSurveys block. To make things easier I set this JSON body schema

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "odata.type": {
                "type": "string"
            },
            "odata.id": {
                "type": "string"
            },
            "Id": {
                "type": "string"
            },
            "ListItemEntityTypeFullName": {
                "type": "string"
            },
            "StaticName": {
                "type": "string"
            },
            "Title": {
                "type": "string"
            }
        },
        "required": [
            "Id"
        ]
    }
}

Because I don't want to send all the properties back to my PowerApp.

Great we have a Flow that retrieves all the Survey of a particular SharePoint site. Now we have to create another Flow called Get Polls that takes in parameter the name of a survey and respond with all the questions of that survey. Again the Flow will query the SharePoint REST API to get all the questions. The URI is

_api/web/lists/getbytitle('NAME_OF_THE_SURVEY')/fields?$filter=(CanBeDeleted eq true)

So the corresponding Flow block is

Now again we just have to parse the json and reply to the PowerApp. We will use the Response action block in Flow

Here the body is set to the node "Results" of the result from the GetQuestionsFromSurvey block. To make things easier I set this JSON body schema

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "odata.type": {
                "type": "string"
            },
            "odata.id": {
                "type": "string"
            },
            "odata.editLink": {
                "type": "string"
            },
            "AutoIndexed": {
                "type": "boolean"
            },
            "CanBeDeleted": {
                "type": "boolean"
            },
            "ClientSideComponentId": {
                "type": "string"
            },
            "Description": {
                "type": "string"
            },
            "Direction": {
                "type": "string"
            },
            "EnforceUniqueValues": {
                "type": "boolean"
            },
            "EntityPropertyName": {
                "type": "string"
            },
            "Filterable": {
                "type": "boolean"
            },
            "FromBaseType": {
                "type": "boolean"
            },
            "Group": {
                "type": "string"
            },
            "Hidden": {
                "type": "boolean"
            },
            "Id": {
                "type": "string"
            },
            "Indexed": {
                "type": "boolean"
            },
            "InternalName": {
                "type": "string"
            },
            "JSLink": {
                "type": "string"
            },
            "PinnedToFiltersPane": {
                "type": "boolean"
            },
            "ReadOnlyField": {
                "type": "boolean"
            },
            "Required": {
                "type": "boolean"
            },
            "SchemaXml": {
                "type": "string"
            },
            "Scope": {
                "type": "string"
            },
            "Sealed": {
                "type": "boolean"
            },
            "ShowInFiltersPane": {
                "type": "integer"
            },
            "Sortable": {
                "type": "boolean"
            },
            "StaticName": {
                "type": "string"
            },
            "Title": {
                "type": "string"
            },
            "FieldTypeKind": {
                "type": "integer"
            },
            "TypeAsString": {
                "type": "string"
            },
            "TypeDisplayName": {
                "type": "string"
            },
            "TypeShortDescription": {
                "type": "string"
            },
            "FillInChoice": {
                "type": "boolean"
            },
            "Choices": {
                "type": "object",
                "properties": {
                    "__metadata": {
                        "type": "object",
                        "properties": {
                            "type": {
                                "type": "string"
                            }
                        }
                    },
                    "results": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    }
                }
            },
            "EditFormat": {
                "type": "integer"
            },
            "AllowHyperlink": {
                "type": "boolean"
            },
            "AppendOnly": {
                "type": "boolean"
            },
            "NumberOfLines": {
                "type": "integer"
            },
            "RestrictedMode": {
                "type": "boolean"
            },
            "RichText": {
                "type": "boolean"
            },
            "UnlimitedLengthInDocumentLibrary": {
                "type": "boolean"
            },
            "WikiLinking": {
                "type": "boolean"
            }
        },
        "required": [
            "Id"
        ]
    }
}

 Now we will need a Flow called Submit Poll to submit the answers. To do so we will again use the SharePoint REST API to post a json in order to create an entry in the survey.

This is the corresponding Flow block

Basically to create an item in any SharePoint list using the REST API we have to do a post request to the "/items" URI of the list and send a json with the values we want to insert and the __metadata type corresponding to this particular list with is always of the form "SP.Data.LISTNAMEListItem".
For the different values it is for the most common types of columns like text, multitext, choice, numbers, etc it is of the form "ColumnInternalName":"VALUE". So basically the SurveyAnswers variable will hold all the column, value pairs we want to insert in the SharePoint list. This variable will be provided to the Flow by the PowerApps application.

Let's have a look to the PowerApps application.

The first screen the Home Page holds a gallery binded to the GetAvailableSurveys Flow that will display the Title of all the available surveys.

On the OnStart of the application we call the GetAvailablesurveys Flow and set to to a collection variable _surveyList
ClearCollect(_surveyList,GetAvailableSurveys.Run());
The Items property of the gallery is set to _surveyList. The gallery has just one label to display the item Title.

On the selection of an item we store the selected survey in the _currentSurvey variable, call the GetPolls Flow with the selected survey title as parameter and navigate to the SurveyPage screen.

Now the survey screen has a couple of labels to display the title of the survey, a message at the end of the survey and some debug information. But the most important components are the gallery that will display every question and the submit button