Tuesday, October 27, 2015

SharePoint 2013 Use the cross-domain library in a tenant-scoped app (REST)

SharePoint 2013 Use the cross-domain library in a tenant-scoped app (REST)

Step1: vs2012 -> file -> new -> project.
Step2: templates -> visual c# -> office/sharepoint -> app -> app for sharepoint -> name (CrossDomainTenantScopeREST)
 Step3: CrossDomainTenantScopeREST -> Scripts -> right click -> add -> new item -> Visual C# items -> web -> javascript file -> Name (CrossDomainExec.js)
Open CrossDomainExec.js file and add below script
--------------------------------------------------------------------
var hostweburl;
var appweburl; 
$(document).ready(function () {
    hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    var scriptbase = hostweburl + "/_layouts/15/";
    $.getScript("/_layouts/15/SP.RequestExecutor.js");
}); 
function execCrossDomainRequest() {
    var executor;
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url: appweburl + "/_api/SP.AppContextSite(@target)/web?@target='" + document.getElementById("sitecoll1").value + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
        }
    );
} 
function successHandler(data) {
    document.getElementById("WebTitles").innerHTML = "";
    var jsonObject = JSON.parse(data.body);
    var oli = document.createElement("li");
    oli.innerText = jsonObject.d.Title + " (" + jsonObject.d.Url + ")";
    document.getElementById("WebTitles").appendChild(oli);
}

function errorHandler(data, errorCode, errorMessage) { 
    var oli = document.createElement("li");
    oli.innerText = "Could not complete cross-domain call: " + errorMessage;
    document.getElementById("WebTitles").appendChild(oli);
} 
function getQueryStringParameter(paramToRetrieve) {
    var params = document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}
--------------------------------------------------------------------
Step4:
CrossDomainTenantScopeREST -> pages -> default.aspx
--------------------------------------------------------------------
<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" MasterPageFile="~masterurl/default.master" Language="C#" %> 
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
    <script type="text/javascript" src="/_layouts/15/sp.js"></script>
    <meta name="WebPartPageExpansion" content="full" />
    <link rel="Stylesheet" type="text/css" href="../Content/App.css" />
    <script type="text/javascript" src="../Scripts/App.js"></script>
    <script type="text/javascript" src="../Scripts/CrossDomainExec.js"></script>
</asp:Content>
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
    Page Title
</asp:Content>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <div>
        <p id="message">
            initializing...
        </p>
    </div>
    <br />
    <p>
        Site collection 1:&nbsp;<input id="sitecoll1" type="text" style="width: 400px;" />
        <br />
        <input value="Go" type="button" onclick="execCrossDomainRequest();" />
    </p>
    Web titles:<br />
    <ul id="WebTitles"></ul>
</asp:Content>
--------------------------------------------------------------------
Step5: deploy (F5)
App gives below error message. because app don't have permission to read site collection data.
Step6: To privide permission to app, go to CrossDomainTenantScopeREST -> AppManifest.xml (double click) -> permissions ->
Scope: Tenant
Permission: Read
and deploy (F5) now again.
 
Thank You.

6 comments:

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   ...

Popular posts