1

Is there a way to connect to SharePoint Online from Web client javascript (js-code embedded in an .aspx page).

How can I authenticate ?

How can I access list item objects such as library documents from my client JavaScript code (if possible)?

1 Answer 1

0

Sounds as if you want to connect a local ASP.NET App to your SharePoint Online (I am making a lot of assumptions in my answer based on this). If this is true, you would need to impersonate a SharePoint Online user in the application code by setting the network credentials to the username and password of the SharePoint Online user account then connect to the web services and pull the data using JSOM and LINQ to work with the results once received. I have done this approach not with SharePoint Online but with a public facing SharePoint Server that was in a different domain (Been a long while but should be same in concept). A LOT of issues to consider both with security and availability with this approach, I would make sure you must do it this way before committing to it. Below is some code to look at and examine hope it helps (Note: I would put the Username and Password in the web.config file as a app setting key value pair and encrypt/decrypt in the code for production just like you would with any SQL style external data connection, also I apologize for the weird x's in the code I had to redact it to post here):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using x_GetExecApprover.xWebService;

    namespace x_GetExecApprover
    {
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ProcedureCall(String inExecApprover, String inExecApprovalDate, String inxID)
    {
        ProcedureLbl.Text = inExecApprover + ", " + inExecApprovalDate + ", " + inxID;
    }

    protected void SubBtn_Click(object sender, EventArgs e)
    {
        try
        {
            String lclExecApprover = null;
            String lclExecApprovalDate = null;
            String lclxID = xIdTxtBx.Text;

            if (xIdTxtBx.Text.Length != 0)
            {
                xManagementApprovalSystemDataContext dc = new xManagementApprovalSystemDataContext(new Uri("https://xxx.sharepoint.com/_vti_bin/ListData.svc"));
                System.Net.NetworkCredential credentials = new NetworkCredential("UserName", "Password");
                dc.Credentials = credentials;//CredentialCache.DefaultNetworkCredentials;

                var query = from xApprovalsItem x_RowItems in dc.xApprovals
                            where x_RowItems.xID.Equals(lclxID) && x_RowItems.CurrentLocation.Equals("complete")
                            orderby x_RowItems.ExecutiveApprovalDate descending
                            select new
                            {
                                ExecApprover = x_RowItems.ExecutiveApprover,
                                ExecApprovalDate = x_RowItems.ExecutiveApprovalDate
                            };

                ResultsGrid.DataSource = query;
                ResultsGrid.DataBind();

                foreach (var item in query)
                {
                    lclExecApprover = item.ExecApprover;
                    lclExecApprovalDate = item.ExecApprovalDate;
                    break;
                }

                ProcedureCall(lclExecApprover, lclExecApprovalDate, lclxID);
            }
        }
        catch 
        {
            Response.Write("Error");
        }
    }
}
    }
4
  • Shane, many thanks for this. Yes it is correct , but the requirement is that all these things are done not from C#, but from code inside javascript tags/javascript functions connected to C# aspx-pages. I suppose that is an even worse idea, if even possible ? Commented May 19, 2016 at 8:42
  • Problem you will run into is that you have to be authenticated before running JS script against SPO see this discussion here for more info: Authenticate user in SPO using JS
    – Shane Gib.
    Commented May 19, 2016 at 14:06
  • If I am already authenticated , will the "SP" JavaScript code run seamless ? Commented May 23, 2016 at 8:04
  • I should have stated that better, the problem you will run into is the ASP.NET App has to be authenticated before running the JavaScript on that page, which is possible using the code around "System.Net.NetworkCredential credentials = new NetworkCredential("UserName", "Password");" line of code. So if you as the user log into O-365 and then browser to the internal ASP.NET App, it will still fail b/c the ASP.NET App is making the call for data, not the user that is using your internal ASP.NET App. Hope this helps.
    – Shane Gib.
    Commented May 25, 2016 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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