I have a requirement where I have to perform CRUD operation on SharePoint 2016 List and Libraries using the REST API from JavaScript. The problem is the application from where I need to make the calls is a .net application outside of SharePoint and is not even in the same server. I am aware of SharePoint REST API and have used it but with SharePoint. I have no idea how to use it outside of SharePoint. When I am trying to make the calls to SharePoint site it showing the Error 404 unauthorized because I am not able to authenticate the current user which was not required within the SharePoint.

If this is possible please help me to solve the problem and if possible please attach a code snippet. And if it is not possible what will be the recommended way to perform the CRUD operation on SharePoint List/Library from outside SharePoint. For e.g. creating a .net web api or something like that

Any help would be appreciated.

Issue Updated

So it all has boil down to this. I have a page(in any language but outside sharepoint) and a Javascript file linked to it. Is there any possible way to perform SharePoint Operation from it.

var url = "https://mysite.sharepoint.com" + "/_api/web/currentuser";
            $.ajax({
                url: url,
                headers: {
                    Accept: "application/json;odata=verbose"
                },
                async: false,
                success: function (data) {
                    var items = data.d;
                    console.log("Login Name: "+items.LoginName);
                    console.log("Email: " + items.Email);
                    console.log("ID: "+items.Id);
                    console.log("Title: "+items.Title);
                },
                error: function (jqxr, errorCode, errorThrown) {
                    console.log(jqxr.responseText);
                }
            });

When I run this code it gives me two errors 1. 401 Unauthorised 2. No 'Access-Control-Allow-Origin' header is present on the requested resource.

But this code is tried and tested in SharePoint Environment and it works fine. So is there any way I can run the above code from JavaScript otherwise the basic fundamentals of Rest Api fails here.

有帮助吗?

解决方案

Finally got it working. Below is the AJAX call to sharepoint. I am using the SharePoint 2013 server with AD authentication

function readSPRestApi()
        {
            $.support.cors = true;
            var url = "https://mysite.sharepoint.com" + "/_api/web/currentuser";
            $.ajax({
                url: url,
                headers: {
                    Accept: "application/json;odata=verbose"
                },
                xhrFields: { withCredentials: true },
                async: false,
                success: function (data) {
                    var items = data.d;
                    console.log("Login Name: " + items.LoginName);
                    console.log("Email: " + items.Email);
                    console.log("ID: " + items.Id);
                    console.log("Title: " + items.Title);
                },
                error: function (jqxr, errorCode, errorThrown) {
                    console.log(jqxr.responseText);
                }
            });

        }

After this I enabled the CORS in the SharePoint by adding the following code to the web.config for my web application.

<add name="Access-Control-Allow-Origin" value="http://localhost:62747" />
<add name="Access-Control-Allow-Headers" value="Content-Type,Accept,X-FORMS_BASED_AUTH_ACCEPTED,crossDomain,credentials " />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />

The above steps can also be done through powershell or c#. Refer LINK .

After these steps the Unauthorized (Error 404) and CORS error was resolved. But the data was still not coming as the headers we add in the web.config were coming two times in response from the SharePoint (still don't know why).However, the data was coming from the server and can be verified under the response in the Network tab in the Chrome Developer tool. So for that we have to disable the "Request Management" service on the SharePoint server. Following are the steps to do it.

Go to

Central Admin-->Application Management-->Manage services on server (under Service Applications)--> and Stop the Request Management service.

If you don't have the access to Central Admin refer this Link to get it done through PowerShell.

Happy Coding..!!

其他提示

Via JavaScript, I dunno how to connect to SP outside SP. but you can create a windows application using C# CSOM you can connect to SharePoint.

Check this answer for How to use Console Application for accessing SharePoint using REST API?

许可以下: CC-BY-SA归因
scroll top