Skip to main content

Access Host Web from App web in SharePoint hosted app - Office 365


Here is a snippet to save your day.
There are many snippets out there showing you how to do cross domain calls, but none of them complete.

Here is a sample to access host web components from a SharePoint hosted app or add in.
Keywords: SharePoint, access host web, sharepoint online, JSOM, CSOM, SharePoint hosted app, Office 365.


'use strict';

//URLs
var hostweburl;
var appweburl;

var context;
var appContextSite;
var factory;

var context = SP.ClientContext.get_current();
var appContextSite;  
var rootweb ;
var roleDefinitions ; 

(function () {

 $(document).ready(function () {
     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
 
     var scriptbase = hostweburl + "/_layouts/15/";
 
     $.getScript(scriptbase + "SP.Runtime.js",
        function () {
      $('#message').append(" Getting Scripts.. 
");
            $.getScript(scriptbase + "SP.js",
                function () { $.getScript(scriptbase + "SP.RequestExecutor.js", loadRoleDefinition); }
                );
        }
        );
 });

 // This function prepares, loads, and then executes a SharePoint query 
 function loadRoleDefinition() {
  
  context = new SP.ClientContext(appweburl);
     factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
     context.set_webRequestExecutorFactory(factory);
     appContextSite = new SP.AppContextSite(context, hostweburl);
  rootweb = appContextSite.get_web();
  roleDefinitions = rootweb.get_roleDefinitions();
 
  context.load(appContextSite);
  context.load(rootweb);
  context.load(roleDefinitions);
  context.executeQueryAsync(onGetRoleDefinitionSuccess, onGetRoleDefinitionFail);
 }

 function onGetRoleDefinitionSuccess() {
  
  var permissionExists = false;
  var roleDefinitionsEnum = roleDefinitions.getEnumerator();
  while (roleDefinitionsEnum .moveNext()) {
   var temp = roleDefinitionsEnum.get_current();
   $('#message').append(temp.Name + " 
");
  }
 
 }

 function onGetRoleDefinitionFail(sender, args) {
  $('#message').text('Failed to get Role Definitions. Error:' + args.get_message());
 }
})();

Comments

Popular posts from this blog

SharePoint 2013 workflow : The server was unable to process the request

Here, I received another SharePoint 2013 workflow error. This time the workflow was not able to send emails and the following error was being shown Retrying last request. Next attempt scheduled in less than one minute. Details of last request: HTTP Unauthorized to http://sitename/_vti_bin/client.svc/web/lists/getbyid(guid'guid') Correlation Id: id Instance Id: id Fortunately I came across this post which gave me the answer http://sharepoint.stackexchange.com/questions/89101/failed-to-retrieve-the-com-class-factory-for-component-with-clsid-sp-sprequest Solution 1: In InetMgr, go to advanced settings for Security Token Service Application Pool and change "Load User Profile" to true. Recycle application pool. Reason 2 and Solution 2 : There could be another reason for this error. The workflow authentication can fail if the user executing the workflow (this will be the user initiating the workflow) is given permission through active directory group and the co

OWA not working after security update

Office web apps for SharePoint 2013 not working after security updates / server patching. After installation of the following security patches, OWA stopped working there by causing issues with SharePoint 2013. Details of ULS logs are provided below. FarmStateReplicator.exe (0x0CD8) Office Web Apps Farm State Error when trying to connect to Farm State Manager service: System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at http:// /farmstatemanager/FarmStateManager.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal