I decided to post it since it was not so immediate and i had to play with verbs and item properties and maybe could help someone else (i also wrote that 2 months ago, so i don't remember details, so please don't ask me).
Ah, i haven't write method for delete since i don't need it, so have fun :p
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | repositories.service( 'ListRepository' , [ '$http' , '$resource' , function ($http, $resource) { var self = this ; var listName = "List Name" ; var itemType = "SP.Data.List_x0020_NameListItem" ; var wsAddress = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('" + listName + "')/" ; self.getListItem = function (key, onSuccess, onError) { var requestUrl = wsAddress + "items?$filter=Title%20eq%20'" + key + "'" ; $http({ method: 'GET' , url: requestUrl, headers: { "Accept" : "application/json;odata=verbose" } }) .success( function (response) { onSuccess(response); }) .error( function (response) { onError(response); }); }; self.createListItem = function (item, onSuccess, onError) { var requestUrl = wsAddress + "items" ; item[ "__metadata" ] = { "type" : itemType }; var stringifiedItem = JSON.stringify(item); var digest = $( "#__REQUESTDIGEST" ).val(); $http({ url: requestUrl, method: "POST" , headers: { "accept" : "application/json;odata=verbose" , "X-RequestDigest" : digest, "content-Type" : "application/json;odata=verbose" }, data: stringifiedItem }) .success( function (response) { onSuccess(response); }) .error( function (response) { onError(response); }); }; self.updateListItem = function (item, onSuccess, onError) { self.getListItem(item.ExportToNasSetting_Source, function (response) { var oldItem = response.d.results[0]; var requestUrl = wsAddress + "items(" + oldItem.Id + ")" ; item[ "__metadata" ] = { "type" : itemType }; var stringifiedItem = JSON.stringify(item); var digest = $( "#__REQUESTDIGEST" ).val(); $http({ url: oldItem.__metadata.uri, method: "POST" , headers: { "accept" : "application/json;odata=verbose" , "X-RequestDigest" : digest, "content-Type" : "application/json;odata=verbose" , "X-Http-Method" : "MERGE" , "If-Match" : oldItem.__metadata.etag }, data: item }) .success( function (response) { onSuccess(response); }) .error( function (response) { onError(response); }); }, onError); }; }]); |
Also the method getListItem, use filter query string to filter by title, feel free to play with this method as you like.
0 commenti:
Post a Comment
Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).