1

how to check Title( Country) if exists in the list. if exists it is necessary to do the update if not it is necessary to add it ?

function AddListItem() {

    var listTitle = "Test";
    //Get the current client context  
    context = SP.ClientContext.get_current();
    var airportList = context.get_web().get_lists().getByTitle(listTitle);

        //Create a new record  
    var listItemCreationInformation = new SP.ListItemCreationInformation();
    var listItem = airportList.addItem(listItemCreationInformation);

    //Set the values 
    var country= $("#Country").val();
    var client= $("#Client").val();
    // var nb1 = $("#nb1").val(); 
    // var nb2 = $("#nb2").val();  
    // var nb3 = $("#nb3").val(); 
    // var nb4 = $("#nb4").val();  
    // // var nb5 = $("#nb5").val(); 


    listItem.set_item('Country', Country);
    listItem.set_item('Client', Client);
    listItem.update();
    context.load(listItem);
    context.executeQueryAsync(AddListItemSucceeded, AddListItemFailed);
}
function AddListItemSucceeded() {
}
function AddListItemFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

2 Answers 2

1

You need to do multiple nested asynchronous operations, i.e. query for title equals the selected country.

In the success callback, if you got an item back, update it. If not add a new one. Either way, you're going to be calling executeQueryAsync again inside of the success callback for the first call to executeQueryAsync. Something like:

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
    '<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + 
    '<Value Type=\'String\'>' + $("#Country").val() + '</Value></Eq></Where></Query>' + 
    '<RowLimit>1</RowLimit></View>'
);
var items = airportList.getItems(camlQuery);

clientContext.load(items);
clientContext.executeQueryAsync(
    function(sender, args) {
        var itemEnum = items.getEnumerator();
        // if we have an item, update it
        if(itemEnum.moveNext()) {
            // update the item
            var current = itemEnum.get_current();
            current.set_item('Title', $("#Country").val());
            current.set_item('Client', $("#Client").val());
            current.update();
            context.load(current);
            clientContext.executeQueryAsync(
                function(sender, args) {
                    // handle successful update
                },
                function(sender, args) {
                    // handle error
                });
        }
        // otherwise, add a new one
        else {
            // use the code you've already written to add a new item
            var info = new SP.ListItemCreationInformation();
            var current = airportList.addItem(info);
            current.set_item('Title', $("#Country").val());
            current.set_item('Client', $("#Client").val());
            current.update();
            context.load(current);
            clientContext.executeQueryAsync(
                function(sender, args) {
                    // handle successful add
                },
                function(sender, args) {
                    // handle error
                });
        }
    },
    function(sender, args) {
        // handle errors
    }
); 
1

I had used rest api for similar requirement.

function CheckListItem(title) {
            return $.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('listtitle')/items?$filter=Title eq '"+title+"'",
                type: 'GET',
                async: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                }
            })
        }


CheckListItem('title').done(function (data,textStatus, jqXHR) {
                    if(data.d.results.length>0){
                        var itemID=data.d.results[0].ID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.