New demand arrived to duplicate items in a custom list. Out of the box, Sharepoint hasn't provided that functionality. So, lets develop it our selfs... Wat you need to do is to provide a custom link on you existing item that provides a parameter, for example DID (Duplicate ID). Create a custom newform.aspx where you add a new datawebpart that takes your parameter as filter. Add the fields that you want to duplicate to your page en put them in a table that you set hidden. Once done, you add a content webpart Editor to your page and some next script to your page :
I have written an function for the different types of Sharepoint Fields... if I have missed one, don't hesitate to let me know... Now the important block, the different functions... two function I use often to get the Field Objects for manipulating the values or innerHTML methods:
One of the difficult fields to fill in by script was the PeoplePicker. This because when you take al look at the source of the generated Sharepoint HTML, you see that the field exists of different span, div and input fields. Also, in the view state of your element you get HTML tags with a link and as display the name of the user that you have to put in the PeoplePicker field. So first step is to get only the username. Second step is to retrieve the element object of the peoplepicker field that ends with _upLevelDiv. Third step is to put your username in that DIV and at last but not least to validate the user.
One of the easy ones is the normal InputFields. With this field you only have to read and set the values...
An other hard one is the Multiline textfield. I have passed half a day for understanding how this field is being generated by Sharepoint !!! Not easy to understand, because you only see a hidden input field and some DIV's that are empty. First step I have done is to remove from the view field the surrounded DIV-tag. Afterwards convert it as full HTML format, and at least put it in the sharepoint Element and call a sharepoint function to validate it. This is needed because otherwise your text is removed again.
An other funny one is the Lookup or Select Fields. This because Sharepoint can generate two different type of fields. Or it is an select/option list or just even an input tag. This depending if there are less or more than 20 elements in the list. So you need to check the type of field Sharepoint has created, and depending on it populate the select-tag or input-tag.
A Hyperlink field gives you in view an array with the link and the description of the link. so to fill it in your newform you need to fill in two input fields.
Sharepoint provided also a YES_NO field, behind the screen it is just a checkbox.
Two elements that acts amost the same are the radiobutton list and the checkbutton list. Here you need to loop the array of items in your fields and check them if needed.
The Date Time field of sharepoint is not so easy to fill in. This because you get from your field a totaly other format as what you need to fill in sharepoint. The new fields exists of three blocks of fields. The date-field, the Hour-field and the minutes-field. So you need to convert your datetime result into three different blocks. Also it is possible to fill in the hours as format HH: or HH AM/PM. We have put our general site settings for time to 24h option. So here an example function, but depending your site settings it may have to be changed or be completed !
For a Select field, you have the option to choose of an normal selectbox, or one where you may fill in a custom text. This means that in case of pre-populate you need to check if you value exist in the Dropdown field, if not to fill in the custom inputfield.
A interesting field for prepopulating is also the Multi Select field. This because you get an array of fields in your result, but in the newform you receive two multi select boxes. One with the available items and one with the selected items. For making me easy, I created the script to select first the items in the candidates list and call the sharepoint function to populate them in the selected box. So I don't have to remove them myself in the candidate list and transfer them manualy in the selected items box.
Last function I hope is the MetaData Field (or better known as Termstore or taxonomy-field). What I thought to be difficult ended very easy to do. You just need to fill in a DIV that has your fieldID followed by editableRegion. put the values in that DIV and call a Sharepoint function with as parameter the master Parent of your TermStore field. And at least call the validation function to justify your filled in records.
I hope I have handled all different types of fields. It has costed me some hours of development, but I hope the functions I have written may help you and others. I also prefered not to use the JQuery library, this for learning reasons. It makes you understand better how Sharepoints generates his fields and how there behaviour are. Spread the words around and forward my blog so many others can save some time on finding solutions.