1

I can data from all columns but cannot get attachment. how do i get attachment and load it to image tag. Here is code i have so far to get data from list.

<script type="text/javascript">

var context = new SP.ClientContext.get_current();
debugger;
var web = context.get_web();
var list = web.get_lists().getByTitle("Mylist");


var query = new SP.CamlQuery(); //The Query object. This is used to query for data in the List

query.set_viewXml('<View><RowLimit></RowLimit>3</View>');

var items = list.getItems(query);

context.load(list); //Retrieves the properties of a client object from the server.
context.load(items);

var table = $("#tblcategories");
var innerHtml = "<tr><td>ID</td><td>Category Id</td>";

//Execute the Query Asynchronously
context.executeQueryAsync(
    Function.createDelegate(this, function () {
        var itemInfo = '';
        var enumerator = items.getEnumerator();
        debugger;
        while (enumerator.moveNext()) {
            var currentListItem = enumerator.get_current();
            innerHtml += "<tr><td>" + currentListItem.get_item('Title') + "</td><tr>";
        }
        table.html(innerHtml);
    }),
    Function.createDelegate(this, fail)
    );

function fail() {
    debugger;
    alert(arguments[1].get_message());
}

Also would like to know how can i get top rows. My camlQuery giving all records now.

1

2 Answers 2

1

Use SP.ListItem.attachmentFiles property to get the collection of attachments that are associated with the list item.

In order to load list items with attachments it have to be requested explicitly, for example:

ctx.load(items,'Include(AttachmentFiles)');

Example

The following example shows how to load list items with attachments

function getListItems(listTitle)
{ 
   var ctx = SP.ClientContext.get_current();
   var web = ctx.get_web();
   var list =  web.get_lists().getByTitle(listTitle);
   var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
   ctx.load(items,'Include(AttachmentFiles)'); 
   var deferred = $.Deferred();
   ctx.executeQueryAsync(function() {
        deferred.resolve(items);
   },
   function(sender,args){
        deferred.reject(sender, args);
   });
   return deferred.promise();
}

Usage

var listTitle = 'Tasks';
getListItems(listTitle).then(renderSlideshow,logError);

function logError(sender,args){
   console.log('An error occured: ' + args.get_message());
}

function renderSlideshow(items){
   items.get_data().forEach(function(item){
       var attachmentFiles = item.get_attachmentFiles().get_data();
       attachmentFiles.forEach(function(attachmentFile){
          var url = attachmentFile.get_serverRelativeUrl();
          var name = attachmentFile.get_fileName();
          //...
       });
   });
}
0

Regarding the top rows, order by the column which is base to identify the top rows. Update the CAML query to order by.

1

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.