Monday, September 28, 2015

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());
 }
})();

Thursday, September 17, 2015

The best way to print out powershell exception details



Thanks to this blog, I learnt the best way to print out powershell exception details.

$_.Exception|format-list -force


Wednesday, September 9, 2015

Azure Active Directory GraphClient AuthorizationException


Error : Microsoft.Azure.ActiveDirectory.GraphClient.AuthorizationException Insufficient privileges to complete the operation

Solution:

Under Configure
under Permissions to other applications

Add application - Windows Azure Active Directory
Give the following permissions

Read directory data
Sign in and read user profile


Stack Trace : 

Microsoft.Azure.ActiveDirectory.GraphClient.AuthorizationException was unhandled by user code
  HResult=-2146233088
  Message=Insufficient privileges to complete the operation.
  Source=Microsoft.Azure.ActiveDirectory.GraphClient
  Code=Authorization_RequestDenied
  ErrorMessage=Insufficient privileges to complete the operation.
  ResponseUri=https://graph.windows.net//users/?api-version=2013-11-08
  StackTrace:
       at Microsoft.Azure.ActiveDirectory.GraphClient.ConnectionWrapper.InvokeNetworkOperation[T](Func`1 action)
       at Microsoft.Azure.ActiveDirectory.GraphClient.ConnectionWrapper.DownloadData(String address, WebHeaderCollection additionalHeaders)
       at Microsoft.Azure.ActiveDirectory.GraphClient.ConnectionWrapper.DownloadData(Uri address, WebHeaderCollection additionalHeaders)
       at Microsoft.Azure.ActiveDirectory.GraphClient.GraphConnection.GetCore(Type objectType, String objectId, FilterGenerator filterGenerator, Uri& requestUri)
       at Microsoft.Azure.ActiveDirectory.GraphClient.GraphConnection.Get(Type objectType, String objectId, LinkProperty expandProperty)
       at Microsoft.Azure.ActiveDirectory.GraphClient.GraphConnection.Get(Type objectType, String objectId)
       at Microsoft.Azure.ActiveDirectory.GraphClient.GraphConnection.Get[T](String objectId)
       at Readify.Sudo.Web.Infrastructure.Identity.GraphClaimsAuthenticationManager.Authenticate(String resourceName, ClaimsPrincipal incomingPrincipal) in
       at System.IdentityModel.Services.WSFederationAuthenticationModule.SignInWithResponseMessage(HttpRequestBase request)
       at System.IdentityModel.Services.WSFederationAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs args)
  InnerException: