Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a repeater in a user control and I databind it in code behind like so:

                var items = list.Items.Cast<SPListItem>()
                .OrderByDescending(a => a[SPBuiltInFieldId.Modified])
                .Where(a => a.HasPublishedVersion)
                .Select(a => new
                {
                    Title = a.Title,
                    Comments = a[FieldNames.Comments],
                    RollupImage = a[FieldNames.RollupImage],
                    PageContent = a[FieldNames.PageContent],
                    NewsDate = a[SPBuiltInFieldId.Modified],
                    Url = SPUtility.ConcatUrls(web.Url, a.Url)
                }).ToList().Take(maxItems);


                rptNews.DataSource = items;
                rptNews.DataBind();

I now try to do the same but with REST and jQuery instead, I got it working for normal text fields but when I try to get the URL and RollupImage I get into trouble ("undefined").

$("#<%=lblReadMore.ClientID%>").click(function () {
    var displayedItems = $(".news-item").length;
    var url = L_Menu_BaseUrl + "/content/_vti_bin/ListData.svc/Pages?$skip=" + displayedItems + "&top=3?$orderby=Modified";

    $.getJSON(url,
     function (data) {
         $.each(data.d.results,
          function (i, result) {
              $('<div class="news-item noindex"><table><tr><td class="news-item-image">' + result.PublishingRollupImage + '</td><td class="news-item-content"><h2>' + result.Title + '</h2><p>' + result.Comments + '<a class="moreButton" href="' + result.Url + '">Read More</a></p></td></tr></table></div>').appendTo("#HomePageNews");
          }
         );
     });

});

The internal name for rollupimage is PublishingRollupImage but when I use this I get undefined.

Also, in the C# code I filter on a.HasPublishedVersion, is this taken care of automatically with the REST API or do I need a check for this?

Thanks in advance.

Edit: Updated code:

function GetImage(id) {
    var ctx = new SP.ClientContext("/content/");
    var targetList = ctx.get_web().get_lists().getByTitle('Pages');
    targetListItem = targetList.getItemById(id);
    ctx.load(targetListItem, 'PublishingRollupImage');
    ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
    alert(targetListItem.get_item('PublishingRollupImage'));
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

function LoadAllNews() {
    $('.news-item').remove();
    var siteUrl = L_Menu_BaseUrl + "/content/";
    var listUrl = siteUrl + "_vti_bin/ListData.svc/Pages?$filter=ApprovalStatus eq '0'";

    $.getJSON(listUrl,
     function (data) {
         $.each(data.d.results,
          function (i, result) {
            GetImage(result.Id);
              $('<div class="news-item noindex"><table><tr><td class="news-item-image"></td><td class="news-item-content"><h2>' + result.Title + '</h2><p>' + result.Comments + '<a class="moreButton" href="' + result.Path + "/" + result.Name + '"> Read More</a></p></td></tr></table></div>').appendTo("#HomePageNews");
          }
         );
     });
}
share|improve this question
    
OK I figured Url out by using firebug so it's result.Path + "/" + result.Name –  John Oct 2 '12 at 2:27

1 Answer 1

up vote 3 down vote accepted

Would it be possible to use the Client Object Model?

This works for me:

var ctx = new SP.ClientContext();
var items = ctx.get_web().get_lists().getByTitle('Pages').getItems(new SP.CamlQuery());
ctx.load(items);
ctx.executeQueryAsync(function() {
  // Get the first items rollup image, just as an example
  var rollupImage = items.getItemAtIndex(0).get_item('PublishingRollupImage');
  console.log(rollupImage);
});
share|improve this answer
    
I tried to use something like GetImage(result.Id) in my function and a function with your code above but I'm not sure how to return the URL for each item, I only get "Uncaught Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested." Updated my question –  John Oct 2 '12 at 8:18
1  
When I run that code I get the URL. But perhaps you got a lot of data, maybe you must do a items.getItemAtIndex(0).get_item('PublishingRollupImage');ctx.load();ctx.execut‌​eQueryAsync(function(){console.log(items.getItemAtIndex(0).get_item('PublishingRo‌​llupImage'))}) after the comment (// Get the first...) –  eirikb Oct 2 '12 at 9:05
    
I get the URL when I run the code but now when I pass on the ID from another method and try to return the URL to the calling method. It works fine to use alert('..') but not to return a value. –  John Oct 2 '12 at 9:31
    
You can't return a value, it's asynchronous, you need to write a callback. Like: function Get(cb){var ctx=...; ctx.executeQueryAsync(function(){ var rollupImage = ...; cb(rollupImage);})} –  eirikb Oct 2 '12 at 10:10
    
Thanks for your help eirikb, do you have a link to somewhere where I can read more about async and callback with the SharePoint 2010 client object model? –  John Oct 2 '12 at 23:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.