0

I am trying to build a REST call through Javascript from Salesforce. I have been getting "No 'Access-Control-Allow-Origin' header is present on the requested resource." error. I tried to understand from CORS perspective but wondering whether any one specifically resolved this from Salesforce - SharePoint Integration perspective.

enter image description here

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script src="/soap/ajax/37.0/connection.js"></script>
<script src="/soap/ajax/37.0/apex.js"></script>
<script type="text/javascript">

function uploadFile()
{
      j$ = jQuery.noConflict();
      var listname = 'SalesforceDocuments';
      var ID = 1;
//    var fileInput = document.getElementById("file-input");
      var fileInput = j$("#file-input")[0].files[0];
      var fileName  = fileInput.name;
      alert(fileName);
      var fileCount =  j$("#file-input")[0].files.length;
      alert(fileCount);

    for(var i = 0; i < fileCount; i++){
        alert(fileCount);
        // Get the local file as an array buffer.
        var getFile = getFileBuffer(i);
        alert('getFile is'+getFile);
        getFile.done(function (arrayBuffer,i) {
          var addFile = addFileToFolder(arrayBuffer,i);

        });

    }

    function getFileBuffer(i) {
        var deferred = j$.Deferred();
        var reader = new FileReader();
        reader.onload = function (e) {
            deferred.resolve(e.target.result,i);
        }
        reader.onerror = function (e) {
            deferred.reject(e.target.error);
        }
        reader.readAsArrayBuffer(fileInput);
        return deferred.promise();
    }

    function addFileToFolder(arrayBuffer,i) {

        //construct the endpoint
        sforce.connection.sessionId = '{!$Api.Session_ID}';
        var ep = sforce.connection.query("SELECT Endpoint FROM NamedCredential WHERE DeveloperName='SPO_Integration'");
        var records = ep.getArray('records');
        var endpoint = records[0].Endpoint;
        alert(endpoint);

        // Send the request to SharePoint.
        j$.ajax({
            url: endpoint +
             "/_api/web/lists/getbytitle(\'SalesforceDocuments\')/items?",
            type: 'GET',
            crossDomain: true,
            headers: {
             "Accept": "application/json; odata=verbose",
             "Content-Type" : "application/json; odata=verbose"
            },
            success: function(data){
               alert('success');
            },
            error: function(XMLHttpRequest,textStatus,errorThrown){
                console.log(XMLHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
            ///items(" + ID + ")/AttachmentFiles/add(FileName='" + fileName + "')
    }
0

CORS is first and foremost a Server configuration setting, so is not something you set with you JavaScript code.

You can only get in the Microsoft way

These will get you started, but be sure to Google, lots of changes/additions in the past years

For SharePoint Online it is lot easier (from a PHP or Python env.):

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.