how to get list item attachments using client object model ecma lib
-
Wednesday, August 25, 2010 12:53 PM
Hi,
I'd like to know how to get the attachments paths of a list item using the client object model ecma library.
context = new SP.ClientContext.get_current();
web = context.get_web();
var list = web.get_lists().getByTitle('ListName');
itemToShow = list.getItemById(ItemID);
context.load(itemToShow, 'Title','Attachments');
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));The attachments field contains TRUE if the item has attachments or FALSE if not, but what I need is the path of the attachments..
Thanks.
- Moved by Wayne Fan Friday, August 27, 2010 8:21 AM (From:SharePoint 2010 - Using SharePoint Designer, Infopath, and other customization)
All Replies
-
Wednesday, August 25, 2010 4:25 PMFor development questions, please use the development forum. Thanks!
SharePoint Architect || Microsoft MVP || My Blog
Planet Technologies || SharePoint Task Force -
Saturday, August 28, 2010 10:50 AM
Hi Ghiluz,
Thanks for your post.
You can get the path using “SP.Folder” object instead of “SP.ListItem”. The attachment files are stored in the list as the structure as follows:
List rootfoler à ‘attachment’ folder à ‘Item Id’ folder à attachments.
For example, the path of the attachment in my server likes this: http://<servername>/lists/listtest/Attachments/1/doctext.docx.
So the main logic of my solution is:
1. Get the folder of the url likes : var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId);
2. Loop all the files in the folder, and get the path of them using get_serverRelativeUrl;
Here is my sample code, hope it’s helpful.
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getWebProperties, "SP.js");
var attachmentFiles;
function getWebProperties() {
var itemId=2;
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/City/Attachments/'+itemId);
attachmentFiles= attachmentFolder.get_files();
ctx.load(attachmentFiles);
ctx.executeQueryAsync(Function.createDelegate(this,this.onSuccess),Function.createDelegate(this,this.onFailed));
}
function onSuccess(sender, args) {
var i=0;
for(var file in attachmentFiles)
{
alert(attachmentFiles.itemAt(i).get_serverRelativeUrl());
i++;
}
}
function onFailed(sender, args) {
alert("sorry!");
}
</script>
- Marked As Answer by Ghiluz Monday, August 30, 2010 10:00 AM
-
Monday, August 30, 2010 9:59 AM
Hi, it works great!!
thanks a lot
-
Tuesday, June 12, 2012 9:29 PM
it worked in IE but not in FF. Is there any reason behind this???
-
Thursday, June 21, 2012 10:11 AM
The below link will clearly explain "how to read list item attachments using client object model
http://vangalvenkat.blogspot.com/2011/08/sharepoint-2010-getting-list-item.html

