Geeks With Blogs

News
About Me:
I am a Female SharePoint Developer (a platinum unicorn). I have been working with SharePoint since I attended the Portal University in 2005. I hold a BA in Computer Science from the University of Missouri - Kansas City. I love playing Rockband, organizing user group meetings, working with code, attending events as a speaker or organizer, and having bizarre conversations about geek things with cool people. If you have any comments or questions fill out the contact form and I will try my best to help.



The Guide of a MOSSLover Becky Isserman's Blog

Recently I was tasked to create a console application to get all the attachments from a list and copy them to a file share.  Everyone has code out there, but I noticed not one single place had the correct formatting for everything.

First off there is gotcha.  You cannot copy SPAttachmentCollection from the List Item and step through the list of strings.  You need to pull in the Item.Attachments.UrlPrefix and throw it into an SPFolder Item like below:

SPFolder Folder = Web.GetFolder(Item.Attachments.UrlPrefix);

If you try and pull out the File.OpenBinary(); without using this specific method for some reason you cannot grab the full SPFile.  It is malformed.

So here is what it could look like if you wanted to get the attachments and copy them to the file share:

foreach(SPListItem Item in List.Items)

{

           if(Item.Attachments.Count !=0)

           {

                        SPFolder Folder = Web.GetFolder(Item.Attachments.UrlPrefix);

                        foreach(SPFile File in Folder)

                       {

                                   byte[] CopyFile = File.OpenBinary(); //this fails if you pull in SPFile directly 
                                    //Your fstream code to copy file, remember to close your fstream when done

                        }

           }

}

I hope that this helps someone else in the long run.  Good luck.  Catch you all later.

Posted on Monday, January 25, 2010 3:45 AM | Back to top


Comments on this post: Bug When Copying Attachments in SharePoint

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © MOSSLover | Powered by: GeeksWithBlogs.net