Quantcast
Channel: Developing Apps for SharePoint 2013 forum
Viewing all articles
Browse latest Browse all 8089

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

$
0
0

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


Viewing all articles
Browse latest Browse all 8089

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>