Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExecuteAsync(throwOnError:false) is ignored #741

Closed
kohsel opened this issue Feb 1, 2022 · 8 comments
Closed

ExecuteAsync(throwOnError:false) is ignored #741

kohsel opened this issue Feb 1, 2022 · 8 comments
Assignees
Labels
area: model 📐 Related to the core SDK models question Further information is requested

Comments

@kohsel
Copy link

kohsel commented Feb 1, 2022

Hello
I am rather new to pnp core SDK, so please bear with me.

I have a SharePoint list where I have enabled EnforceUniqueValues on a field.

If I try to save a duplicate in a batch execution the context.ExecuteAsync(false) is ignoring the throwOnError = false and throws a SharePointRestServiceException instead of returning a List

It looks like it is in the PnP.Core.Model.SharePoint.ListItem constructor:

// In some cases SharePoint will return HTTP 200 indicating the list item was added ok, but one or more fields could 
// not be added which is indicated via the HasException property on the field. If so then throw an error.
if (field.TryGetProperty("HasException", out JsonElement hasExceptionProperty) && hasExceptionProperty.GetBoolean() == true)
{
throw new SharePointRestServiceException(string.Format(PnPCoreResources.Exception_ListItemAdd_WrongInternalFieldName, fieldName));
}
@jansenbe jansenbe self-assigned this Feb 1, 2022
@jansenbe jansenbe added area: model 📐 Related to the core SDK models question Further information is requested labels Feb 1, 2022
jansenbe added a commit that referenced this issue Feb 1, 2022
@jansenbe
Copy link
Contributor

jansenbe commented Feb 1, 2022

Thanks for reporting this @kohsel , this was indeed an issue. I've pushed a fix for this, please try again with tomorrow's nightly build and report back.

@kohsel
Copy link
Author

kohsel commented Feb 3, 2022

Hi again

I've included the code thats not behaving as described in the PnP Core documentation:

private async Task CommitBatch(string batchDescription, Batch batch) { logger.LogInformation("Committing batch operation - {batchdescription}", batchDescription); try { var batchResult = await context.ExecuteAsync(batch, false); batchResult.ForEach(r => logger.LogInformation("StatusCode: {statuscode}", r.StatusCode)); } catch (SharePointRestServiceException spEx) { var error = spEx.Error as SharePointRestError; logger.LogError("{@error}", error); } catch (Exception e) { logger.LogError(e, "{errorMessage}", e.Message); throw new SPDataServiceBatchCommitException(e.Message, e); } }
After installing the NuGet package 1.5.47 I ran the application again triggering a "uniqueness" exception in the list and the ExeceuteAsync method still throws an SharePointRestServiceException with message:

There was an exception while writing field dgit_SAPUID. Verify you're using the correct InternalName value for the field you want to write to.

Please let me know if you need further information.

Best regards
Lars Kohsel

@jansenbe
Copy link
Contributor

jansenbe commented Feb 3, 2022

Let me verify things @kohsel , in your scenario you're using a named batch while I tested with the default implicit batch. If you could step over the code in your app (see https://pnp.github.io/pnpcore/using-the-sdk/basics-debug.html) that would also help here.

@jansenbe
Copy link
Contributor

jansenbe commented Feb 3, 2022

@kohsel : nevermind, using a custom batch I can simulate the issue.

jansenbe added a commit that referenced this issue Feb 3, 2022
@jansenbe
Copy link
Contributor

jansenbe commented Feb 3, 2022

@kohsel : an improved fix was committed, this one also correctly handles the case where one uses a named batch. Can you test again with the next nightly (so tomorrow) and provide feedback.

@jansenbe
Copy link
Contributor

@kohsel : think this now works given there's no further feedback. If not then please re-open this issue or create a new one.

@kohsel
Copy link
Author

kohsel commented Feb 11, 2022

@jansenbe with the latest nightly-build I no longer get the exception. However I am a bit surprised on the batchResult returned.

I submit a batch of 11 creates where one is a violating duplicate. In the batchResult I get 1 item, which I have dumped below. Apparently only the error results are returned, but I find it strange that the StatusCode is OK. Is this because the statusCode is actually the HTTP status code? Then I would imagine that the variable should be a the batchresult collection and not on the Items, since there is only one HTTP request - or is there more?

