Hi
All,
In
a following post , we will see how to read Attachments from List by using ECMA
Script.
Below
Method BindAttachments() is used to bind attachment to the table.
In
my Page I have defined table as follows,
<table>
<tbody id="att_row_div">
</tbody>
</table>
I
am passing listItem,web,context objects to the Method.
function BindAttachments(listItem, web, context) {
var attachmentFolder = web.getFolderByServerRelativeUrl('Lists/ListName/Attachments/' + listItem.get_id());
var attachmentFiles = attachmentFolder.get_files();
context.load(attachmentFiles);
context.executeQueryAsync(onQuerySuccess,
onQueryFailed);
function onQuerySuccess() {
var listEnumerator = attachmentFiles.getEnumerator();
var count = listEnumerator.$2K_0;
if (count > 0) {
for (var i = 0; i < count; i++) {
var attachUrl = SPHostUrl + "/Lists/ListName/Attachments/" + listItem.get_id() + "/" + attachmentFiles.itemAt(i).get_name();
var contentID = document.getElementById('att_row_div');
var newtr = document.createElement('tr');
newtr.setAttribute('id', 'AttachmentstrText' + i);
newtr.innerHTML = "<td> <a href='" + attachUrl + "'>Add</a></td>";
contentID.appendChild(newtr);
}
}
// We Can Use below Method also to get the
Attachment attachmentFiles.itemAt(i).get_serverRelativeUrl();
}
function onQueryFailed(sender, args) {
alert('Request
failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
}
We can read attachments by using below way also,
We can read attachments by using below way also,
function BindAttachments(listItem, web, context) {
var Attachments = listItem.get_attachmentFiles();
context.load(Attachments);
context.executeQueryAsync(onQuerySuccess,
onQueryFailed);
function onQuerySuccess() {
var AttachmentsEnum = Attachments.getEnumerator();
var count = 0;
while (AttachmentsEnum.moveNext()) {
count = count + 1;
var listFileItem = AttachmentsEnum.get_current();
var fileName = listFileItem.get_fileName();
var attachUrl = SPHostUrl + "/Lists/ListName/Attachments/" + listItem.get_id() + "/" +
fileName;
var contentID = document.getElementById('att_row_div');
var newtr = document.createElement('tr');
newtr.setAttribute('id', 'AttachmentstrText' + count);
newtr.innerHTML = "<td> <a href='" + attachUrl + "'>" + fileName + "</a></td>";
contentID.appendChild(newtr);
}
}
function onQueryFailed(sender, args) {
alert('Request
failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
}
Thanks.
Related Post
Apps.SharePoint 2013
- Get List Field Type using ECMA Script in SP Hosted Apps
- SharePoint 2013 AppModel Basics
- Something Went wrong error message while deploying App in SharePoint 2013
- Create Content Type using ECMA Script
- Get List Content Types using ECMA Script
- Get Lookup Column Value using ECMA Script
- Create Folder in document Library using ECMA Script in SharePoint Hosted Apps
- Remove an App from SharePoint Site
- Delete List Item Collection using ECMA Script
- XMLHttpRequest: Network Error 0x2eff, Could not complete the operation due to error 00002eff.
- Create SharePoint Hosted App in SharePoint 2013
- 101 New Features of Share Point 2013
- Creating Apps in SharePoint 2013
ECMA Script
- Get List Field Type using ECMA Script in SP Hosted Apps
- Get Choice Field Type Values using ECMA Script
- Executing Search Queries using ECMA Script in SharePoint 2013
- Create Site Column and Adding it to Content Type using ECMA Script
- Create Content Type using ECMA Script
- Get List Content Types using ECMA Script
- Get Lookup Column Value using ECMA Script
- Create Folder in document Library using ECMA Script in SharePoint Hosted Apps
- Delete List Item Collection using ECMA Script
- How to get List Items Count from SP List Using Visual webpart and ECMA script in SharePoint 2010
- How to get the List Items Count of SP List using ECMA Script in SharePoint 2010 ,2013
- SharePoint Client Object Model using ECMAScript in 2013
Nice Post... Usefull... Thank You
ReplyDeleteThank you..@Ameti Mahesh
Delete