Copy item details on newform in SharePoint
문제
I need to implement Copy option on newform. I have a form that has a field as "existing issue id" which is a lookup to issue ids of the same list.
On selection of id on form, I need to prefill fields with the values of existing issue.
How do I achieve it without workflow?
해결책
Try to do the following:
- Edit your new form,
- Add Script Editor web part,
- Add the below script.
[Script]
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script language="javascript">
$(document).ready(function () {
// the lookup field in the new form
var IssueIDField= $("select[title='Issue ID']");
IssueIDField.change(function () { Populate(); });
});
var ListItem;
function Populate() {
var IssID = $("select[title='Issue ID']").val();
var clientContext = new SP.ClientContext.get_current();
var IssueList = clientContext.get_web().get_lists().getByTitle('Issues');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq> <FieldRef Name=\'ID\' /> <Value Type=\'Text\'>' + IssID + '</Value></Eq></Where></Query><RowLimit>20</RowLimit></View>');
ListItem = IssueList.getItems(camlQuery);
clientContext.load(ListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.Succed),Function.createDelegate(this,this.Failed));
}
function Succed(sender, args) {
var listItemEnumerator = ListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var LItem = listItemEnumerator.get_current();
$("input[title='Issue Name']").val(LItem.get_item("Title"));
}
}
function Failed(sender, args) {
alert('Error. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
Output
All detail steps, I have mentioned at AUTOFILL LIST FORM FIELDS BASED ON LOOKUP SELECTION IN SHAREPOINT
다른 팁
best solution that i would think of that i have done in the past is to create a new newform.aspx based off the original. Then use Javascript to run when an item is selected to query a sharepoint list to pull the information that you need and then fill all the fields in the form.
its not entirely complex but takes a little coding to get it working. To create the new form you would do that in sharepoint designer. as for the code you can either do it in SPD aswell or goto the form in sharepoint and edit the page and add a script editor.
so to recap.
1) create a new form. set it as default.
2) use the browser, goto the new form and hit f12... in developer tools get the id of the control. and in javascript get the value when selected like:
3) create another function that is called from the one above passing the value to query a list to get all the data that you need:
https://msdn.microsoft.com/en-us/library/office/hh185007(v=office.14).aspx
4) when you get the values from above put them into the controls via its ID.
https://stackoverflow.com/questions/5700471/set-value-of-input-using-javascript-function
sorry its not one full solution. had a long day but above should work perfectly fine.