Next generation's garbage RSS 2.0
# Thursday, June 12, 2008

SharePoint advertises the fact that you can access files on a document library via WebDAV. This is done via \\{server}\{site}\{doclibname}\ and you can easily get to it with the list menu under "Actions", "Open with Windows Explorer". A lesser known fact is that you can also use WebDAV to access the attachments collection of a list with attachments enabled. This is done via \\{server}\{site}\Lists\{listname}\Attachments\{listitemid}\

You should be aware that the WebDAV folder for the list item will not exist until a file has been added to the attachment collection. If you need it to always exist, you can make an ItemAdded event handler that simply adds a file and deletes it, like this:

Public Overrides Sub ItemAdded(ByVal properties As Microsoft.SharePoint.SPItemEventProperties)
   Const tempFileName As String = "deleteme.txt"
   If item.Attachments.Count = 0 Then
      Try
         Me
.DisableEventFiring()
         item.Attachments.AddNow(tempFileName, System.Text.Encoding.ASCII.GetBytes("This is a temporary file. If you find it, please delete it."))
         item.Attachments.DeleteNow(tempFileName)
         ' Here we also save a WebDAV hyperlink
         Dim u As New Uri(item.Attachments.UrlPrefix)
         item("AttachmentsLink") = String.Format("file://{0}{1}, Attachments", u.Host, u.AbsolutePath))
         item.Update()
      Finally
         Me.EnableEventFiring()
      End Try
   End If
End Sub

In the code above, I also set a hyperlink field so the user has easy access to the WebDAV view of the list item. I simply added a hyperlink column called "AttachmentsLink" to the list.

If you are having trouble using the WebDAV access, be sure that your WebClient service is enabled and started, which is NOT the default on Windows Server 2003.

There is a series of ItemAttachment events available and as I mentioned before, the documentation for them is not very good. Hint: properties.AfterUrl will indicate the added file. .BeforeUrl will indicate the file being deleted.

BUG: The thing to watch out for is that when using WebDAV, the ItemAttachment events don't fire, but instead, an item event fires with .AfterUrl or .BeforeUrl set, which it wouldn't ordinarily -- you can use this fact to choose to reroute Item events to the appropriate ItemAttachment events in your event code. It would be nice if you could poke in the proper properties.EventType, but it's read-only so you need to make sure that any part of your ItemAttachment event code can handle that.

Public Overrides Sub Item{Whatever}(ByVal properties As Microsoft.SharePoint.SPItemEventProperties)
   ' Reroute for WebDAV event bug
   If properties.AfterUrl IsNot Nothing AndAlso properties.AfterUrl <> "" Then ItemAttachmentAdding(properties) : Exit Sub 'or ..Added( if -ed event.
   If properties.BeforeUrl IsNot Nothing AndAlso properties.BeforeUrl <> "" Then ItemAttachmentDeleting(properties) : Exit Sub 'or ..Deleted( if -ed event

   ' Normal event code follows

End Sub

Thursday, June 12, 2008 4:50:50 PM (US Mountain Standard Time, UTC-07:00)  #    Comments [0] -
SharePoint
Archive
<June 2008>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
All Content © 2012, Hafthor Stefansson - Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. - Sign In