Nintex Forms O365 - Getting SharePoint ClientContext Do's and Dont's

Document created by cellou.diallo@nintex.com Employee on Jan 25, 2015
Version 1Show Document
  • View in full screen mode

Nintex Forms for Office 365 is developed following the SharePoint App Model which lets you run your application outside of SharePoint from any web server and it allows developers to interact with SharePoint resources using JSOM on the client side.

 

To work with SharePoint JSOM we need to get hold of the ClientContext which exposes useful APIs to us. There are many tutorials on the web showing how to do that. A good starting point is referenced on msdn

Basically all you need to do is reference to relevant SharePoint script files and initialize the ClientContext with the site url that you want to query. However, be aware of the following:

 

Do's:

  • Wrap your codes within the Nintex client ready events
    NWF.FormFiller.Events.RegisterAfterReady(function (){});    
  • Only load the SharePoint scripts that you need
  • Load SharePoint scripts asynchronously
  • Leverage the built-in Nintex cachedScript function which return a promise to cache your scripts NWF$.cachedScript('myscripturl');
  • Be aware of cross domain restrictions. Use the cross-domain library if you want to interact with the Host Web for example

 

Dont's:

 

Nintex Forms for Office 365 by default loads most of the core SharePoint scripts, therefore if you load a script that was already loaded you might ended with an error on your developer console and an unresponsive form probably; in that case check the console error and remove the duplicated script. For example, the form will load, but not only, these core files by default:

  • 'SP.Runtime.js'
  • 'SP.RequestExecutor.js'
  • 'SP.js'

The code snippet below shows how you can load SharePoint scripts and initialize a ClientContext.

 

// A sample how you can load your SharePoint scripts and initialize a clientContext.
NWF.FormFiller.Events.RegisterAfterReady(function (){     
  /*if you need to load scripts other than those pre-loaded by Nintex Forms*/
  //loadScripts().done(function(){
  // getDataFromSharePoint();
  //});

  /* if you don't need to load any specific scripts and use those pre-loaded by Nintex Forms*/
  // getDataFromSharePoint();
});


function loadScripts(){
  var def = NWF$.Deferred();
  var layoutsPath = '/_layouts/15/';
  var appWebUrl = NF.Url.getQueryStringParameter("SPAppWebUrl");
  var hostUrl = NF.Url.getQueryStringParameter("SPHostUrl");
  var scriptsPath = hostUrl + layoutsPath;


  (function () {
  // only load the scripts that you need
  return NWF$.cachedScript(scriptsPath + 'mysharepointneeded.js');
  })()
  .pipe(function () {
  // you can chain, and load many script by using pipe callback
  return NWF$.cachedScript(scriptsPath + 'mysharepointneeded.js');
  })
  .done(function () {
  def.resolve();
  })

  return def.promise();
}




function getDataFromSharePoint() {
    var clientContext;
    var factory;
    
  clientContext = new SP.ClientContext(appwebUrl); 
  factory = new SP.ProxyWebRequestExecutorFactory(appwebUrl);
    clientContext.set_webRequestExecutorFactory(factory);
    
  // write your codes here to query SharePoint
}

 

NB: Above code snippet is for reference purposes only

2 people found this helpful

Attachments

    Outcomes