0

I want to show the associated attachment/s with a particular blog on "Post.aspx" page in SharePoint 2013. Is there any way to meet this?

2 Answers 2

1

You can make a REST call it get attachments from the list item. You can also utilize JSLink's postRender method to do the query.

var itemId = location.search.split('ID=')[1];

$(document).ready(function(){
    var p = $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Posts')/items(" + itemId + ")?$select=ID,Attachments,AttachmentFiles&$expand=AttachmentFiles",
        type: "GET",
        headers: { "ACCEPT": "application/json;odata=verbose" }
    });

    p.done(function (data) {
        var item = data.d;
        var str = "";
        if (item.Attachments && item.Attachments == true) {
            str += "<div class='attachments'>";
            var attachmentsArr = item.AttachmentFiles.results;
            for (var i = 0; i < attachmentsArr.length; i++) {
                str += "<i class='fa fa-paperclip'></i> <a href='" + attachmentsArr[i].ServerRelativeUrl + "'>" + attachmentsArr[i].FileName + "</a>";
                if (i != attachmentsArr.length - 1) {
                    str += "<br/>";
                }
            }
            str += "</div>";
        }

        $("div.ms-blog-postBody").prepend(str);
    });
});
1
  • I used ClientContext instead Rest API call to fetch attachments and it worked successfully. I will also try your code. Commented Jan 14, 2017 at 0:50
0
function BlogAttachmentMethod() {    
    var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);

    var web = clientContext.get_web();
    attachmentFolder = web.getFolderByServerRelativeUrl('Lists/Posts/Attachments/' + postID);
    clientContext.load(attachmentFolder);

    clientContext.executeQueryAsync(Function.createDelegate(this, function () {
        attachmentFiles = attachmentFolder.get_files();
        clientContext.load(attachmentFiles);

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }), Function.createDelegate(this, this.onQueryFailed));

    setTimeout(function () {
        //id of div to which attachments need to bind
        var parent = document.getElementById("commandBar0").parentElement;
        var div = document.createElement("div");
        div.id = "divAttachments";
        div.innerHTML = orderList;

        parent.appendChild(div);
    }, 4000);
}

function onQuerySucceeded(sender, args) {    
    orderList = "<span><b>Attachments</b></span>";
    orderList += "<ul>";
    var count = attachmentFiles.get_count();
    for (var i = 0; i < count; i++) {
        var serverRelativeURL = attachmentFiles.itemAt(i).get_serverRelativeUrl();
        var fileName = serverRelativeURL.substring(serverRelativeURL.lastIndexOf("/") + 1, serverRelativeURL.length);
        //alert(fileName);
        orderList += "<li><a href='" + serverRelativeURL + "'>" + fileName + "</a></li>";
    }
    orderList += "</ul>";    
}

function onQueryFailed(sender, args) {
    orderList = "";
    //alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.