Advertisement
PtiTom

SharePoint-Hosted Get Host Lists

Nov 28th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var context = SP.ClientContext.get_current();
  4. var user = context.get_web().get_currentUser();
  5. var web = context.get_web();
  6.  
  7. var hostWeb;
  8. var hostweburl;
  9. var appweburl;
  10.  
  11. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
  12. $(document).ready(function () {
  13.     InitCrossContext();
  14.     getUserName();
  15. });
  16.  
  17. function InitCrossContext() {
  18.     hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
  19.     appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
  20.     var scriptbase = hostweburl + '/_layouts/15/';
  21.  
  22.     // Permet l'exécution sur un autre domaine (Site hôte) :
  23.     $.getScript(scriptbase + 'SP.RequestExecutor.js', printAllListNamesFromHostWeb);
  24. }
  25.  
  26. function printAllListNamesFromHostWeb() {
  27.     // Prépare le contexte d'exécution vers l'hôte :
  28.     var contextHost = new SP.ClientContext(appweburl);
  29.     var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
  30.     contextHost.set_webRequestExecutorFactory(factory);
  31.     var appContextSite = new SP.AppContextSite(contextHost, hostweburl);
  32.  
  33.     // Batches de chargement des informations :
  34.     this.hostWeb = appContextSite.get_web();
  35.     var collList = this.hostWeb.get_lists();
  36.     contextHost.load(this.hostWeb);
  37.     contextHost.load(collList);
  38.  
  39.     contextHost.executeQueryAsync(
  40.         Function.createDelegate(this, successHandler),
  41.         Function.createDelegate(this, errorHandler));
  42.  
  43.     function successHandler() {
  44.         var listInfo = '';
  45.         var listEnumerator = collList.getEnumerator();
  46.         while (listEnumerator.moveNext()) {
  47.             var oList = listEnumerator.get_current();
  48.             listInfo += '<li>' + oList.get_title() + '</li>';
  49.         }
  50.  
  51.         $("#distantLists").html('<p>Web Title (Host) : ' + this.hostWeb.get_title() + '</p><p>X - Lists found:</p><ul>' + listInfo + '</ul>');
  52.     }
  53.  
  54.     function errorHandler(sender, args) {
  55.         document.getElementById("message").innerText = "Could not complete cross-domain call: " + args.get_message();
  56.     }
  57. }
  58.  
  59. // This function prepares, loads, and then executes a SharePoint query to get the current users information
  60. function getUserName() {
  61.     context.load(user);
  62.     context.load(web);
  63.     context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
  64. }
  65.  
  66. // This function is executed if the above call is successful
  67. // It replaces the contents of the 'message' element with the user name
  68. function onGetUserNameSuccess() {
  69.     $('#message').text('Hello ' + user.get_title());
  70.     $('#localLists').text('Web title (local) : ' + web.get_title());
  71. }
  72.  
  73. // This function is executed if the above call fails
  74. function onGetUserNameFail(sender, args) {
  75.     alert('Failed to get user name. Error:' + args.get_message());
  76. }
  77.  
  78. function getQueryStringParameter(paramToRetrieve) {
  79.     var params =
  80.         document.URL.split("?")[1].split("&");
  81.     var strParams = "";
  82.     for (var i = 0; i < params.length; i = i + 1) {
  83.         var singleParam = params[i].split("=");
  84.         if (singleParam[0] == paramToRetrieve)
  85.             return singleParam[1];
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement