In a previous post I wrote, I showed you how to update a taxonomy field using REST even when Microsoft says you must use the CSOM/JSOM to update these fields! Well I can't take "No" for an answer... Somewhere in that post I told everyone that you could not use that method to update a multi-value taxonomy field. Well, I have some good news! There is a way to update multi-value taxonomy fields using REST, albeit its a bit confusing and complicated. However, if you enjoy using the REST API over the client object model... I think you will enjoy this!
As many of us know, when you add a taxonomy field to a list in SharePoint, you are really adding two fields to your list. The first field being the visible field you see and edit, while another hidden field is added which only holds the values for that taxonomy fields. Lets say you added a taxonomy field called 'asdf' (because that was any easy string of letters to type on my left hand) to a list, you will also be adding a note field called 'asdf_0' to you list.
In my previous post, I was using a 'metadata object' to update a taxonomy field. If you thought you could do the same thing on a multi-value field, you'd be mistaken. You'll come up with an assortment of errors saying that SharePoint cannot de-serialize the object you are trying to pass to the server. Luckily, when there is a will, there is a way! I was frustrated for days trying to figure out how I could make this work because I'd rather use REST over JSOM. Then it finally hit me! What if there was a way to update a multi-value taxonomy field without actually updating the data on the field itself?? Instead, what would happen if we tried to update the hidden note field 'asdf_0' with a value... would it then update its associated visible field? I'll spare the suspense, of course it works!
The difficult part of doing this is really trying to figure out the association between the hidden field and its visible counterpart. Take a look at the object below of my hidden note field value.
One would hope you could somehow relate the hidden field 'asdf_0' with the visible 'asdf' field by a property on the object. However, they do not provide (at least I cannot tell) an association between the two fields except by the title of the field! It should be pretty self-explanatory that you could take the title of your taxonomy field and append '_0' to retrieve the hidden field. The real problem however is the internal name for this field... it reads 'pdb5e75.....". What are we supposed to do with this? How would any developer know the actual value of this field in order to update it?? We don't. Therein lies problem.
In my use case it was pretty easy because I was creating a tool to upload documents into SharePoint and I had to pull back all the fields on the list. Thus, when I was looping through all the fields and I came across a multi-value taxonomy field denoted by the 'AllowMultipleValues' property, I would then need to select the associated hidden field using the Title + '_0', and grabbing the associated hidden fields internal name.
If you haven't read my last post, I suggest you do as this next piece of code is updated to show the REST call for the multi-value taxonomy field.
Recap: In order to update a multi-value taxonomy field. You must find its associated hidden note field, denoted by the Title + '_0'. When you do this, you will need to grab the hidden fields internal name which starts with 'pdb...'. This is the field that we are going to update using REST!