Query/Update/Delete an item in JavaScript

Forums Classic DFFS Query/Update/Delete an item in JavaScript

Viewing 2 reply threads
  • Author
    Posts
    • #35254
      MichaelTCC
      Participant

      I’m looking for a way to query, update, or delete a list item in JavaScript within the DFFS framework. I’ve searched the documentation but have not found a solution. Can someone please point me in the correct direction?

    • #35260
      Alexander Bautz
      Keymaster

      Built in to DFFS you find function for crud (Create, Read, Update, and Delete) in the spjs.utility library like this.

      Create a list item:

      spjs.utility.addItem({
          "listName": "DFFS_TestList", // List display name or GUID
          "listBaseUrl": _spPageContextInfo.webServerRelativeUrl,
          "data": {"Title": "New item"}
      });

      Read items:

      spjs.utility.queryItems({
          "listName": "DFFS_TestList", // List display name or GUID
          "listBaseUrl": _spPageContextInfo.webServerRelativeUrl,
          "query": "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>New item</Value></Eq></Where>",
          "viewFields": ["ID", "Title"]
      });

      Update list item with id 150:

      spjs.utility.updateItem({
          "listName": "DFFS_TestList", // List display name or GUID
          "listBaseUrl": _spPageContextInfo.webServerRelativeUrl,
          "id": 150,
          "data": {"Title": "New item - updated"}
      });

      Delete item with i 150:

      spjs.utility.deleteItem({
          "listName": "DFFS_TestList", // List display name or GUID
          "listBaseUrl": _spPageContextInfo.webServerRelativeUrl,
          "id": 150
      });

      PS: Please note that the built in methods use the old webservice methods to keep compatibility with SP 2007 and SP 2010. I recommend using the REST API to do these kind of operations if you are writing your own custom js.

      Create list item with REST:

      spjs.utility.getListByUrlName(_spPageContextInfo.webServerRelativeUrl, "DFFS_TestList").done(function (list) {
          var item = {
              "__metadata": { "type": list.type },
              "Title": "New item created with REST"
          };
          jQuery.ajax({
              "url": _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbyid('"+list.id+"')/items",
              "method": "POST",
              "data": JSON.stringify(item),
              "headers": {
                  "Accept": "application/json;odata=verbose",
                  "content-type": "application/json;odata=verbose",
                  "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
              },
              "success": function (data) {
                  complete(data);
              },
              "error": function (data) {
                  console.log(data);
              }
          });
      });

      Read list items with REST:

      spjs.utility.getListByUrlName(_spPageContextInfo.webServerRelativeUrl, "DFFS_TestList").done(function (list) {
          jQuery.ajax({
              "url": _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbyid('"+list.id+"')/items?$filter=Title eq 'New item created with REST'",
              "method": "GET",
              "headers": {
                  "Accept": "application/json;odata=verbose",
                  "content-type": "application/json;odata=verbose"
              },
              "success": function (data) {
                  console.log(data);
              },
              "error": function (data) {
                  console.log(data);
              }
          });
      });

      Update list item id 152 with REST:

      spjs.utility.getListByUrlName(_spPageContextInfo.webServerRelativeUrl, "DFFS_TestList").done(function (list) {
          var item = {
              "__metadata": { "type": list.type },
              "Title": "New item created with REST - updated"
          };
          jQuery.ajax({
              "url": _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbyid('"+list.id+"')/items(152)",
              "method": "PATCH",
              "data": JSON.stringify(item),
              "headers": {
                  "Accept": "application/json;odata=verbose",
                  "content-type": "application/json;odata=verbose",
                  "If-Match": "*",
                  "X-HTTP-Method": "MERGE",
                  "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
              },
              "success": function (data) {
                  console.log(data);
              },
              "error": function (data) {
                  console.log(data);
              }
          });
      });

      Send item with id 152 to the recycle bin with REST:

      spjs.utility.getListByUrlName(_spPageContextInfo.webServerRelativeUrl, "DFFS_TestList").done(function (list) {
          jQuery.ajax({
              "url": _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbyid('"+list.id+"')/items(152)/recycle",
              "method": "PATCH",
              "headers": {
                  "Accept": "application/json;odata=verbose",
                  "X-HTTP-Method": "DELETE",
                  "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
              },
              "success": function (data) {
                  console.log(data);
              },
              "error": function (data) {
                  console.log(data);
              }
          });
      });

      Read more here: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-lists-and-list-items-with-rest

      Alexander

      • This reply was modified 2 years, 3 months ago by Alexander Bautz. Reason: Replaced getbytitle with getbyid in the REST examples
    • #35262
      MichaelTCC
      Participant

      Thank you Alexander!!!

      • #35265
        Alexander Bautz
        Keymaster

        Thanks the for donation!

        PS: I edited the REST examples and changed from getbytitle to getbyid.

        Alexander

      • #35267
        MichaelTCC
        Participant

        Thanks Alezander! Is this information in one of the user guides? Can you please point me to it?

      • #35269
        Alexander Bautz
        Keymaster

        No, this is not documented in any of the user manuals. What kind of information are you looking for?

        Alexander

Viewing 2 reply threads
  • You must be logged in to reply to this topic.