SharePoint Online: [Rest API & SharePoint App] How to access other web context

Required App permission to be setup in order to access the web, under AppManifest.xml

Example code snippet:

SP.AppContextSite is the core part to call from App to SharePoint


var hostweburl =
        decodeURIComponent(getQueryStringParameter("SPHostUrl"));

var appweburl =
        decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

//Example: Get List Item
function getItems (listName, queryText, rowLimit) {
     var d = $.Deferred();
     var viewXml = '<View><Query>' + queryText + '</Query></View>';
     var query = {
         'query': {
            '__metadata': { 'type': 'SP.CamlQuery' },
            'ViewXml': viewXml
         }
     };
     //default limit
     var limit = 10;
     if (rowLimit)
        limit = rowLimit;
                
     var settings = {
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/GetByTitle('" + listName + "')/items?@target='" + hostweburl + "'&$top=" + limit,
        contentType: "application/json;odata=verbose",
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {                                                
           d.resolve(data.d.results);
        },
        error: function (data) {
            d.reject(data);
        }
      };

      if (queryText) {
        settings["data"] = JSON.stringify(query);
      }

      $.ajax(settings);

      return d.promise();
}
Advertisements

MVC: how to render view via jQuery get method

In Razor format:

To avoid page get fully rendered with it’s inheriting layout page content

In _ViewStart.cshtml, change the following from:

@{
Layout = “~/Views/Shared/_Layout.cshtml”;
}

to (adding the following code in bold):

@{
Layout = Request.IsAjaxRequest() ? null : “~/Views/Shared/_Layout.cshtml”;
}

How to load view via jQuery get method

<div id=”targetPlaceholder”></div>

var url = ‘/Home/Index’;

$.get(url, function (data) {
$(‘#targetPlaceholder’).html(data);
});

This will be handy if you need to render different view into one page, or rendering your view data into your modal dialog, or rendering one view in another when the view is not a partial view.

How to handle Postback/ form submit?

to be continue…