{% fetchxml sharepointdocumentlocations %} {% endfetchxml %} {% if sharepointdocumentlocations.results.entities.size > 0 %} {% assign currentLocation = sharepointdocumentlocations.results.entities[0] %} {% assign url = currentLocation.relativeurl %} {% for counter in (0..100) %} {% if currentLocation.parentsiteorlocation %} {% fetchxml nextlocation %} {% endfetchxml %} {% if nextlocation.results.entities.size > 0 %} {% assign currentLocation = nextlocation.results.entities[0] %} {% assign url = currentLocation.relativeurl | append: '/' | append: url %} {% else %} {% fetchxml sharepointsite %} {% endfetchxml %} {% assign sharepointsiteurl = sharepointsite.results.entities[0].absoluteurl %} {% break %} {% endif %} {% endif %} {% endfor %} {% assign sharepointdomain = "" %} {% for c in sharepointsiteurl %} {% assign char = c | string %} {% assign sharepointdomain = sharepointdomain | append: char %} {% if sharepointdomain endswith 'sharepoint.com' %} {% break %} {% endif %} {% endfor %} $(document).ready(function() { DynPCA.UserId = '{{user.emailaddress1}}'; DynPCA.Auth.Params = { ClientId: 'ccbf02d7-6a9e-4d85-b9be-b359113b2a83', Tenant: 'b7d0e39e-887b-41af-93ab-53328f41531f', RedirectUri: window.location.protocol + "//" + window.location.host, ApiScopes: '{{sharepointdomain}}', }; DynPCA.Auth.Init(); DynPCA.App.ExecuteRequest( 'GET', null, 'json', true, "{{sharepointsiteurl}}/_api/web/getfolderbyserverrelativeurl('{{url}}')/files", { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" }) .done(function (data) { var table = $('').insertAfter('.entity-form'); table.append(""); $.each(data.value, function(i, e) { table.append(""); }); }).fail(function (jqXHR, textStatus, errorThrown) { alert(errorThrown); }); }); var DynPCA = { Uri: '', NewGuid: function () { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } }; DynPCA.Auth = { Client: '', Init: function () { console.log("PBAL Auth Initializing"); DynPCA.Auth.Client = new CC.CORE.PBAL.AppTokenFactory( DynPCA.Auth.Params.ClientId, DynPCA.Auth.Params.Tenant, DynPCA.Auth.Params.ApiScopes, DynPCA.Auth.Params.Policy, DynPCA.UserId, DynPCA.Auth.Params.RedirectUri, DynPCA.Uri); console.log("PBAL Auth Initialized"); }, GetHeaders: function (token, additionalHeaders) { var ajaxHeaders = {}; // check token for error if (token !== null) { ajaxHeaders = { 'Authorization': 'Bearer ' + token }; } else { console.log("token object null or missing, not including authoriztion header"); } // add additional headers if they exist if (typeof additionalHeaders === "object") { jQuery.extend(ajaxHeaders, additionalHeaders); } return ajaxHeaders; } } DynPCA.App = { BaseScripts: [ DynPCA.Uri + '/js/pbal/pbal.js' ], Init: function () { console.log("Dynamics Portal Companion App Initializing"); console.log("Dynamics Portal Companion App Initialized"); }, ExecuteRequest: function (type, data, dataType, processData, queryPath, additionalHeaders, async, requestKind) { var deferred = jQuery.Deferred(); DynPCA.Auth.Client.GetToken().always(function (accessToken) { if (requestKind == 'xml') { DynPCA.App.ExecuteXmlRequest(type, data, responseType, async, queryPath, additionalHeaders, accessToken).then(function (data) { deferred.resolve(data); }); } else { DynPCA.App.ExecuteAjaxRequest(type, data, dataType, processData, queryPath, additionalHeaders, accessToken).then(function (data) { deferred.resolve(data); }); } }); return deferred.promise(); }, ExecuteAjaxRequest: function (type, data, dataType, processData, queryPath, additionalHeaders, token) { var deferred = jQuery.Deferred(); var ajaxHeaders = DynPCA.Auth.Client.GetHeaders(token, additionalHeaders); jQuery.ajax({ method: type, url: DynPCA.Uri + queryPath, data: data, dataType: dataType, processData: processData, headers: ajaxHeaders, success: function (responseData) { deferred.resolve(responseData); }, error: function (jqXHR, textStatus, errorThrown) { deferred.reject({ xhr: jqXHR, text: textStatus, error: errorThrown }); } }); return deferred.promise(); }, ExecuteXmlRequest: function (type, data, responseType, async, queryPath, additionalHeaders, token) { var deferred = jQuery.Deferred(); var ajaxHeaders = DynPCA.Auth.Client.GetHeaders(token, additionalHeaders); var xhr = new XMLHttpRequest(); xhr.open(type, DynPCA.Uri + queryPath, async); xhr.responseType = responseType; for (var key in ajaxHeaders) { xhr.setRequestHeader(key, ajaxHeaders[key]); } xhr.onreadystatechange = function (oEvent) { if (xhr.readyState === 4) { deferred.resolve(this); } }; xhr.send(); return deferred.promise(); } } var CC = CC || {}; CC.CORE = CC.CORE || {}; CC.CORE.Cache = (function () { var defaultCacheExpiry = 15 * 60 * 1000; // default is 15 minutes var aMinuteInMs = (1000 * 60); var anHourInMs = aMinuteInMs * 60; var getCacheObject = function () { // Using session storage rather than local storage as caching benefit // is minimal so would rather have an easy way to reset it. return window.sessionStorage; }; var isSupportStorage = function () { var cacheObj = getCacheObject(); var supportsStorage = cacheObj && JSON && typeof JSON.parse === "function" && typeof JSON.stringify === "function"; if (supportsStorage) { // Check for dodgy behaviour from iOS Safari in private browsing mode try { var testKey = "candc-cache-isSupportStorage-testKey"; cacheObj[testKey] = "1"; cacheObj.removeItem(testKey); return true; } catch (ex) { // Private browsing mode in iOS Safari, or possible full cache } } CC.CORE.Log("PBAL: Browser does not support caching"); return false; }; var getExpiryKey = function (key) { return key + "_expiry"; }; var isCacheExpired = function (key) { var cacheExpiryString = getCacheObject()[getExpiryKey(key)]; if (typeof cacheExpiryString === "string" && cacheExpiryString.length > 0) { var cacheExpiryInt = parseInt(cacheExpiryString); if (cacheExpiryInt > (new Date()).getTime()) { return false; } } return true; }; var get = function (key) { if (isSupportStorage()) { if (!isCacheExpired(key)) { var valueString = getCacheObject()[key]; if (typeof valueString === "string") { CC.CORE.Log("PBAL: Got from cache at key: " + key); if (valueString.indexOf("{") === 0 || valueString.indexOf("[") === 0) { var valueObj = JSON.parse(valueString); return valueObj; } else { return valueString; } } } else { // remove expired entries? // not required as we will almost always be refreshing the cache // at this time } } return null; }; var set = function (key, valueObj, validityPeriodMs) { var didSetInCache = false; if (isSupportStorage()) { // Get value as a string var cacheValue = undefined; if (valueObj === null || valueObj === undefined) { cacheValue = null; } else if (typeof valueObj === "object") { cacheValue = JSON.stringify(valueObj); } else if (typeof valueObj.toString === "function") { cacheValue = valueObj.toString(); } else { alert("PBAL: Cannot cache type: " + typeof valueObj); } // Cache value if it is valid if (cacheValue !== undefined) { // Cache value getCacheObject()[key] = cacheValue; // Ensure valid expiry period if (typeof validityPeriodMs !== "number" || validityPeriodMs < 1) { validityPeriodMs = defaultCacheExpiry; } // Cache expiry getCacheObject()[getExpiryKey(key)] = ((new Date()).getTime() + validityPeriodMs).toString(); CC.CORE.Log("PBAL: Set in cache at key: " + key); didSetInCache = true; } } return didSetInCache; }; var clear = function (key) { var cache = getCacheObject(); if (key) { cache.removeItem(key); cache.removeItem(getExpiryKey(key)); } else { var keys = []; for (var i = 0; i < cache.length; i++) { if (cache.key(i).indexOf("candc_cache_PBAL") >= 0) { keys.push(cache.key(i)); } } for (var i = 0; i < keys.length; i++) { cache.removeItem(keys[i]); } } }; return { Get: get, Set: set, Clear: clear, IsSupportStorage: isSupportStorage, Timeout: { VeryShort: (aMinuteInMs * 1), Default: (anHourInMs * 2), VeryLong: (anHourInMs * 72), } }; })(); CC.CORE.Log = function (errMsg) { // console.log is undefined in IE10 and earlier unless in debug mode, so must check for it if (typeof window.console === "object" && typeof console.log === "function") { console.log(errMsg); } }; CC.CORE.PBAL = (function () { "use strict"; var appTokenFactory = function (aadAppClientId, tenant, b2cScope, b2cPolicy, userId, redirectUrl) { // NOTE on security: include the userId in the cache key to prevent the case where a user logs out but // leaves the tab open and a new user logs in on the same tab. The first user's calender // would be returned if we didn't associate the cache key with the current user. var cacheKey = "candc_cache_PBAL_" + userId + "_" + aadAppClientId; this.params = { clientId: aadAppClientId, tenant: tenant, redirectUrl: redirectUrl, cacheKey: cacheKey, scope: b2cScope, policy: b2cPolicy, oid: userId }; var getAuthorizeUri = function (params, redirectUrl) { var startUrl = "https://login.microsoftonline.com/te/" + params.tenant + "/" + params.policy; if (!params.policy) startUrl = "https://login.microsoftonline.com/" + params.tenant; var b2cAuthUri = startUrl + "/oauth2/authorize?" + "client_id=" + params.clientId + "&response_type=token" + "&redirect_uri=" + encodeURIComponent(redirectUrl) + "&resource=" + encodeURIComponent(params.scope) + "&response_mode=fragment" + "&state=12345" + "&nonce=12345" + "&prompt=none" + "&domain_hint=organizations" + "&login_hint=" + params.oid; return b2cAuthUri; }; var getQueryStringParameterByName = function (name, url) { name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&#]" + name + "(=([^&#]*)|&|#|$)"); var results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); }; // create iframe, set its href, set listener for when loaded // to parse the query string. Deferred returns upon parse of query string in iframe. var acquirePassiveToken = function (params) { var deferred = jQuery.Deferred(); // create iframe and inject into dom var iframe = jQuery("
Document
" + e.Name + "View