3

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.

  • Javascript by default doesnt allow requests to domains other thena the current domain. – Abdul Hameed Apr 18 '18 at 10:37
  • you will need to enable Access-Control-Allow-Origin in order to do that. – Abdul Hameed Apr 18 '18 at 10:38
2

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..!!

0

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?

  • Thanks abdul for the answer, but I don't think console application will solve the purpose as I need to call the code to perform sharepoint operations from another application – Ankit Katiyar Apr 18 '18 at 7:08
  • what type is this other application? Web? Windows Form? – Abdul Hameed Apr 18 '18 at 7:12
  • In the current scenario the front end application is being built in java. Now I have to make a wrapper that will call the Sharepoint API's from the front end page events (e..g button click) and will update the data in SharePoint. So I am planning to make a .net web application as a wrapper. Also will my approach solve the problem or you have a better suggestion to do it..?? – Ankit Katiyar Apr 18 '18 at 7:27
  • The answer linked in my answer can be used in any .NET application not just console application. – Abdul Hameed Apr 18 '18 at 9:05
  • I am aware of that code but thanks for sharing. One last question if I have to call RestApi from JavaScript in any technology (in current case it will be Java). Can i do that..?? – Ankit Katiyar Apr 18 '18 at 9:38

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.