locked
Updating and Deleting a SharePoint 2013 List item in the Host Web from a SharePoint App RRS feed

  • Question

  • Hi,

    I am  trying to update/delete a specific list item in the Host web from a SharePoint 2013 App using Rest service.

    In lot of samples i have seen that they are updating a list item in the app itself and so they are using data._metadataUrl which is in the below format.

    http://app-d6dc114e55bbc9.apps-qa.domain.com/sites/CSOM/REST/_api/Web/Lists(guid'b96fb283-0e21-440d-9111-7422048b45d8')/Items(1)

    This is for list item with ID=1.For ID=2 it will be items(2) etc.

    I have followed this blog and i came to know that we need to query the lists in the host Web by using

    http://blog.ctp.com/2014/04/28/performing-rest-operations-on-host-webs-from-sharepoint-hosted-apps/

    App-Web-URL/_api/SP.AppContextSite(@target)/web/lists/getbytitle(List-Name‘)/items?@target=’Host-Web-URL

    But this works fine for selcting any items in the list but for updating i need to hard code the paritcular list item

    For ex:I am updating a list item with ID=1

    var updateUrl = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('" + listName + "')/items(1)?" + "@target='" + SPHostUrl + "'";

    How to get the URL of the current list item in the Host Web dynamically from a SharePoint like

    (http://WebAPpName/sites/CSOM/REST/_api/Web/Lists(guid'b96fb283-0e21-440d-9111-7422048b45d8')/Items(1))

       var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('" + listName + "')/items?" +
            "@target='" + SPHostUrl + "'&$filter=CustomerID eq 'BERGS'";

    http://app-d6dc114e55bbc9.apps-qa.domain.com/sites/CSOM/REST/_api/Web/Lists(guid'b96fb283-0e21-440d-9111-7422048b45d8')/Items(1)

    Unfortuantely i cannot use that since i dont have a  List with this GUID in my app since my list is residing in the Host Web instead of app.

    function updateListItem(url, listname, id, metadata, success, failure) {
        // Prepping our update


        var updateUrl = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('" + listName + "')/items(1)?" + "@target='" + SPHostUrl + "'";

        //http://app-d6dc114e55bbc9.apps-qa.domain.com/sites/CSOM/REST/_api/Web/Lists(guid'b96fb283-0e21-440d-9111-7422048b45d8')/Items(1)

        var title = "vijay";


        //var item = $.extend({
        //    "__metadata": { "type": getListItemType(listname) }
        //}, "Company_x0020_Name:vijay");

        var item = {
            "__metadata": { "type": getListItemType(listname) },
            "City": title
        };


        getListItemWithId(url, id, function (data) {

            $.ajax({
                url: updateUrl,
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify(item),
                headers:
                    {
                        "Accept": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "X-HTTP-Method": "MERGE",
                        "If-Match": data.__metadata.etag

                    },
                success: function (data) {
                    success(data);
                },
                error: function (data) {
                    failure(data);
                }
            });

        }, function (data) {
            failure(data);
        });
    }

    function getListItemWithId(url, itemId, success, failure) {


        var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('" + listName + "')/items?" +
            "@target='" + SPHostUrl + "'&$filter=CustomerID eq 'BERGS'";

        //http://app-d6dc114e55bbc8.apps-qa.domain.com/sites/CSOM/REST/_api/Web/Lists(guid'b96fb283-0e21-440d-9111-7422048b45d8')/Items(1)


        $.ajax({
            url: url,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data) {
                if (data.d.results.length == 1) {
                    success(data.d.results[0]);
                }
                else {
                    failure("Multiple results obtained for the specified Id value");
                }
            },
            error: function (data) {
                failure(data);
            }
        });
    }


    Thanks, Vijay Arockiasamy

    Thursday, January 29, 2015 9:27 PM

All replies

  • Hi Vijay!

    Have a look at my post: http://ioi.solutions/easy-way-get-sharepoint-context/

    It will save you a lot of your time.

    SPLoader.js file has example of updating/deleting  items:

    function updateCheckList(list,listItemId,fields) {
        var deferred = $.Deferred();
        SpLoader.ready(function () {
    
            var listName = list;
    
                var oList = SpLoader.hostWeb.get_lists().getByTitle(listName);
    
                var oListItem = oList.getItemById(listItemId);
    
                $.each(fields, function (index,item) {
                    oListItem.set_item(item.fname, item.value);
                });
    
                oListItem.update();
    
    
                SpLoader.context.executeQueryAsync(
                    function () {
                        deferred.resolve("Operation successfully completed.");
                    },
                    function (sender, args) {
                        deferred.resolve('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                    }
                );
    
        });
        return deferred.promise();
    }

    In the above example I am dynamically updating fields with new values.

    fields= [{fname:'field1',value:1},{fname:'field2',value:0},....]

    Nick



    • Edited by nicksav Friday, January 30, 2015 12:53 AM
    Friday, January 30, 2015 12:49 AM
  • Thanks Nick.Let me check the blog and will keep you posted.

    Thanks, Vijay Arockiasamy

    Friday, January 30, 2015 10:24 PM
  • Hi Vijay,

    Any update?

    Best regards


    Patrick Liang
    TechNet Community Support

    Friday, February 6, 2015 3:18 AM
  • I looked in to SPLoader.Js file.Could you please let me know which exact function inside that js file is used to get the Dynamic list item values from Host Web.

    Thanks, Vijay Arockiasamy

    Wednesday, February 11, 2015 3:10 AM