GetAttachmentCollection and building links

Jul 18, 2011 at 7:39 PM

Am trying to insert a column into my list display that exposes links to attachment(s).  I am struggling with two issues:

  • it flood fills the inserted Attchment column with the link to the first attachment it finds
  • if I have multiple attachments, it builds extra columns - I understand why it does this, but can't wrap my head around an alternative.

Caveats:

  • only have access to CEWP (no SP Designer)
  • am a novice at best as far as JS/jQuery/jAnything goes
  • my list is called AttList, and eventually I will replace my "split" command with a pop to grab the filename

I am trying to leverage the Edit Control block...here is my code!!

<SCRIPT type=text/javascript>

$(".ms-vh2 table").each(function(index) {
if(index==0){
     $(this).parent().parent().parent().prepend("<td class='ms-vh2'><a>Attachment</a></td>");
  }
 });


 $().SPServices({
   operation:'GetAttachmentCollection',
   async: false,
   listName: "AttList",
   ID: $(this).attr("ows_ID"),
     completefunc: function (xData, Status) {
     $(xData.responseXML).find("Attachment").each(function() {
        var longLink = $(this).text().split("/");
        var fileName = longLink[9];
 //        alert(fileName)
        var attLink = "<a href='" + $(this).text() + "'  target=_blank><img src='/_layouts/images/attach.gif' alt='" + fileName + "'/></a>";
     
    $(".ms-vb-title table").each(function() {
        var theId = $(this).attr("Id");
         $(this).parent().parent().prepend("<td>" + attLink + "</td>");
      });

   });
  }
});


</script>
Coordinator
Jul 18, 2011 at 8:47 PM

will266:

It's a little tough to follow your code, and perhaps that's the problem. So, in English, it seems that what you want to do is display the attachments for each item in a list view as a new column on the left side of the view.

It's also a little hard to know how your selectors are going to work without seeing the markup from the view. There must also be more jQuery code involved or the line ID: $(this).attr("ows_ID"), wouldn't make any sense.

You're creating a link for each attachment, which makes sense, but then you seem to be adding that link to *all* of the items in the view (based on your $(".ms-vb-title table").each(function() ).

What you're doing ought to be possible. but you're just not there yet. My suggestion would be to put the script into a file in a Document Library and use the Content link in your CEWP. That will allow you faster "round trips" in your debugging.

M.

Jul 18, 2011 at 8:50 PM

Another attempt...

And I forgot to mention that for each of these, I removed the attachment column from the list view, as well as included the Title(with edit control block) column.

this one gives me a single paperclip icon, regardless of how many attachments exist, if any.  Clicking the paperclip launches all attachments in their own window...

<STYLE>

.custom {
vertical-align:top;
color:#2B80FF;
text-decoration:underline;
font-size=8pt;
font-family=tahoma;
cursor:pointer;
width:75px;
text-align:center;
}

</STYLE>


<SCRIPT type=text/javascript>

var URL = document.location.href;
var urlParts = URL.split("/Lists/");
var redURL = urlParts[1];
var splitURL = redURL.split("/");
var lName = splitURL[0];


$(".ms-vh2 table").each(function(index) {
if(index==0){
     $(this).parent().parent().parent().prepend("<td class='ms-vh2'><a>Attachment(s)</a></td>");
  }
 });

$(".ms-vb-title table").each(function() { 
     var theId = $(this).attr("Id"); 
     $(this).parent().parent().prepend("<td class='custom'><a onclick='custAction1(" + theId + ")'><img src='/_layouts/images/attach.gif'></a></td>");
});




function custAction1(theId)
{

 $().SPServices({
   operation:'GetAttachmentCollection',
   async: false,
   listName: lName,
   ID: theId,
     completefunc: function (xData, Status) {
     $(xData.responseXML).find("Attachment").each(function() {
        var origLink = $(this).text();
        window.open(origLink)
   

   });
  }
 });
}




</script>
Coordinator
Jul 18, 2011 at 9:02 PM

This is pretty different. You're attaching to the click event for the link, so you won't see anything until you click on the link, and then you'll get a new window for each attachment. I'm really not clear what you're trying to do.

M.

Jul 18, 2011 at 9:32 PM

In a perfect scenario, I would have a column that contained a link to each attachment a list item had.  It would be a hyperlink, and the name of the link would be the file name.  Basically what you summized in your initial response.

My first attempt was a disaster, so I did a wild departure for the second.  The second, since the links are read at time of click, will never be a good solution.

I think my thought of building the column on the fly is a mistake - is likely better to build a true list column, and use SP Services to populate it.  I have done a lot with the list services - unfortunately nothing with the attachment functions.

The biggest drawback to that would be having to repopulate the values on each page load...  I was trying to "have it both ways"

 

Coordinator
Jul 18, 2011 at 9:36 PM

Your general approach is fine, but the bits and bobs aren't right yet.

The idea is, for each item in the view, gather all of the attachments and show a link which will open each. That's absolutely possible. You'll need to get the right markup built to contain those links. Doing it on the fly should work fine, though it will lock up the page a little if you do it synchronously.

M.

Aug 1, 2012 at 11:06 PM

Hello,

A co-worker has this requirement and so an update on this would be most appreciated.

Thanks,

Areon

Aug 2, 2012 at 1:35 PM

What code have you tried so far @AreonJackson?

Cheers,
Matt 

Aug 2, 2012 at 1:40 PM

I found a different but effective way to change the attachment paperclip into a hover spot that listed each attachment as a link.  I am sure you can alter to write the values into a column, and use SPServices in lieu of the calls in the code:



http://sharepointjavascript.wordpress.com/2011/10/07/list-all-attachments-when-clicking-the-paperclip-in-a-list-view-updated-version/