spfx-40-fantastics icon indicating copy to clipboard operation
spfx-40-fantastics copied to clipboard

simple poll is not submitting the data to survey list

Open dhiru08484 opened this issue 6 years ago • 3 comments

simple poll is not submitting the data to survey list . Please let us know if i need to change anything on code.

dhiru08484 avatar Aug 31 '18 17:08 dhiru08484

I think its caused by SPSurveyService.getListName function. It gets the Title of the survey list and passes its result to SPSurveyService.getItemTypeForListName which produces ListItemEntityTypeFullName of the target survey list. You add that 'ListItemEntityTypeFullName' to item metadata in postVote function. You can NOT post votes because that property is built incorrectly. Thats the story.

Exact problem with getListName was that it assumed it was processing XML and in my case my rest endpoint was returning JSON.

To solve that problem in a more elegant way, I have created another function to fetch ListItemEntityTypeFullName from rest api and replaced that with getListName & getItemTypeForListName in postVote function.

You could fetch that property like this: https://yourtenant.sharepoint.com/_api/Web/Lists(guid'b1893655-f211-44d1-b243-fa8b93bb2a23')?$select=ListItemEntityTypeFullName Becareful if you have special characters in your survey title it may return xml even though you set application/json in your request header.

Also in case of your questions in the survey list have some special characters, I humbly advise you to pass questionInternalName instead of question title to postVote function.

edit: you can also use pnpjs to get ListItemEntityTypeFullName property.

mit4dev avatar Sep 18 '18 08:09 mit4dev

Thank you So much for your advice. I will try your suggestions and let you know the results.

dhiru08484 avatar Sep 21 '18 17:09 dhiru08484

This worked for me.

public postVote(surveyListId: string, question: string, choice: string): Promise { var _this = this; return this.getItemTypeForListName(surveyListId).then((typeListName: string) => { var restUrl: string = this.context.pageContext.web.absoluteUrl; restUrl += "/_api/Web/Lists(guid'"; restUrl += surveyListId; restUrl += "')/items"; var item = { "__metadata": { "type": typeListName }, "Title": "newItemTitle" }; item[question] = choice; var options: ISPHttpClientOptions = { headers: { "odata-version": "3.0", "Accept": "application/json" }, body: JSON.stringify(item), webUrl: this.context.pageContext.web.absoluteUrl }; return _this.context.spHttpClient.post(restUrl, SPHttpClient.configurations.v1, options).then((response: SPHttpClientResponse) => { return response.json().then((responseFormated: any) => { //responseFormated.id if(responseFormated.Id){ return true; }else { return false; }
}); }) as Promise; }) as Promise; }
private getItemTypeForListName(listId: string): Promise { //string { var restUrl: string = this.context.pageContext.web.absoluteUrl; restUrl += "/_api/Web/Lists(guid'"; restUrl += listId; restUrl += "')?$select=ListItemEntityTypeFullName"; var options: ISPHttpClientOptions = { headers: { "odata-version": "3.0", "Accept": "application/json" } }; return this.context.spHttpClient.get(restUrl, SPHttpClient.configurations.v1, options).then((response: SPHttpClientResponse) => { return response.json().then((responseFormated: any) => {
return responseFormated.ListItemEntityTypeFullName; }); });
//return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
}

got2ski avatar Sep 01 '20 15:09 got2ski