I also find it a bit strange that in the API response object the ErrorCode is 0, but the error message clearly states that the action did not succeed:

{
"ErrorCode": 0,
"ErrorMessage": "This value already exists in the list.",
"FieldName": "dgit_SAPUID",
"FieldValue": "TLI_298590_DE_E5",
"HasException": true,
"ItemId": 0
},

but I guess this is the raw response from the REST api?

BatchResult {
StatusCode: OK,
Error: SharePointRestError {
ServerErrorCode: 0,
HttpResponseCode: 200,
Message: "There was an exception while writing field dgit_SAPUID. Verify you're using the correct InternalName value for the field you want to write to.",
ClientRequestId: null,
Code: null,
Type: SharePointRestServiceError,
PnPCorrelationId: 00000000-0000-0000-0000-000000000000,
AdditionalData: null },
ApiResponse: "[{"ErrorCode":0,"ErrorMessage":"This value already exists in the list.","FieldName":"dgit_SAPUID","FieldValue":"TLI_298590_DE_E5","HasException":true,"ItemId":0},{"ErrorCode":0,"ErrorMessage":null,"FieldName":"Title","FieldValue":"RGFSLTest Document","HasException":false,"ItemId":0},{"ErrorCode":0,"ErrorMessage":null,"FieldName":"dgit_DeletionIndicator","FieldValue":"0","HasException":false,"ItemId":0},{"ErrorCode":0,"ErrorMessage":null,"FieldName":"dgitDeltaTime","FieldValue":"2020-06-15 13:13:02","HasException":false,"ItemId":0},{"ErrorCode":0,"ErrorMessage":null,"FieldName":"dgit_ReleaseStatus","FieldValue":"OB","HasException":false,"ItemId":0},{"ErrorCode":0,"ErrorMessage":null,"FieldName":"dgit_TLI_Group","FieldValue":"VACON 100-RGFSL","HasException":false,"ItemId":0},{"ErrorCode":0,"ErrorMessage":null,"FieldName":"dgit_TLI_Series","FieldValue":"V100 xps-346","HasException":false,"ItemId":0},{"ErrorCode":0,"ErrorMessage":null,"FieldName":"dgit_TLI_ManualNo","FieldValue":"MG3750","HasException":false,"ItemId":0},{"ErrorCode":0,"ErrorMessage":null,"FieldName":"Id","FieldValue":"0","HasException":false,"ItemId":0}]",
ApiType: "SPORest",
ApiRequest: "https://delfinen2.sharepoint.com/sites/DDSdemo/_api/Web/Lists(guid'24ebf23d-677d-40e9-a28d-bf7be3a20f9b')/AddValidateUpdateItemUsingPath",
ApiMethod: HttpMethod { Method: "POST" },
ApiBody: "{
"listItemCreateInfo": {
"FolderPath": {
"DecodedUrl": "https://delfinen2.sharepoint.com/sites/DDSdemo/lists/TLIOriginal\"
},
"UnderlyingObjectType": 0
},
"bNewDocumentUpdate": false,
"formValues": [
{
"FieldName": "dgit_SAPUID",
"FieldValue": "TLI_298590_DE_E5"
},
{
"FieldName": "Title",
"FieldValue": "RGFSLTest Document"
},
{
"FieldName": "dgit_DeletionIndicator",
"FieldValue": "0"
},
{
"FieldName": "dgitDeltaTime",
"FieldValue": "2020-06-15 13:13:02"
},
{
"FieldName": "dgit_ReleaseStatus",
"FieldValue": "OB"
},
{
"FieldName": "dgit_TLI_Group",
"FieldValue": "VACON 100-RGFSL"
},
{
"FieldName": "dgit_TLI_Series",
"FieldValue": "V100 xps-346"
},
{
"FieldName": "dgit_TLI_ManualNo",
"FieldValue": "MG3750"
}
]
}" }

@jansenbe
Copy link
Contributor

@kohsel : that's correct, we return what's provided by the REST call

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: model 📐 Related to the core SDK models question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants