我有3个列表 - 控件,父母和孩子。每当一个项目被添加到控件列表中时,我需要将父项中的所有项目复制到子项中(稍微更改一下)在该过程发生几次之后,子列表计数将是父列表的倍数计数,但父母将保持不变。
我想在用户点击控制列表中新项目的“保存”按钮时发生这种情况。
我在PreSaveAction中编写了代码并将其放在控制列表的New Item页面的Script editor部分中。
代码无法按需执行。我添加了提醒,以便我可以关注正在发生的事情。我得到的最后一个警报是在第42行,它告诉我父列表上的计数。我没有得到第45行或其他任何其他警报。请注意,这两个警报之间没有任何操作,只有少数注释行。添加了控件,但副本没有发生。
以下是代码:
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript">
function PreSaveAction()
{
var clientContext;
var web;
var lists
var inputList;
var outputList;
alert("before caml query");
// var query = "<View Scope = 'RecursiveAll'><Query><Where><Eq><FieldRef Name='Active'/>" +
// "<Value Type='Integer'>1</Value></Eq></Where></Query></View>";
var query = "<Where><Eq><FieldRef Name='Active'/>" +
"<Value Type='Integer'>1</Value></Eq></Where>";
clientContext = SP.ClientContext.get_current();
web = clientContext.get_web();
lists = web.get_lists();
inputList = lists.getByTitle('ParentList');
outputList = lists.getByTitle('ChildList');
var camlQuery = new SP.CamlQuery();
// return only Active items
camlQuery.set_viewXml(query);
alert("in view xml");
this.inputListItems = inputList.getItems(camlQuery); //Parent
clientContext.load(inputListItems);
alert("after loading list items");
// The copying will be done in the succeeded function
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
return true;
};
function onQuerySucceeded(sender, args)
{
alert("start Query Succeeded");
var inputItem = '';
var outputItem = '';
var parentID = 0;
var parentTitle = '';
var contolName = "blah";
alert("Input List count = " + inputListItems.get_count());
// get the reference to the control
// var contolName = document.getElementsByName("Title")[0].value;
alert("in control " + controlName);
var listItemEnumerator = inputListItems.getEnumerator();
alert("after setting Enumerator");
while (listItemEnumerator.moveNext())
{
var inputItem = listItemEnumerator.get_current();
parentID = inputItem.get_id();
parentTitle = inputItem.get_item('Title');
// now create the copy in output
// CreateOutputListItem(parentID, parentTitle, contolName);
outputItem.set_item('Parent', parentTitle);
outputItem.set_item('ParentID', parentID);
outputItem.set_item('Control', contolName);
outputItem.update();
clientContext.load(outputItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onWriteSucceeded), Function.createDelegate(this, this.onWriteFailed));
}
}
function onWriteSucceeded(sender, args) {
alert('Item created: ' + outputItem.get_id());
}
function onWriteFailed(sender, args)
{
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>