Skip to content

Instantly share code, notes, and snippets.

@RainerAtSpirit
Created August 28, 2012 14:01
Show Gist options
  • Save RainerAtSpirit/3498247 to your computer and use it in GitHub Desktop.
Save RainerAtSpirit/3498247 to your computer and use it in GitHub Desktop.
Nappa apps and Chrome Control
(function () {
var params = getParams(),
scriptbase = params.SPHostUrl + "/_layouts/15/";
// Load the js file and continue to the
// success handler.
$.getScript(scriptbase + "SP.UI.Controls.js", renderChrome)
// Load the js file and continue to the
// success event handler.
$.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
// Function to prepare the options and render the control.
function renderChrome() {
// The Help, Account, and Contact pages receive the
// same query string parameters as the main page.
var options = {
"appIconUrl": "../images/AppIcon.png",
"appTitle": "Napa and Chrome control",
"appHelpPageUrl": "Help.html?"
+ document.URL.split("?")[1],
"settingsLinks": [
{
"linkUrl": "Account.html?"
+ document.URL.split("?")[1],
"displayName": "Account settings"
},
{
"linkUrl": "Contact.html?"
+ document.URL.split("?")[1],
"displayName": "Contact us"
}
]
};
var nav = new SP.UI.Controls.Navigation(
"chrome_ctrl_placeholder",
options
);
nav.setVisible(true);
$('#chromeControl_stylesheet').on('load', function (event) {
$('#message').show();
})
}
// 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('<h1 class="ms-accentText">CurrentUser: ' + jsonObject.d.CurrentUser.Title + '</h1>' +
'<h2 class="ms-accentText">HostWeb: ' + jsonObject.d.Title + '</h2>');
}
// 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;
}
})();
<%-- 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>
<!-- Chrome control placeholder -->
<div id="chrome_ctrl_placeholder"></div>
<div id="message" style="display: none">
<!-- 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