Skip to content

Instantly share code, notes, and snippets.

@RainerAtSpirit
Created August 23, 2012 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RainerAtSpirit/3436332 to your computer and use it in GitHub Desktop.
Save RainerAtSpirit/3436332 to your computer and use it in GitHub Desktop.
Goodbye Ferrari, hello Napa
(function () {
var params = getParams(),
scriptbase = params.SPHostUrl + "/_layouts/15/";
// Load the js file and continue to the
// success event handler.
$.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
// Function to prepare and issue the request to get
// SharePoint data.
function execCrossDomainRequest() {
var executor = new SP.RequestExecutor(params.SPAppWebUrl);
// Issue the call against the host web.
// To get the title using REST we can hit the endpoint:
// app_web_url/_api/SP.AppContextSite(@target)/web/title?@target='siteUrl'
// The response formats the data in the JSON format.
// The functions successHandler and errorHandler attend the
// success and error events respectively.
executor.executeAsync(
{
url:
params.SPAppWebUrl +
"/_api/SP.AppContextSite(@target)/web?$expand=CurrentUser&@target='" +
params.SPHostUrl + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: successHandler,
error: errorHandler
}
);
}
// Function to handle the success event.
// Prints the host web's title to the page.
function successHandler(data) {
var jsonObject = JSON.parse(data.body);
$('#message').html(jsonObject.d.CurrentUser.Title +
'<br/>HostWeb: <b>' + jsonObject.d.Title + '</b>');
}
// Function to handle the error event.
// Prints the error message to the page.
function errorHandler(data, errorCode, errorMessage) {
$('#message').html("Could not complete cross-domain call: " + errorMessage);
}
// Returning the params object
function getParams() {
var params = {};
location.search.split('?')[1].split('&').forEach(function (param) {
var key = param.split('=')[0],
val = decodeURIComponent(param.split('=')[1]);
params[key] = val;
});
return params;
}
})();
<!DOCTYPE HTML>
<html>
<head>
<title>
Goodbye Ferrari, hello Napa
</title>
<!-- Add your CSS styles to the following file -->
<link rel="Stylesheet" type="text/css" href="../Content/App.css" />
</head>
<body>
<div id="message">
<!-- The following content will be replaced with the user name when you run the app - see App.js -->
initializing...
</div>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../Scripts/App.js"></script>
</body>
</html>
<%-- The following 5 lines are ASP.NET directives needed when using SharePoint components --%>
<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Language="C#" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<!DOCTYPE HTML>
<html>
<head>
<title>
<SharePoint:ProjectProperty Property="Title" runat="server" />
</title>
<!-- Add your CSS styles to the following file -->
<link rel="Stylesheet" type="text/css" href="../Content/App.css" />
</head>
<body>
<div id="message">
<!-- The following content will be replaced with the user name when you run the app - see App.js -->
initializing...
</div>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../Scripts/App.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment