1

Is there a way to use SPServices update command that is not using the ID as the reference field? I might use employee ID but I cant seem to get it to work. This code below shows when I am using the list ID.

function updateSomething(){
    values = [
          ["employeeID", $("#tbEmployeeID").val()],
          ["firstName", $("#tbFirstName").val()],
          ["lastName", $("#tbLastName").val()],
          ["mail",  $("#tbEmployeeEmail").val()],
          ["mobile", $("#tbMobileNumber").val()],
          ["alternateMobile", $("#tbAlternativeContactNumber").val()],
          ["emergencyContact", $("#tbEmergencyContactName").val()],
          ["emergencyContactNumber", $("#tbEmergencyContactNumber").val()],
          ["lococation", $("#tbWorkLocation").val()]
        ];

    $(function(){
            $().SPServices({
                            operation :"UpdateListItems",
                            sync: true,
                            webURL: this.spSiteUrl,
                            valuepairs:values,
                            listName: "TeamContactDetails",
                            ID:9,
                            completefunc: function (xData, Status) {
                                alert("Update Completed");
                            }
                });    
    });
}
  • Are you passing default ID column value here "ID:9"? – Dikesh Gandhi Jul 8 '16 at 8:14
  • yup.. currently it is using the default column ID which was created in the list. I created a new column named employeeID and I want to use that as my reference value for the update – Rey Norbert Besmonte Jul 8 '16 at 8:15
  • 1
    You can't use your employeeID column because the SPServices uses the ID value to identify the ListItem. Same applies to JSOM or REST code, ID is the identifier for the Item (in one List/Library) – Danny '365CSI' Engelman Jul 8 '16 at 8:34
  • You can use simple JavaScript JSOM to update the list items. – Dikesh Gandhi Jul 8 '16 at 8:35
1

You can not use your custom column in SPServices update command.

You can use this method instead (JSOM):

var siteUrl = '/sites/MySiteCollection';

function retrieveListItemsInclude() {

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

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Where><Eq><FieldRef Name="EmployeeId"/><Value type='Text'>9</Value></Eq></Where></View>');    //You can pass your query
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem, 'Include(Id, DisplayName, HasUniqueRoleAssignments)');

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

function onQuerySucceeded(sender, args) {


    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        oListItem.set_item('Title', 'NewTitle');
        oListItem.update();
        clientContext.load(oListItem);
    }

    alert('Items updated!');
}

function onQueryFailed(sender, args) {

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

You can get more info from here:

0

Thank you for all the help. I created a work around for the problem. I created a Search command first then stored it on a variable then used it on the SPService Update

function findEmployeeID(){  
var cmlSearcEmpIDDetails = "<Query><FieldRef Name='ID'/><Where><Eq><FieldRef Name='employeeID' /><Value Type='Text'>"+ $("#tbEmployeeID").val()+"</Value></Eq></Where></Query>";
            $().SPServices({
                    operation: "GetListItems",
                    async: false,
                    listName: "TeamContactDetails",
                    CAMLViewFields: "<ViewFields><FieldRef Name='ID'/><FieldRef Name='employeeID'/></ViewFields>", 
                    CAMLQuery:cmlSearcEmpIDDetails, 
                    completefunc: function (xData, Status) {
                      $(xData.responseXML).SPFilterNode("z:row").each(function() {
                        employeeIDArray.push({empID: $(this).attr("ows_ID")});
                    });
                  }
            });
}

function updateSomething(){
    findEmployeeID();
    values = [
          ["employeeID", $("#tbEmployeeID").val()],
          ["firstName", $("#tbFirstName").val()],
          ["lastName", $("#tbLastName").val()],
          ["mail",  $("#tbEmployeeEmail").val()],
          ["mobile", $("#tbMobileNumber").val()],
          ["alternateMobile", $("#tbAlternativeContactNumber").val()],
          ["emergencyContact", $("#tbEmergencyContactName").val()],
          ["emergencyContactNumber", $("#tbEmergencyContactNumber").val()],
          ["lococation", $("#tbWorkLocation").val()]
        ];

    $(function(){
            $().SPServices({
                            operation :"UpdateListItems",
                            sync: true,
                            webURL: this.spSiteUrl,
                            valuepairs:values,
                            listName: "TeamContactDetails",
                            ID:employeeIDArray[0].empID,
                            completefunc: function (xData, Status) {
                                alert("Update Completed");
                            }
                });    
    });
}

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.