Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Sharepoint 2013 REST Retrieve list info presave items

$
0
0

Hi All

I have successfully retrieved a specific item using REST with SP2013. Issue is that value is populated AFTER page load hence I cannot do a comparison on PreSave event. It shows as undefined and later gets populated.Any ideas? I would like to compare before items is saved and do some validation. Code below: 

<script type="text/javascript">
var RetValue;
$(document).ready(function() {


    }); 

function CheckTitle(varInput)
{

getItems('CustomList',varInput)
.done(function(data){
var items = data.d.results;
for(var i = 0; i < items.length; i++) {
 console.log(items[i].Title);   
 RetValue = items[i].Title;
}
})
.fail(function(error){
console.log(JSON.stringify(error)); 
});
};

function getListItems(listTitle, queryViewXml)
{
   var queryPayload = { 
       'query':{ 
           '__metadata': { 'type': 'SP.CamlQuery' },
           'ViewXml': queryViewXml
        } 
   }; 
   var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";
   return $.ajax({
      type: "POST", 
      headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
      }, 
      data: JSON.stringify(queryPayload),
      url: endpointUrl
   });
};
function getItems(listTitle,Term)
{
    var query =  "<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + Term + "</Value></Contains></Where></Query><ViewFields><FieldRef Name='Title'/></ViewFields><RowLimit>1</RowLimit></View>";

    return getListItems(listTitle,query);
};
function PreSaveItem()
{
var input = $("input[title='Title']").val();
CheckTitle(input);
alert(RetValue);
   
   if (input = RetValue)
   {
      alert("same");
 return false;
   }
   alert(RetValue);
}
</script>


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>