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'> "+fileName;
}
else if(fileExtension == 'doc' fileExtension == 'docx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/icdoc.gif'> "+fileName;
}
else if(fileExtension == 'ppt' fileExtension == 'pptx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICPPT'> "+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'> "+fileName;
}
else if(fileExtension == 'jpeg')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICJPEG.gif'> "+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'> "+fileName;
}
$(this)[0].innerHTML = imgTag;
});
$("#divAttachments"+index).append(attachmentsTable[0].outerHTML);
});
})
});
</script>
Thank you very much this was quite helpful and very simple to implement
ReplyDeleteHow to use this script?
ReplyDeleteYes, I would like to know how to implement this script, thank you!
ReplyDeleteWhat Happen if the load page has Jquery??
ReplyDeletefor me is throwing me an javascript error
Hi Bharat,
ReplyDeletethanks 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'
Can someone post step by step how to implement this?
ReplyDeleteProcedire to Implement:
ReplyDelete1) 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'
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?
ReplyDeleteFor step 3) Click on Site Actions --> Edit page.
ReplyDeleteOn the web part zone click on add web part to add content editor web part.
hi, your solutions sounds really nice - its that i'll use for my installation.
ReplyDeleteBut 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
Hello Bharat,
ReplyDeleteThanks 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
Ankit,
ReplyDeleteMake 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.
Works like a charm in 2007 SP2 Danish here.
ReplyDeleteBut only in IE.
In FF it only append and empty div. Think it is the $.get() which fails.
Hi Bharat
ReplyDeleteyou 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
Hello Bharat,
ReplyDeleteDoes 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
Thanks for the code. It worked perfectly. I only had to add an ICPDF icon.
ReplyDeleteHi Bharat,
ReplyDeleteWe 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
Hi!
ReplyDeleteThis script doesnt work in my Sharepoint 2007. were there any changes made?
can anyone explain me this line?
ReplyDelete$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[1]).append($(divAttachments));
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?
ReplyDeleteThis is an excellent solution to the problem.
ReplyDeleteis this applicable to sharepoint 2013 discussion board list ?
ReplyDelete