Geeks With Blogs

Tangible Thoughts SharePoint, MOSS? and all the other questions

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.

 

Posted on Sunday, July 17, 2005 9:11 PM SharePoint | Back to top


Comments on this post: SharePoint NewItem, Upload form0 and pre-setting values through JavaScript.

# re: SharePoint NewItem, Upload form0 and pre-setting values through JavaScript.
Requesting Gravatar...
Endless gray cells wasted.... utter crap.. But it works now!! :D
Left by A on Jul 18, 2005 7:14 AM

# re: SharePoint NewItem, Upload form0 and pre-setting values through JavaScript.
Requesting Gravatar...
very good!
Left by 设计 on Aug 10, 2005 11:49 AM

# re: SharePoint NewItem, Upload form0 and pre-setting values through JavaScript.
Requesting Gravatar...
Forgive my ignorance, but where is IntervalPeriod defined?
Left by S on Aug 29, 2005 12:41 AM

# re: SharePoint NewItem, Upload form0 and pre-setting values through JavaScript.
Requesting Gravatar...
Left by shuyinhe on Nov 14, 2006 4:36 AM

# re: SharePoint NewItem, Upload form0 and pre-setting values through JavaScript.
Requesting Gravatar...
very good!
Left by shuyinhe on Nov 14, 2006 4:36 AM

# re: SharePoint NewItem, Upload form0 and pre-setting values through JavaScript.
Requesting Gravatar...
Hi,
Nice post.
I am trying to do something similar, but want to create a template for the new item form.
That is, when a particular item as ID#2 is selected it will open up a form with all pre filled values for that item ID.
When that form is opened up user can change the values and when he submits the form a new item will be created in the list with a new ID.
Please provide any suggestions!

Thanks.
Left by Vaishnavi on Nov 11, 2009 12:58 AM

Your comment:
 (will show your gravatar)


Copyright © Tariq | Powered by: GeeksWithBlogs.net