Archive

Posts Tagged ‘jsom’

SharePoint – JSOM Sample for SP.AttachmentCollection.getByFileName Method

Today I found a question in StackExchange.

http://sharepoint.stackexchange.com/questions/136304/fetch-list-item-attachment-by-name

The OP was asking how to use this method using Client Side Object Model. Since this is not available in MSDN (https://msdn.microsoft.com/en-us/library/office/jj245502.aspx) I thought of creating it.

Here is the sample – You should change the LISTNAME, ID & FILENAME

var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle("LISTNAME");
var listItem = list.getItemById(ID);
clientContext.load(listItem);
clientContext.load(listItem.get_attachmentFiles().getByFileName("FILENAME"));
clientContext.executeQueryAsync(Function.createDelegate(this, onSuccess), Function.createDelegate(this, onFailure));

function onSuccess(sender, args) {
	var item = listItem;
	var total = item.get_attachmentFiles().get_count();
	if (total > 0) {
		console.log(total + " file attachments");
	}		
}

function onFailure(sender, args) {
	//failed
}
Advertisements