none
Having problem with an App or I am doing it wrong! RRS feed

  • Question

  • Hello,
    Here is another one from a novate developer:

    The case is this: I have a developer site in SP Online which contains one subsite called Demo1 and this subsite has another one called Economy. In the Economy subsite I have a library with a couple of Word documents. This structure is the same as in the team-site. In the team-site I have a Demo1 team-site and inside of it an Economy team-site with the library.
    The confusion is this: the app is reading the information from the library only if it is installed in the site-Collection where the library lives. that means that if I want to use my app to read from Demo1/Economy/myLibrary I have to install it in the economy team-site. If I install the app in the Demo1 team-site I get an error telling me that the app can't find the library.
    So, is correct to create an app to read from a list or library which exists in the same site-Collection? I feel like it is enough with a CQWP and some custom display template. Or is something wrong in the app that is blocking it from read if it is in another team-site, say in the parent team-site.
    My url in the app is like this:

    url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Mallar')/items?@target='" + hostweburl + "/demo1/ekonomi/'&$select=EncodedAbsUrl,LinkFilename,Mall,Modified,Editor/Name&$expand=Editor/Name",

    In that url I am pointing to the library called 'Mallar' and lives in the 'ekonomi' team-site.

    Any comment is usefull right now!

    Thanks!!!!

    Tuesday, April 25, 2017 8:32 AM

Answers

  • Hi Americo,

    As you are getting data from the site in the same site collection as the host web where the app is installed, please set the app permission to be site collection read and write.

    And please also make sure that the target web URL is set correctly.

    From your code, it seems that the target URL is "hostweburl/demo3", that means the library is in a web which is a subsite of the host web of your app. But based on your description, it seems that the target web URL is not a subsite of the host web.

    Please confirm this first.

    Best Regards,

    Victoria


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, May 8, 2017 5:49 AM
    Moderator
  • Hi Americo,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    Best Regards,

    Victoria


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Americo2014 Thursday, June 1, 2017 6:49 PM
    Thursday, June 1, 2017 12:08 PM
    Moderator

All replies

  • Hi Americo,

    It seems that you got data from the host web successfully. However if you want to get data from the other sites in the same site collection as the app, then you need to use other way to get the data.

    Please check the demo in the link below:

    https://blog.ch.atosconsulting.com/data-access-in-sharepoint-hosted-apps/#accessing-data-from-same-site-collection

    Best Regards,

    Victoria

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, April 27, 2017 11:35 AM
    Moderator
  • thanks, I am gonna take a look to the link and I tell you how I am doing!!!

    Thursday, April 27, 2017 12:38 PM
  • Hi Americo,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    Best Regards,

    Victoria


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, May 4, 2017 2:40 AM
    Moderator
  • Hi, sorry for not responding earlier.

    I looked to the code that you liked and tested it but the problem remains.

    Here is an image to represent the structure of the site:

    The document library is in the subsite2 team site and I have access to it from the root team site and from the subsite2 itself. But from the subsite1 I get the corss-domain call error.

    the root team site has the cross publishing feature activated.

    My app is using Tenant read permission. And the code I am using is this:

    <%@ Page language="C#" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, 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" %>
    <%@ 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" %>
    
    <WebPartPages:AllowFraming ID="AllowFraming" runat="server" />
    
    <html>
    <head>
        <title></title>
    
        <script type="text/javascript" src="../Scripts/jquery-1.9.1.min.js"></script>
        
    
        <script type="text/javascript">
            // Set the style of the client web part page to be consistent with the host web.
            (function () {
                'use strict';
    
                var hostUrl = '';
                var link = document.createElement('link');
                link.setAttribute('rel', 'stylesheet');
                if (document.URL.indexOf('?') != -1) {
                    var params = document.URL.split('?')[1].split('&');
                    for (var i = 0; i < params.length; i++) {
                        var p = decodeURIComponent(params[i]);
                        if (/^SPHostUrl=/i.test(p)) {
                            hostUrl = p.split('=')[1];
                            link.setAttribute('href', hostUrl + '/_layouts/15/defaultcss.ashx');
                            break;
                        }
                    }
                }
                if (hostUrl == '') {
                    link.setAttribute('href', '/_layouts/15/1033/styles/themable/corev15.css');
                }
                document.head.appendChild(link);
            })();
        </script>
        <script type="text/javascript">
    
            var hostweburl;
            var appweburl;
    
            // Load the required SharePoint libraries
            $(document).ready(function () {
                //Get the URI decoded URLs.
                hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
                appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    
                // resources are in URLs in the form: web_url/_layouts/15/resource
                var scriptbase = hostweburl + "/_layouts/15/";
    
                // Load the js files and continue to the successHandler
                $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
    
            });
    
            // Function to prepare and issue the request to get SharePoint data
            function execCrossDomainRequest() {
                // executor: The RequestExecutor object Initialize the RequestExecutor with the app web URL.
                var executor = new SP.RequestExecutor(appweburl);
    
                // Deals with the issue the call against the app web.
                executor.executeAsync({
                    url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Test')/items?@target='" + hostweburl + "/demo3'&$select=LinkFilename",
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    success: successHandler,
                    error: errorHandler
                }
                );
            }
    
            // Function to handle the success event. Prints the data to the page.
            function successHandler(data) {
                var jsonObject = JSON.parse(data.body);
                var items = [];
                var results = jsonObject.d.results;
    
                console.log(results);
                $(results).each(function () {
                    var linkFileName = this.LinkFilename;
    
                    items.push('<p>' + linkFileName + '</p>');
    
                });
    
                $('#result').html(items.join(''));
    
            }
    
    
            // Function to handle the error event. Prints the error message to the page.
            function errorHandler(data, errorCode, errorMessage) {
                document.getElementById("internal").innerText = "Could not complete cross-domain call: " + errorMessage;
            }
    
            // Function to retrieve a query string value.
            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];
                }
            }
        </script>
    
    </head>
    <body>
        <div id="result"></div>
        <div id="internal"></div>
    </body>
    </html>
    

    Please note that in the url I am pointing to a subsite called "demo3" this is the subsite that in the Picture calls "Subsite2".

    What else can be wrong?

    Thanks!!

    Thursday, May 4, 2017 8:22 AM
  • Hi Americo,

    As you are getting data from the site in the same site collection as the host web where the app is installed, please set the app permission to be site collection read and write.

    And please also make sure that the target web URL is set correctly.

    From your code, it seems that the target URL is "hostweburl/demo3", that means the library is in a web which is a subsite of the host web of your app. But based on your description, it seems that the target web URL is not a subsite of the host web.

    Please confirm this first.

    Best Regards,

    Victoria


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, May 8, 2017 5:49 AM
    Moderator
  • Hi Americo,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    Best Regards,

    Victoria


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Americo2014 Thursday, June 1, 2017 6:49 PM
    Thursday, June 1, 2017 12:08 PM
    Moderator
  • Actually the solution was set the the permission to write and read. 

    The url in the code was just for reference.

    Thanks!!!

    Thursday, June 1, 2017 6:48 PM