OlivierCC / spfx-40-fantastics

This package is a sample kit of Client Side Web Parts built on the SharePoint Framework SPFx. You can find here different kind of high visual web parts as carousel, images galleries, animations, map, editors, etc.
MIT License
408 stars 279 forks source link

simple poll is not submitting the data to survey list #83

Open dhiru08484 opened 5 years ago

dhiru08484 commented 5 years ago

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

mit4dev commented 5 years ago

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.

dhiru08484 commented 5 years ago

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

got2ski commented 3 years ago

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";
}