1

I have a query:

function consultaPropostas() {

    var customlist ;
    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();
    var oList = oWebsite.get_lists().getByTitle( 'WO_Vendor_Proposal' );    
    var camlQuery = new SP.CamlQuery();

    camlQuery.set_viewXml( '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
       '<Value Type=\'Number\'>1</Value></Geq></Where></Query></View>' );

    this.customlist = oList.getItems( camlQuery );
    clientContext.load( this.customlist );
    clientContext.executeQueryAsync(            
        Function.createDelegate( this, onQuerySucceededConsultaPropostas ),
        Function.createDelegate( this, onQueryFailedConsultaPropostas ));

}
function onQuerySucceededConsultaPropostas() 
{
    try{
        var listPropostas = [];
        var getCampo3 = 'WO_x0020_ID';
        var listItemEnumerator = this.customlist.getEnumerator();

        while ( listItemEnumerator.moveNext() ) 
        {
            var oListItem = listItemEnumerator.get_current();
            if( oListItem.get_item( getCampo3 ) != null )
                if( oListItem.get_item( getCampo3 ).$M_1.toString().localeCompare( urlParams['ID'] ) === 0 )
                    listPropostas.push( {   ID: oListItem.get_item( "ID" ), 
                                            Vendor: ( ( isNull( oListItem.get_item( "Vendor" ) ) ) ? "" : oListItem.get_item( "Vendor" ) ),
                                            CostEffort: ( ( isNull( oListItem.get_item( "Cost_x0020_Effort_x0020__x0028_V" ) ) ) ? "" : oListItem.get_item( "Cost_x0020_Effort_x0020__x0028_V" ) ),
                                            ManpowerEffort: ( ( isNull( oListItem.get_item( "Manpower_x0020_Effort_x0020__x00" ) ) ) ? "" : oListItem.get_item( "Manpower_x0020_Effort_x0020__x00" ) ),
                                            StartDate: ( ( isNull( oListItem.get_item( "Start_x0020_Date_x0020__x0028_Ve" ) ) ) ? "" : oListItem.get_item( "Start_x0020_Date_x0020__x0028_Ve" ) ),
                                            EndDate: ( ( isNull( oListItem.get_item( "End_x0020_Date" ) ) ) ? "" : oListItem.get_item( "End_x0020_Date" ) ),
                                            Declined: ( ( isNull( oListItem.get_item( "Declined" ) ) ) ? "" : oListItem.get_item( "Declined" ) ),
                                            NrVersion: ( ( isNull( oListItem.get_item( "NrVersion" ) ) ) ? "" : oListItem.get_item( "NrVersion" ) ) } );
        }


   }catch(err){
        alert( err );
    }
 }

function onQueryFailedConsultaPropostas( sender, args ) { alert( "Failed" ); }

And now I want to get the links of attachments..
I tryed oListItem.get_item( "Attachments" ) but it only returns true.
Is it possible to get link of Attachments?

4

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

How to retrieve list items with attachments using JSOM

function getListItems(listTitle,success,error)
{ 
   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)'); //Note: AttachmentFiles is requested explicitly
   ctx.executeQueryAsync(
     function() {
       success(items);  
     },
     error
   );
}

Usage

The following example demonstrates how to print attachment urls for list items:

var listTitle = 'Contacts';
getListItems(listTitle,
  function(items){
     //print items
     for(var i = 0; i < items.get_count(); i++){
        var item = items.getItemAtIndex(i);

        //print attachments
        var attachments = item.get_attachmentFiles();
        for(var j = 0; j < attachments.get_count(); j++){
            var attachment = attachments.getItemAtIndex(j);
            console.log(attachment.get_serverRelativeUrl());
        }   
     }
  },
  function(sender,args){
      console.log(args.get_message());
  });
  • it gives me 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 error after adding ctx.load(items,'Include(AttachmentFiles)'); line. Any suggestion? – Trimantra Software Solution Jun 1 '17 at 7:02
1

Even you can use SpServices, its gives a easy way to get the attachments from lists on the basis of item ID.

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.