Copying List Item to Create New Item

Looking to create a clone button on a form so when someone clicks it, it will open up a new form (same list) that is pre populated with values from the parent item. I can do this with a workflow but it'll save it and ideally I want the user to be able to make edits before hitting save for the first time (since I have workflows that trigger on creation that I don't want to go off prematurely).

I seem to recall there being a relatively simple solution to do this (as I did it once before opting for a workflow for a previous case) but I can't recall or find it.

Hello @teddy0bear,

You can save the data of a form to local storage and then populate a new form with it.

This is the code sample for the button click property:

//save form data to the local storage
localStorage.setItem('FormData', JSON.stringify(fd.data()));

//redirect to the new form
window.location.href = 'New Form URL';

And you need to add this code to the New form:

fd.spRendered(function() {
    var formData = localStorage.getItem('FormData')
    //check whether the local storgae has form data
    if (formData) {
        //populate form
        fd.data(JSON.parse(formData));
        //clear local storage 
        localStorage.removeItem('FormData');
    }
});
1 Like

Thank you! That's what I was looking for :slight_smile:

This worked great. The only issue I had was with columns that were People/Group. For some reason those values were either not stored into "formData" or not retrieved. Lookup columns were cloned just fine.

Hello @vhancock,

I've tested the code on my form with Person or Group fields, and it works fine.

What SharePoint version are you using? Are you getting errors in the browser console when Person or Group field is not populated? Is the form where you save data to local storage the same one that is populated with this data?

What SharePoint version are you using? SharePoint Online

Are you getting errors in the browser console when Person or Group field is not populated? No

Is the form where you save data to local storage the same one that is populated with this data? Yes

Here is the code in the order it is in the script:

image

After we clone the TestCase item, we update some of the fields and set the active Tab as shown above.

image

Confirming that Lookup, single & multiline text, Choice & Boolean columns are cloned just fine. Did not test Date/Time or numeric. Still no luck on Person/Group columns.

@vhancock,

Please export the form and share it with me. I'll check your form to see what goes wrong.

Item (Edit) (9).json (54.8 KB)
Please find attached the exported file.

We read in the form data on line 150
We set the form data on line 246