1

What I am trying to achieve is using javascript to check if a user has created an item for the post. So it would check if post ID = current post and created by = current user.

If it finds an item exists then it is to exit.

If item does not exists then I have another function I need to run to create the item.

The issue seems to be it's getting up to excutequeryasync then nothing is happening

Currently I have the following code.

function createListItem(decision, id, likes) {
var siteUrl = '/';
var countLikes = likes;

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

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><Where>"+
    "<And><Eq><FieldRef Name='Author' /><Value Type='Integer'>  <UserID /> </Value></Eq>"+
    "<Eq><FieldRef Name='PostID' /><Value Type='Number'>" + id + "</Value></Eq>"+
    "</And></Where></Query></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
alert("1");

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

function onQuerySucceeded(sender, args) {
alert("2");
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nTitle: ' + oListItem.get_item('PostID') + 
        '\nBody: ' + oListItem.get_item('Like');
}

alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
  • This is for a list I assume? – IMTheNachoMan Aug 25 '15 at 12:49
  • Yes it is for a list – Lee Watson Aug 25 '15 at 12:50
  • Try to add some of your code to your question to clarify your problem. – Mx. Aug 25 '15 at 13:08
  • Added the code im using – Lee Watson Aug 25 '15 at 13:11
  • your CAML should look like this: camlQuery.set_viewXml('<Query><Where><And><Eq><FieldRef Name="Author" /><Value Type=”Integer”><UserID Type=”Integer”/></Value></Eq><Eq><FieldRef Name="PostID" /><Value Type="Number">' + id + '</Value></Eq></And></Where></Query>'); – IMTheNachoMan Aug 25 '15 at 15:31
1

Try the below code. Just added <View></View> to your query.

function createListItem(decision, id, likes) {
var siteUrl = '/';
var countLikes = likes;

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

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><Where>"+
    "<And><Eq><FieldRef Name='Author' /><Value Type='Integer'>  <UserID /> </Value></Eq>"+
    "<Eq><FieldRef Name='PostID' /><Value Type='Number'>" + id + "</Value></Eq>"+
    "</And></Where></Query></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
alert("1");
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
alert("2");
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nTitle: ' + oListItem.get_item('PostID') + 
        '\nBody: ' + oListItem.get_item('Like');
}

alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
  • It doesn't call the onQuerySucceeded, when it finds something which is what I want it to do. and call onQUeryFailed if it can't find an item – Lee Watson Aug 26 '15 at 13:21
  • Are you sure you are trying with the code I posted because I've tested this and it works fine. If you're trying with your code, just know that the quotes are different in Integer in the CAML query. – Akhoy Aug 26 '15 at 13:27
  • I copied yours in exactly, it's getting up to alert("1"); – Lee Watson Aug 26 '15 at 13:28
  • Can you check in developer console (F12) what error you get? – Akhoy Aug 26 '15 at 13:31
  • Nothing apparently, I will add this script is run when a 'like' button is clicked – Lee Watson Aug 26 '15 at 13:44
0

Try using the currentContext by

var ctx = SP.ClientContext.get_current()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.