1

I need to pull/post data ( list items ) from/to SharePoint online to/from another environment like HTML, PHP that hosted externally or in WordPress to share it with the public.

I tried this JS code but unfortunately it is not working ... could you please tell me where is the problem :

$(function (){
    function retrieveListItem()

    })

    function retrieveListItem(){


var siteURL =new SP.ClientContext('https://******.sharepoint.com');
var fullUrl = siteURL+"/_api/web/lists/GetByTitle('Cities')/items";

        $.ajax({
            url: fullUrl,

            type:"GET",
            headers:{

                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
            },

            seccess: onQuerySucceeded,
            error: onQueryFaild

        });


    }

    function  onQuerySucceeded(data){

        var listItemInfo='';
        $.each(data.d.results, function (key, value){
            listItemInfo +='<strong>ID:' +value.Title+'<br />'


        });

        $("#divListItems").html(listItemInfo);

    }

    function onQueryFaild(sender, args){

        alert('Error!');

    }

HTML code :

<div id="divListItems">


</div>
5
  • Could you tell us what error you are getting?
    – Anil Pal
    Commented Jul 9, 2018 at 12:17
  • show nothing .. the items not retrieved
    – Alien
    Commented Jul 9, 2018 at 12:25
  • Check console if you are getting any error there
    – Anil Pal
    Commented Jul 9, 2018 at 12:27
  • try var siteURL ="https://******.sharepoint.com";
    – Anil Pal
    Commented Jul 9, 2018 at 12:28
  • no need of SP.ClientContext
    – Anil Pal
    Commented Jul 9, 2018 at 12:28

3 Answers 3

2

You could directly call Rest API and then append item in a html table within the div like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function(){
    $("#btnClick").click(function(){
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Cars')/items";
           $.ajax({
              url: requestUri,
              type: "GET",
              headers: {
                  "accept":"application/json; odata=verbose"
              },
              success: onSuccess,
              error: onError
});

  function onSuccess(data) {
     var objItems = data.d.results;
     var tableContent = '<table id="tableCars" style="width:100%" border="1 px"><thead><tr><td>Make</td>' + '<td>Model</td>' + '<td>Price</td>' + '<td>Color</td>' + '</tr></thead><tbody>';

     for (var i = 0; i < objItems.length; i++) {
         tableContent += '<tr>';
         tableContent += '<td>' + objItems[i].Make  + '</td>';
         tableContent += '<td>' + objItems[i].Model + '</td>';
         tableContent += '<td>' + objItems[i].Price + '</td>';
         tableContent += '<td>' + objItems[i].Color + '</td>';
         tableContent += '</tr>';
 }
   $('#carsGrid').append(tableContent);
   }
    function onError(error) {
        alert('Error');
   }
  });
});

</script>
<input type="button" id="btnClick" value="Get All Cars"/>
<div id="CustomerPanel">
   <table id='tableCars' style="width: 100%;" border="1 px">
     <tr>
       <td>
          <div id="carsGrid" style="width: 100%"></div>
      </td>
    </tr>
 </table>
</div>

enter image description here

Reference:

SHAREPOINT ONLINE, 2013 AND 2016 – DISPLAY LIST DATA IN GRID USING REST API

1
  • your solutions work correctly if I retrieve the items in SharePoint environment, but what I want is retrieving the items to an HTML that hosted externally.
    – Alien
    Commented Jul 11, 2018 at 7:56
1

First of all I see duplicate function retrieveListItem. Use SP only when context is loaded, use this line to call your function

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', YourMethod());

And when loading SP context you don't need to connect to url. Because js file is already in SharePoint

SP.ClientContext.get_current()
3
  • I want to retrieve the items to an external environment not inside SharePoint
    – Alien
    Commented Jul 11, 2018 at 9:25
  • Why? What's the purpose of this? You cannot add data to element from other site. You can save those items to your local pc using PowerShell or create html with it.
    – Luke359
    Commented Jul 12, 2018 at 7:01
  • I need to publish list items to public, since the public have no ability to access SharePoint online, so I want to retrieve list items to another environment such as WordPress through using HTML/PHP .. if you have better idea please share me.
    – Alien
    Commented Jul 12, 2018 at 14:04
1

You have two options to query content from another site:

  1. Use server side CSOM calls (e.g. C#) to query the SharePoint API
  2. Issue a client-side Cross-Domain Request (CORS), which requires CORS to be properly configured on your SharePoint site as well as the client "host" site.

I won't get into the details of enabling CORS since you can easily find more in-depth articles on that elsewhere for your specific setup. It can be challenging to get started but it's definitely doable.

If you have access to the server side and are familiar with server-side coding against SharePoint, I would recommend that route.

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.