Recently we had a need to autofill some dropdowns in an
upload form based on query parameter.
Sounds pretty easy right? After all, this just involves adding some
JavaScript onto the page to pick something from the query string (lets say an
index for convenience), then manupilate the DOM to set selected item index of
the html "<Select>...</Select>" corresponding to the choice field
you desire to the value obtained from the QueryString.
But the only problem is, this is SharePoint and things arent so easy.
First of all as Sig recently reminded me, loading custom JavaScript within a
SharePoint form (yes the OWSForm) is sentencing yourself to endless hours of
missery repetedly chanting "oh why, oh why isnt my function getting fired".
Fortunately he showed me the following tip which loads the script
outside the form, by enclosing it in an image tag.
<img border="0" alt="" onload="JSFunctionToCall()"
src="/_layouts/images/blank.gif" width="0px" height="0px"/>
I might be paranoid, and so I state that tag is most usefull if inserted
outside SharePoint form tags.
Ok, so now you have script that runs when the page loads, and should set your
dropdown to the value you require.
But it works and it doesnt work. You see it getting set OK. You can query the
value in JavaScript and see it gets set OK. But it doesnt reflect as so on the
page.
Why? The form gets reset as soon as it is loaded.
So you need a means of pausing execution of your JavaScript till after the
form has been reset. Like so
...
function JSFunctionToCall(){
// Do something
clearInterval(ourInterval); // this stops the
timer dead using the ID
}
...
<script language="javascript">var ourInterval =
setInterval("JSFunctionToCall()", IntervalPeriod);</script>
Chances are if you didnt know about this, you will soon
reach your point of frustration and start punching holes in your monitor and
want to start kicking your server really hard. A friend and I would have
resorted to that eventually if we didnt realize the above was happening. Thanks
very much for the help on this one Sig.