在创建项目之前,检查另一个SharePoint列表中是否存在字段值

时间:2015-10-08 18:24:22

标签: javascript sharepoint sharepoint-2013

我想在使用PreSaveAction函数保存之前检查另一个列表中是否存在项值。我要检查的值是EventDate

function PreSaveAction()    {
var time = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
alert(time);
//Validation for current fields

  if(SPUtility.GetSPField('Field').GetValue() == "yes")
{
     alert('Validation passed, let SharePoint continue');
     return true;}
else
{
alert('Validation failed, let SharePoint not continue');
     return false;
}
}

我从第二个列表中得到了这些项目:

function ViewItem()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('demoTrainingRoom2');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, 'Include(Title, EventDate)');

context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}

function success() {
 
//var contador = this.allitems.getCount(); 
var TextFiled = "";
var ListEnumerator = this.allItems.getEnumerator();
var firstListTime = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
while(ListEnumerator.moveNext())
{
var currentItem = ListEnumerator.get_current();
TextFiled += currentItem.get_item('Title') + '-' + currentItem.get_item('EventDate'); +'\n';
}
alert(TextFiled);
}

function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}

所以基本上我想在使用PreSaveAction时检查列表1上的任何项是否在列表2上具有相同的EventDate。任何帮助将不胜感激

更新

感谢您的兴趣,现在我可以使用以下代码成功检查第一个列表中的值是否存在于第二个列表中。现在我需要在demoField等于yes时将该检查的输出传递给PreSaveAction函数

function ViewItem()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('demoTrainingRoom2');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, 'Include(Title, EventDate)');

context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}

function success() {
 
var currentTitle = SPUtility.GetSPFieldByInternalName('EventDate').GetValue(); 
for(var i = 0; i < this.allItems.get_count(); i++){
					 var item = this.allItems.get_item(i);
					 console.log(item.get_item('EventDate'));
					                      
                     if (currentTitle === item.get_item('EventDate')){
                     alert('I exist on the second list' + ' ' + item.get_item('EventDate'));
                      return true; // or item
				      }
				  }
				  return false;
				}


function failed(sender, args) {
alert("failed. Message:" + args.get_message());
}


function PreSaveAction()    {

var time = SPUtility.GetSPFieldByInternalName('EventDate').GetValue();


if(SPUtility.GetSPField('demoField').GetValue() == "yes")
{
   //Saving File
     
     return true;}
else
{ 
    //NOt saving FIle
     return false;
}
}

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在你的第一个列表OnSuccess()方法中调用PreSaveAction()方法,将事件日期作为caml查询传递给第二个列表,即在事件日期查询第二个列表.........这只是片段可能包含一些错误,请查看

 function ViewItem()
 {
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('demoTrainingRoom2');
var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
 context.load(allItems, 'Include(Title, EventDate)');

  context.executeQueryAsync(Function.createDelegate(this, this.success),Function.createDelegate(this, this.failed));
}

  function success() {

 var TextFiled = "";
var ListEnumerator = this.allItems.getEnumerator();
 var firstListTime =        SPUtility.GetSPFieldByInternalName('EventDate').GetValue();
while(ListEnumerator.moveNext())
 {

 var evtDate = currentItem.get_item('EventDate');

function PreSaveAction() {

var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('List2');

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'EventDate\'/>' + 
    '<Value Type=\'DateTime\'>'+ evtDate  +'</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>');
this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

clientContext.executeQueryAsync(Function.createDelegate(this,       this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

 }

 }
 function onQuerySucceeded(sender, args) {

var listItemInfo = '';

var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {

    alert('event date exist')

  }    
 }

function onQueryFailed(sender, args) {

  alert('Request failed. ' + args.get_message() + '\n' +   args.get_stackTrace());
 }