Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var context = SP.ClientContext.get_current();
- var user = context.get_web().get_currentUser();
- var web = context.get_web();
- var hostWeb;
- var hostweburl;
- var appweburl;
- // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
- $(document).ready(function () {
- InitCrossContext();
- getUserName();
- });
- function InitCrossContext() {
- hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
- appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
- var scriptbase = hostweburl + '/_layouts/15/';
- // Permet l'exécution sur un autre domaine (Site hôte) :
- $.getScript(scriptbase + 'SP.RequestExecutor.js', printAllListNamesFromHostWeb);
- }
- function printAllListNamesFromHostWeb() {
- // Prépare le contexte d'exécution vers l'hôte :
- var contextHost = new SP.ClientContext(appweburl);
- var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
- contextHost.set_webRequestExecutorFactory(factory);
- var appContextSite = new SP.AppContextSite(contextHost, hostweburl);
- // Batches de chargement des informations :
- this.hostWeb = appContextSite.get_web();
- var collList = this.hostWeb.get_lists();
- contextHost.load(this.hostWeb);
- contextHost.load(collList);
- contextHost.executeQueryAsync(
- Function.createDelegate(this, successHandler),
- Function.createDelegate(this, errorHandler));
- function successHandler() {
- var listInfo = '';
- var listEnumerator = collList.getEnumerator();
- while (listEnumerator.moveNext()) {
- var oList = listEnumerator.get_current();
- listInfo += '<li>' + oList.get_title() + '</li>';
- }
- $("#distantLists").html('<p>Web Title (Host) : ' + this.hostWeb.get_title() + '</p><p>X - Lists found:</p><ul>' + listInfo + '</ul>');
- }
- function errorHandler(sender, args) {
- document.getElementById("message").innerText = "Could not complete cross-domain call: " + args.get_message();
- }
- }
- // This function prepares, loads, and then executes a SharePoint query to get the current users information
- function getUserName() {
- context.load(user);
- context.load(web);
- context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
- }
- // This function is executed if the above call is successful
- // It replaces the contents of the 'message' element with the user name
- function onGetUserNameSuccess() {
- $('#message').text('Hello ' + user.get_title());
- $('#localLists').text('Web title (local) : ' + web.get_title());
- }
- // This function is executed if the above call fails
- function onGetUserNameFail(sender, args) {
- alert('Failed to get user name. Error:' + args.get_message());
- }
- function getQueryStringParameter(paramToRetrieve) {
- var params =
- document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve)
- return singleParam[1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement