spHTTPClient.POST not working for Taxnomy Field on SPFX WebPart with no error

Asma Rhouma 41 Reputation points
2022-09-27T08:11:32.247+00:00

Hi All,

I need to update a ListItem from my spfx.
****Solution 1** : uses pnpjs**

const i = await list.items.getById(itemId);  
      // execute the update call  
      i.validateUpdateListItem(updateVal)  

==> error "throttle limit exceeded" as the the list have more than 12 MetaDataField (Taxonomy).

Solution 2 : use the REST API

const restAPI = webAbsoluteUrl +  `/_api/web/lists/getByTitle('${listTitle}')/items(${itemId})?select=Title,Document_x0020_Language`;  
        return context.spHttpClient  
          .post(restAPI, SPHttpClient.configurations.v1, {  
            headers: {  
              "content-type": "application/json;odata.metadata=none",  
              "if-match": _etag,  
              "x-http-method": "MERGE",  
            },  
            body: JSON.stringify({  
              /*  __metadata: {  
                type: "SP.Data.TAX_xxxxxItem",  
              },*/  
              Title: "Update_Title_ForTest_10",  
              Document_x0020_Language: {  
                // __metadata: { type: "SP.Taxonomy.TaxonomyFieldValue" },  
                TermGuid: "311c9e5f-d7df-4d0d-9c78-05d3e8100d29",  
                WssId: -1,  
                Label: "English",  
              },  

            }),  
          })  
          .then((response) => {  
            if (response.status === 204) {  
              alert("successfully Updated");  
            } else {  
              console.log(response.statusText);  
            }  
          });  

==>using this code, the response is always succeeded and the Title Field is updated but nothing is changed for the taxonomy Field
I think this is is because the number of lookup fields is more than 12( the limit) but I can't change this structure as the native Microosft Editform is working

Any help would be greatly appreciated!

Thanks

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,597 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2022-10-10T07:34:42.707+00:00

    Hi @Asma Rhouma
    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [spHTTPClient.POST not working for Taxnomy Field on SPFX WebPart with no error]

    Issue Symptom:
    Unable to update taxonomy Field by spfx

    Current status:
    User the @pnp/sp/batching

      import "@pnp/sp/batching";  
              
       const [batchedSP, execute] = sp.batched();  
        sp.web.lists  
              .getByTitle(listTitle)  
              .items.getById(itemId)  
              .validateUpdateListItem([{Document_x0020_Language : "English|311c9e5f-d7df-4d0d-9c78-05d3e8100d29;"},{Document_x0020_Status : "DRAFT|c25c9e5f-d77f-4a1d-7c79-05d3e8100d21;"},{Title:  "Test Update"}]);  
              
            await execute();  
    

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2022-09-28T08:11:26.997+00:00

    Hi @Asma Rhouma
    Per my test, you can refer to following code to update multi value taxonomyfield

    const listWeWantToUpdateMulti = pnp.sp.web.lists.getByTitle('ListWeWantToUpdateTitle');  
      
    // Example of a term string.  
    // You can also use the Id returned in rest calls instead of the full label  
    const termString = '-1;#SomeTerm|02ee415b-99c4-448b-8727-7daa2a4a281;#-1;# SomeOtherTerm |0e2f40d9-09ac-406e-b102-630e8dadade6;';  
      
    // If the name of your taxonomy field is SomeMultiValueTaxonomyField, the name of your note field will be SomeMultiValueTaxonomyField_0  
    const multiTermNoteFieldName = 'SomeMultiValueTaxonomyField_0';  
      
    listWeWantToUpdateMulti.getListItemEntityTypeFullName()  
        .then((entityTypeFullName) => {  
            listWeWantToUpdateMulti.fields.getByTitle(multiTermNoteFieldName).get()  
                .then((taxNoteField) => {  
                    const multiTermNoteField = taxNoteField.InternalName;  
                    const updateObject = {  
                        Title: 'Item title', // Only included as an example  
                    };  
                    updateObject[multiTermNoteField] = termString;  
      
                    listWeWantToUpdateMulti.items.add(updateObject, entityTypeFullName)  
                        .then((updateResult) => {  
                            console.dir(updateResult);  
                        })  
                        .catch((updateError) => {  
                            console.dir(updateError);  
                        });  
                });  
        });  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. Asma Rhouma 41 Reputation points
    2022-10-06T13:30:56.47+00:00

    @RaytheonXie_MSFT , Thanks for your Help

    0 comments No comments

  3. Asma Rhouma 41 Reputation points
    2022-10-06T13:32:08.99+00:00

    Hi @RaytheonXie_MSFT ,
    I tested the solution that you posted but it doesn't work and getListItemEntityTypeFullName was not recognized. but I tested the code without it and it doesn't work.
    The good News is that I found the solution : I used the batching https://pnp.github.io/pnpjs/sp/items/

    So the solution is :

     import "@pnp/sp/batching";  
          
      const [batchedSP, execute] = sp.batched();  
       sp.web.lists  
             .getByTitle(listTitle)  
             .items.getById(itemId)  
             .validateUpdateListItem([{Document_x0020_Language : "English|311c9e5f-d7df-4d0d-9c78-05d3e8100d29;"},{Document_x0020_Status : "DRAFT|c25c9e5f-d77f-4a1d-7c79-05d3e8100d21;"},{Title:  "Test Update"}]);  
          
           await execute();  
    
    0 comments No comments