Sunday, July 27, 2014

Retrieve List item Attachments using REST API


In previous blog, I explained about adding attachments to list item using html5 and Jquery. Now let’s use some REST API to pull these attachments and display them in list.
I am using same page from previous post to display or edit list item. And MY page URL is as below

{Site URL}/SitePages/File.aspx?RID={ID}
Now on document ready event I have used below code which retrieves item as well as attachment and display in my custom form:

 getListItem function uses item ID from query string and passes CAML query to retrieve particular item

In Succeed delegate method we are iterating collection and assigning respective Tittle ,
Description values to textboxes. For retrieving attachments I am using REST API, using ListData WCF service. URL for all attachments collection can be built like below

{Site URL}/_vti_bin/ListData.svc/{List Name} ([item ID])/Attachments 
       
This returns all attachments JSON result collection object , now  get file  Name of each file by iterating through it. And we can build URL for file path as below

{Site URL}/Lists/{List Name} /Attachments/{Item ID}/{FileName}
 


















When your page will load , output will be as shown in below. Click on each attachment will open respective file.
 
 





That's it. 
 

No comments:

Post a Comment