1

When I try to load AJAX scripts into private site it runs with any problems, but when I do public site I have AccessException, I read a lot of it, and I found this link, where have same problem and found solution setting Barer. But I don´t know where I set barer, someone can help me where I should copy method of the solution given in link?

There is my js:

function MenuPopulate(url, listname, target) {
    var lang = "Espanol";
    if ((window.location.href.indexOf("lang=en") > 0)) {
        lang = "English";
    }
    // Getting our list items
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items?$select=Title,Enlace&$orderby=Posicion&$top=6&$filter=Idioma eq '" + lang + "'",
        method: "GET",
        crossDomain: true,
        headers: { "Accept": "application/json; odata=verbose",
         "X-RequestDigest": $("__REQUESTDIGEST").val() },
         contentType: "application/json;charset=utf-8",
        success: function (data) {
            completeMenu(data, target);
        },
        error: function (data) {
            failureMenu(data, target);
        }
    });
}

So first I think I should only add function like:

SharePointContextToken ContextToken = TokenHelper.ReadAndValidateContextToken(ContextTokenString, Request.Url.Authority);

                Uri sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);

                //Get the AccessToken
                string AccessToken = TokenHelper.GetAccessToken(ContextToken,sharepointUrl.Authority).AccessToken;

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"https://mysite-public.sharepoint.com/_api/web/lists");
                request.Method = "GET";
                request.Accept = "application/json;odata=verbose";
                request.Headers.Add("Authorization", "Bearer " + AccessToken);

                HttpWebResponse response =(HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());


function MenuPopulate(url, listname, target) {
    var lang = "Espanol";
    if ((window.location.href.indexOf("lang=en") > 0)) {
        lang = "English";
    }
    // Getting our list items
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items?$select=Title,Enlace&$orderby=Posicion&$top=6&$filter=Idioma eq '" + lang + "'",
        method: "GET",
        crossDomain: true,
        headers: { "Accept": "application/json; odata=verbose",
         "X-RequestDigest": $("__REQUESTDIGEST").val() },
         contentType: "application/json;charset=utf-8",
        success: function (data) {
            completeMenu(data, target);
        },
        error: function (data) {
            failureMenu(data, target);
        }
    });
}

This isn't working, can anyone help me there?

  • Have you already checked if current user has enough permission to list items? – buttercup Jul 28 '16 at 8:24
  • Yes, user have all permissions, I call to microsoft and they confirm I have all right with permissions and they suggest the problem is with code @buttercup – Gerry Jul 28 '16 at 15:58
0

You need the cookie as a header when performing a request to SharePoint. You don't even need the "X-RequestDigest" header when only making a "GET" request.

Just add it here:

headers: { "Accept": "application/json; odata=verbose",
           "Cookie": "FedAuth=" + FedAuth + "; rtFa=" + rtFa  }

You need to perform authentication in order to get the cookies. If you haven't done any authentication, you can try this one -> http://paulryan.com.au/2014/spo-remote-authentication-rest/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.