› Forums › Classic DFFS › Query/Update/Delete an item in JavaScript
- This topic has 5 replies, 2 voices, and was last updated 2 years, 3 months ago by Alexander Bautz.
-
AuthorPosts
-
-
November 26, 2021 at 22:29 #35254MichaelTCCParticipant
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?
-
November 26, 2021 at 23:51 #35260Alexander BautzKeymaster
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
-
November 27, 2021 at 00:56 #35262MichaelTCCParticipant
Thank you Alexander!!!
-
November 27, 2021 at 11:59 #35265Alexander BautzKeymaster
Thanks the for donation!
PS: I edited the REST examples and changed from getbytitle to getbyid.
Alexander
-
November 27, 2021 at 15:26 #35267MichaelTCCParticipant
Thanks Alezander! Is this information in one of the user guides? Can you please point me to it?
-
November 27, 2021 at 18:31 #35269Alexander BautzKeymaster
No, this is not documented in any of the user manuals. What kind of information are you looking for?
Alexander
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.