Monday, March 8, 2010

Show attachments on SharePoint Discussion board Flat/Threaded views

As we all know, default OOB SharePoint Discussion Boards does not show attachments on Flat/Threaded views. If the user wants to view the attachments he had to click on "View Properties" and then view the attachments which is very inconvenient for the user. Moreover the attachment icon beside "View Properties" link is very small, hard to notice and is not linked to attachment. Due to this problem many of my business users were missing the attachments which are attached with the discussion. Due to this problem I had to find a way to show attachments within the Flat/Threaded views along with the content like this.


I started thinking for a way to implement it. Searched Google but couldn't find any solutions and finally one good idea flashed in my mind, i.e why shouldn't I try with jQuery I thought. Started looking into jQuery to implement it and finally here is the solution below.


<script type="text/javascript" src="../../Shared Documents/jquery-1.4.2.js"></script>
<script type="text/javascript">
//This script does not include attachments ICON's
$(document).ready(function(){
$('.ms-disc-padabove img[src*="attach.gif"]').each(function(index) {
var divAttachments;
var displayPageURL = $(this).parents().eq(2).next().find('a')[0].href;
displayPageURL += " #idAttachmentsTable";
if(index == 0)
{
divAttachments = "<br/><div id='divAttachments"+index+"'></div>";
$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[1]).append($(divAttachments));
}
else
{
divAttachments = "<br/><div id='divAttachments"+index+"'></div>";
$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[0]).append($(divAttachments));
}
$("#divAttachments"+index).load(displayPageURL);
});
});

//This script includes attachments ICON's
$(document).ready(function(){
//debugger;
//To display attachments
$('.ms-disc-padabove img[src*="attach.gif"]').each(function(index) {
var divAttachments;
var displayPageURL = $(this).parents().eq(2).next().find('a')[0].href;
if(index == 0)
{
divAttachments = "<br/><div id='divAttachments"+index+"'></div>";
$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[1]).append($(divAttachments));
}
else
{
divAttachments = "<br/><div id='divAttachments"+index+"'></div>";
$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[0]).append($(divAttachments));
}
//$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div > div')[0]).append($(divAttachments));
$.get(displayPageURL, function(data){
var attachmentsTable = $(data).find('#idAttachmentsTable');
$(attachmentsTable).find('a').each(function(index) {
var fileName = $(this)[0].innerHTML;
var fileExtension = fileName.substring(fileName.indexOf('.')+1).toLowerCase();
var imgTag;
if(fileExtension == 'xls' fileExtension == 'xlsx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/icxls.gif'>&nbsp;"+fileName;
}
else if(fileExtension == 'doc' fileExtension == 'docx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/icdoc.gif'>&nbsp;"+fileName;
}
else if(fileExtension == 'ppt' fileExtension == 'pptx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICPPT'>&nbsp;"+fileName;
}
else if(fileExtension == 'txt')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ictxt.gif'>"+fileName;
}
else if(fileExtension == 'bmp')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICBMP.gif'>&nbsp;"+fileName;
}
else if(fileExtension == 'jpeg')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICJPEG.gif'>&nbsp;"+fileName;
}
else if(fileExtension == 'jpg')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICJPG.gif'>"+fileName;
}
else if(fileExtension == 'png')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICPNG.gif'>"+fileName;
}
else
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICGEN.gif'>&nbsp;"+fileName;
}
$(this)[0].innerHTML = imgTag;
});
$("#divAttachments"+index).append(attachmentsTable[0].outerHTML);
});
})
});
</script>

