1

I want to display a single(filtered) item from a list on my main site, to the homepage on a subsite.

I've initially tried the CQWP but it was to limiting when it came to what fields it displayed by default. So i made a script based on SPServices (and some tutorials, but they are lost to me, so i cannot reference them here yet,i'll try finding them).

The script below connects to a element based on the filtervalue in the queryUrl, and when I check the console, it does display the correct item. The script is placed directly in a script editor webpart.

So how do I display the information on the page? I tried to check the values by adding console.log(jqXHR.Title); but that just gives me undefined.

<script type="text/javascript" src="https://contoso.sharepoint.com/mainsite/SiteAssets/jquery-1.11.1.min.js"></script>
<script type="text/javascript"src="https://contoso.sharepoint.com/mainsite/SiteAssets/jquery.SPServices-2014.01.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    var queryUrl = "https://contoso.sharepoint.com/mainsite/_api/web/lists/GetByTitle('Projects')/items/?$filter=Title eq 'some title'"
    var json = $.getJSON(queryUrl,function(response,status,jqXHR){
                console.log(jqXHR.responseText);
                    });

});

</script>
2
  • A tip is to use console.dir() instead of console.log(). That way you will be able to drill down in the object in for example Chrome javascript console Jun 25 '14 at 9:06
  • Yes, tried that, and it gives me alot more info, but the json is nested inside the responseJson in an array, trying to access it with something like console.log(json.Title) just gives undefined.
    – Kembo
    Jun 25 '14 at 10:19
1

Ok, so I figured it out, or rather I rewrote the call without shorthand.

<script type="text/javascript" src="https://contoso.sharepoint.com/mainsite/SiteAssets/jquery-1.11.1.min.js">   </script>

<script type="text/javascript">
    $(document).ready(function(){    
         $.ajax({
             url:"https://contoso.sharepoint.com/mainsite/_api/web/lists/GetByTitle('Projets')/items/?$filter=Title eq 'something'",
             type:"GET",
             headers:{
                 "accept":"application/json;odata=verbose",
             },
         success:function(data){
             $.each(data.d.results,function(index, item){
                $("#mydiv").append("<p>"+index+" : "+item.value+"</p>");
             });
         },
         error: function(error){
            alert(JSON.stringify(error));
         }
    });
});
</script>

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.