Tuesday, August 30, 2016

Sharepoint : Get list item Attachment by Name using JSOM

Scenario : Suppose you want to get the attachment from a list item and you only know its name .



Solution :You can achieve this using a simple JSOM code as shown below . the major player in this code is the line "splistItem.get_attachmentFiles().getByFileName("FILENAME"));"




function getAttachmentsDetails()
{

var clientContext = SP.ClientContext.get_current();
var splist = clientContext.get_web().get_lists().getByTitle("LISTNAME");
var splistItem = splist.getItemById(ID);
clientContext.load(splistItem);

// gets the item by attachment file name
clientContext.load(splistItem.get_attachmentFiles().getByFileName("FILENAME"));


clientContext.executeQueryAsync(Function.createDelegate(this, Success), Function.createDelegate(this, Fail));
 
function Success(sender, args) {
    var attachitem = splistItem;
    var total = attachitem.get_attachmentFiles().get_count();
    if (total > 0) {
        console.log(total + " file attachments"); // count will give the attachment count
    }      
}
 
function Fail(sender, args) {
    //failed function
}
}
That's All! SharePoint All the Way !!!


No comments:

Post a Comment

Rename a Web Application in Sharepoint : A solution using powershell

Here is a small script that will enable you all to change a SharePoint Web Application name . We can use the following SharePoint Pow...