22 comments:

  1. Thank you very much this was quite helpful and very simple to implement

    ReplyDelete
  2. Yes, I would like to know how to implement this script, thank you!

    ReplyDelete
  3. What Happen if the load page has Jquery??
    for me is throwing me an javascript error

    ReplyDelete
  4. Hi Bharat,

    thanks for this solution! However, I noticed that small pieces are missing in the code above, namely the 'OR' in the different IF statements, e.g.
    fileExtension == 'xls' fileExtension == 'xlsx'

    ReplyDelete
  5. Can someone post step by step how to implement this?

    ReplyDelete
  6. Procedire to Implement:

    1) Go to the site http://jquery.com/ and download the latest version of JQuery file.

    2) Upload this file in the Shared Documents library. This is not mandatory to upload jquery file in Share documents library, If you decide to place it in another place please reference it accordingly as I have referenced in this statment

    <script type="text/javascript" src="../../Shared Documents/jquery-1.4.2.js"></script>

    3) Now edit the Flat/Threaded view of discussion boards and add a content editor webpart.

    4) Go to content editor webpart settings by clicking on "Modify Shared webpart" option.

    5) in the source editor paste the above script and exit the page edit mode.

    thats it it will work for you.

    Please note that as modery commented there should be an OR(||) symbol between these statements
    fileExtension == 'xls' fileExtension == 'xlsx'

    fileExtension == 'doc' fileExtension == 'docx'

    fileExtension == 'ppt' fileExtension == 'pptx'

    ReplyDelete
  7. Regarding step 3) how do you edit the threaded view? By default it says I am working in a Shared View and doesn't have any publishing options. Does it have to be done in sharepoint designer?

    ReplyDelete
  8. For step 3) Click on Site Actions --> Edit page.
    On the web part zone click on add web part to add content editor web part.

    ReplyDelete
  9. hi, your solutions sounds really nice - its that i'll use for my installation.
    But it won't workout, i have SP 2010. As i can see from you pics, youre using 2007. So have y ever tried it w 2010? All i get is an error from the content editor wp: can't call inserted Url.

    I tried it with absolute paths to jquery as well as hard coded.
    thanks, alex

    ReplyDelete
  10. Hello Bharat,

    Thanks for your post. I tried this but its not working for me in SP 2007.
    I copied script on page and also uploaded Jquery file 1.4.4 but its not working for me. :(

    Thanks
    Ankit

    ReplyDelete
  11. Ankit,

    Make sure your path to jQuery is accurate. Just do a small test to confirm if jQuery is working on your page. Once you have proper jQuery reference everything should work.

    Thanks,
    Bharat.

    ReplyDelete
  12. Works like a charm in 2007 SP2 Danish here.
    But only in IE.
    In FF it only append and empty div. Think it is the $.get() which fails.

    ReplyDelete
  13. Hi Bharat
    you rocking.... i works for me without any issue. However, I need to display all attachments for the same discussion in tool bar along with hyperlinks. Is it possible?

    vamsi

    ReplyDelete
  14. Hello Bharat,
    Does this work for generic SharePoint lists, or only for discussion Board. Only Can you explain me what is going on this piece of code

    $($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[1]).append($(divAttachments));

    Thank You

    ReplyDelete
  15. Thanks for the code. It worked perfectly. I only had to add an ICPDF icon.

    ReplyDelete
  16. Hi Bharat,

    We have been using your script successfully for quite some time, thanks. A couple of quick questions:

    1) how would one make the attachment's link open in a separate window (_blank)?
    2) how would one show a thumbnail of jpg, gif, or png attachments, instead of an icon? I almost had it with the following, but it didn't pick-up the attachments folder path

    imgTag = "(tag start)IMG BORDER=0 WIDTH=200px ALIGN=middle ALT='"+fileName +"'title='" +fileName +" 'SRC='"+fileName +"'(tag end)"+fileName;
    }

    We have a lot of clients who attach photos to our internal discussion board and viewers would love these features.

    Larry Tyndall
    City of Mississauga

    ReplyDelete
  17. Hi!

    This script doesnt work in my Sharepoint 2007. were there any changes made?

    ReplyDelete
  18. can anyone explain me this line?

    $($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[1]).append($(divAttachments));

    ReplyDelete
  19. This approach works well for me in SP2007, but ONLY under flat/default view of the discussion items. If I change from flat or from default it stops working. Has anyone heard of any strategy to make the code more robust to changes in the SharePoint view parameters?

    ReplyDelete
  20. This is an excellent solution to the problem.

    ReplyDelete
  21. is this applicable to sharepoint 2013 discussion board list ?

    ReplyDelete