I am trying to prepopulate a managed metadata field in a NewForm of my list. I don't want to save the value as it will be used as a suggestion passed in through URL a parameter that the end user is free to change.
So far I have been able to set the value of the textbox. I am having trouble verifying the string to make the form pick it up as an actual value.
I am setting the initial value using:
document.querySelector(".ms-taxonomy-writeableregion").innerHTML = "MyValue";
This appears correctly on the form but doesn't validate the value and do the full steps unless you click into the textbox.
If I try and save the form without clicking into the textbox to verify the value it will save with no error, but the managed metadata field is blank.
Does anyone have any experience with setting this value in the textbox via JavaScript?
EDIT:
I have found out a few things but still no solution.
It looks like after a selection is made from the drop down or on blur of the managed metadata field a call is made at <sitecollection>/_vti_bin/TaxonomyInternalService.json/GetMatches. I have looked into this a bit, but haven't found anything worthwhile. One of the few resources I found was Marc mentioning the potential use of it.
I tried going a different route too, like Dylan mentioned. I wasn't able to have any luck using focus() or click(). From what I can tell it is because the managed metadata fields are not made using <input> but rather <div>.
This is the HTML that renders on the NewForm for the managed metadata field:
<div title="Customer" class="ms-taxonomy-fieldeditor ms-taxonomy-fieldeditor-standard" style="width: 364px;"> <div class="ms-rtestate-write ms-taxonomy-writeableregion ms-inputBox" id="Customer_$containereditableRegion" role="textbox" aria-haspopup="true" contenteditable="true" aria-autocomplete="both" aria-multiline="true" DisableRibbonCommands="True" AllowMultiLines="false" RestrictPasteToText="True" RteDirty="true"> </div> </div> If you take the class ms-inputBox out of the interior <div> you can see that the 'input-like' look is only done via css. I think this is why I'm unable to get the .click() and .focus() actions to work appropriately.
Something else I found interesting was that after a value has been validated SharePoint adds in a <span> as a container for the text as a child of the <div>.
HTML after validated value:
<div title="Customer" class="ms-taxonomy-fieldeditor ms-taxonomy-fieldeditor-standard" style="width: 364px;"> <div class="ms-rtestate-write ms-taxonomy-writeableregion ms-inputBox" id="Customer_$containereditableRegion" role="textbox" aria-haspopup="true" contenteditable="true" aria-autocomplete="both" aria-multiline="true" DisableRibbonCommands="True" AllowMultiLines="false" RestrictPasteToText="True" RteDirty="false"> <span title="" class="valid-text">Customer X</span> </div> </div> I have tried to manually add a <span> and insert my text there. That also did not work when saving the form.
I think this info opens up more questions than answers, but if someone has any ideas on a solution that would be great.
Customer Xpage, prepopulate field withCustomer X, if they came fromCustomer Ypage, prepopulate field withCustomer Y.mySite/_vti_bin/TaxonomyInternalService.json/GetMatchesis called. Have done some brief research on that and haven't found much. I haven't been able to depict what is initiating that call via dev tools yet.click()orfocus()on the textbox from Javascript after you have populated the value?