0

I'm using JSOM to get the attachment file from SharePoint. These are my lines of code:

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var oListActiveMatter = web.get_lists().getByTitle("ABC List");
var camlQuery = new SP.CamlQuery();    
var viewXml = String.format("<View><Query><Where><Eq><FieldRef Name='ABCNumber' /><Value Type='Text'>{0}</Value></Eq></Where></Query></View>", "1232");
camlQuery.set_viewXml(viewXml);
var oListItems = oListActiveMatter.getItems(camlQuery);
ctx.load(oListItems);
ctx.executeQueryAsync(function () {
    var oListItemEnumerators = oListItems.getEnumerator();
    while (oListItemEnumerators.moveNext()) {
        var item = oListItemEnumerators.get_current(); 
        //debugger;
        var attachmentFiles = item.get_attachmentFiles()
        ctx.load(attachmentFiles);
        ctx.executeQueryAsync(function () {
            debugger;
            if(attachmentFiles.get_count() > 0){
                var attachmentsItemsEnumerator = attachmentFiles.getEnumerator();
                while (attachmentsItemsEnumerator.moveNext()) {
                    var item = attachmentsItemsEnumerator.get_current(); 
                    var fileName = item.get_fileName();
                    var filepath = item.get_path();
                    var serverPath = item.get_serverRelativePath();
                    var serverUrl = item.get_serverRelativeUrl();
                    var objetctData = item.get_objectData();
                    var typedObj = item.get_typedObject();
                }
            }
        });
    }
    console.log(oListItems.get_count());
});

But I don't find properties as: author, created date, type of file, etc... How can I get them? Please help!

1

Try below code:

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var oListActiveMatter = web.get_lists().getByTitle("Test");
var camlQuery = new SP.CamlQuery();    
var viewXml = String.format("<View><Query><Where><Eq><FieldRef Name='ABCNumber' /><Value Type='Text'>{0}</Value></Eq></Where></Query></View>", "1232");
camlQuery.set_viewXml(viewXml);
var oListItems = oListActiveMatter.getItems(camlQuery);
ctx.load(oListItems);
ctx.executeQueryAsync(function () {
    var oListItemEnumerators = oListItems.getEnumerator();
    while (oListItemEnumerators.moveNext()) {
        var item = oListItemEnumerators.get_current(); 
        var createdBy = item.get_item('Author');
        var createdDate = item.get_item('Created');
        var modifiedBy = item.get_item('Editor');
        var modifiedDate = item.get_item('Modified');
        //debugger;
        var attachmentFiles = item.get_attachmentFiles();
        ctx.load(attachmentFiles);
        ctx.executeQueryAsync(function () {
            debugger;
            if(attachmentFiles.get_count() > 0){
                var attachmentsItemsEnumerator = attachmentFiles.getEnumerator();
                while (attachmentsItemsEnumerator.moveNext()) {
                    var attachitem = attachmentsItemsEnumerator.get_current(); 
                    var fileName = attachitem.get_fileName();
                    var filepath = attachitem.get_path();
                    var serverPath = attachitem.get_serverRelativePath();
                    var serverUrl = attachitem.get_serverRelativeUrl();
                    var objetctData = attachitem.get_objectData();
                    var typedObj = attachitem.get_typedObject();
                }
            }
        });
    }
    console.log(oListItems.get_count());
});
2
  • If I login will other account and attach other file. I think it doesn't work – LazyCatIT Jan 23 '17 at 8:15
  • Works for me, just ensure that account has sufficient permission(at least read permission) on the list. – Gautam Sheth Jan 23 '17 at 8:26
2

You can get the details by specifying the internal names of these fields, like item.get_item('Editor') will give you the who modified and item.get_item('Author') will give you who created the item.

3
  • Uncaught TypeError: item.get_item is not a function. This function doesn't exist – LazyCatIT Jan 23 '17 at 6:21
  • The code in the answer will give you the details of the list item which contains the attachments. Write it before the comment //debugger in your code. – Nadeem Yousuf-AIS Jan 23 '17 at 6:51
  • With the case other user can attach file, it will doesn't work – LazyCatIT Jan 23 '17 at 8:13
2

It is very easy to get all properties of ListItem attachments using REST API. Below is the URL for getting all info using REST API.

siteURL + _api/lists/getByTitle('MyList')/items?$select=AttachmentFiles,Title&$expand=AttachmentFiles

You can expand columns as per your requirement in REST API.

2
  • It's good idea. But I want get them with JSOM. Can you suggest any ideas for me? – LazyCatIT Jan 23 '17 at 6:23
  • 2
    Can you please include properties in load method as shown below. Use ctx.load(attachmentFiles,'Include(Created,ContentTypes)'); instead of ctx.load(attachmentFiles); in your code. – Samir Khimani Jan 23 '17 at 9:16

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.