Krichie – That SharePoint Guy

February 21, 2007

Importing/Exporting SharePoint Document Libraries to/from the File System

Filed under: Development, SharePoint — Keith Richie @ 3:08 am

Like my previous post, Purging a SharePoint list of all items and folders, here is another quick peice of code that will allow you to either Import a file system folder (And all files and subfolders) into a SharePoint Document Library, and also export a SharePoint Document Library to the file system. 

I had a SPExportWebFiles tool in the old Utility Suite that did the exporting, and an Import feature in SPSiteBuilder.  I’ve been waiting to find the time or opportunity to provide both a sample/tool that provided both import and export, and ran into that need today :) So, I thought I’d share.

The code contained in this file: SPIEFolder.txt, contains sample code to do just that, and create the nested folders as necassary to match the target location.

This code should work with either WSS 2.0/SPS2003 or WSS 3.0/MOSS 2007 without change. (I’ve only tested on WSS 3.0/MOSS btw at this time)

(Update 03-06-2007: See this link as this SPIEFolder is now published at http://www.codeplex.com/SPIEFolder )

So, got a folder you want to import into a SharePoint Document Library and you want to replicate the folder heirarchy and contents?  Or, want to export a SharePoint Document Library and replicate the folder heirarchy and contents on the file system?  This could help!

Hopefully this weekend, I’ll have some time to provide compiled versions, build scripts, and documentation like SPLSBackup for both this and the List Purging code as noted in my previous post.  But I doubt very seriously that the document will be as extensive as SPLSBackup :) , as I don’t see a big need for that :) .

HTH

Keith Richie


30 Comments »

  1. Hi ,
    Sorry this comment has nothing to do with your post. I am new to Sharepoint and I am having a lil problem with a webpart on SP07.
    I have used a Page Viewer Webpart to display a classic ASP Page. I would like to change the behaviour of this WebPart when ‘Edit’ is clicked and ‘Modify shared webpart’ is selected. I would like it to display another ASP Page. Is this possible? Or else is it possible to add another option onto the drop down that comes when clicking on ‘Edit’ of the webpart?
    Scratching my head.

    Comment by Nita Arvind — February 21, 2007 @ 1:17 pm

  2. Unfortunately, that’s not my area of expertise at this time. You might want to post your question to the design and customization or development and programming newsgroup at http://www.microsoft.com/sharepoint/community.mspx

    Comment by Keith Richie — February 21, 2007 @ 9:28 pm

  3. [...] Importing/Exporting SharePoint Document Libraries to/from the File System Got a folder you want to import into a SharePoint Document Library and you want to replicate the folder heirarchy and contents? Or, want to export a SharePoint Document Library and replicate the folder heirarchy and contents on the file system? This could help! [...]

    Pingback by Quick Links Krichie Edition » Sharepoint BUZZ - Blogging the Best of SharePoint 2007 (MOSS) — February 26, 2007 @ 6:21 am

  4. Hi Keith. Yes, we would really like build scripts or compiled versions of this tool! Keep on coding.

    Comment by Chris Fields — February 28, 2007 @ 12:30 am

  5. Hey Chris :) I didn’t find the time last weekend obviously :) But I’ll see if I can’t knock it out over the next couple of days

    Comment by Keith Richie — February 28, 2007 @ 4:32 am

  6. [...] SPIEFolder on CodePlex – Importing/Exporting SharePoint Document Libraries I previously mentioned and linked to sample code for Importing/Exporting SharePoint Document Libraries to/from the File System [...]

    Pingback by SPIEFolder on CodePlex - Importing/Exporting SharePoint Document Libraries « Krichie - That SharePoint Guy — March 7, 2007 @ 4:34 am

  7. Hi Keith
    I am a fellow sharepoint and was wondering if you would be interested in joing SharePoint Knowledge Network on Feedburner Network.
    http://networks.feedburner.com/just-sharepoint

    Just use contact form on my blog if you are keen.
    Cheers!
    Alpesh

    Comment by Alpesh Nakar — March 12, 2007 @ 6:31 pm

  8. Hi Keith,

    Here’s what I want to do. I want to populate a document library on my Sharepoint site. The DL will have a lot of metadata such as author_name, abstract, etc. I then want to export the DL from my site and import the DL into another site. Will the meta data be preserved on export and import?

    Also, does the import process overwrite or append any existing document library of the same name? I would like to load the documents iteratively, in other words, load 100 docs per week at my office and take them over to the other sharepoint site? Would the import process automatically overwrite the existing DL at the other sharepoint site every time I did an import?

    Thank You,
    -Stuart

    Comment by Stuart — September 14, 2007 @ 11:54 pm

  9. I should add that my site and the ‘other’ are physically separate and one of the sites does not have internet access.

    Comment by Stuart — September 14, 2007 @ 11:59 pm

  10. Hi Keith,

    I´m trying to use SPIEF with MOSS 2007, to export some documents to a folder.

    SPIEFolder.exe http://servidor:8000 “hospital/central/Pruebas Primer Version” C:\admin\resplado export

    And I,m getting this error message: “El valor no está dentro del intervalo esperado” (Value not in the expected interval)

    Thanks in Advance,

    Jesus

    Comment by Jesus — September 16, 2007 @ 1:03 am

  11. Jesus,

    from looking at your syntax, I’m assuming that “Pruebas Primer Version” is the actual name of your document library and “http://servidor:8000/hospital/central” is truly the name of your site, if so use this syntax

    http://servidor:8000/hospital/central “Pruebas Primer Version” C:\admin\resplado export

    Stuart

    Metadata is not preserved, and the import “Should” just iimport your new docs to the existing document library. It tries to “Add” the new folder to match the file system, and looking at the code, I could see where it would probably fail trying to add a subfolder that already exists. I can see a way for it to just check to see if that subfolder already exists, then select it for import, but don’t know when I’d be able to get to it.

    I’m supposed to re-publishing SPSIteManager soon, but can’t seem to find the time to get that done right now :)

    Comment by Keith Richie — September 19, 2007 @ 8:36 am

  12. would this tool enable me to take a document library from one sharepoint site on an intranet and move it and all of its contents to a completely different teamsite on the same intranet? i know what i want to do, but i cant translate anything on the microsoft help site and my sharepoint manipulation skills are relatively limited.
    thanks!

    Comment by erica — September 20, 2007 @ 5:57 am

  13. I just ran across this and was wondering:

    How does it handle invalid WSS 3.0/2.0 characters in filenames? Generally on a file server people use everything they can think of to name a file. Will thisdo a rename and weed out the erroneous characters before an upload.

    If not would you mind if I changed your code to make i work for such a situation?

    Comment by Stephen Schuster — November 21, 2007 @ 8:03 pm

  14. Hey Stephen, I don’t believe I put in any special handling for that, but sure if you change the code to work for you, I’d love to integrate the changes into the tool

    Comment by Keith Richie — November 21, 2007 @ 10:00 pm

  15. Hey Keith,

    Just found this online and it solved a major headache for me and works flawlessly! Thanks for sharing your excellent tool with the rest of us!

    Matt

    Comment by Matt — January 19, 2008 @ 3:04 am

  16. Your welcome Matt!

    Comment by Keith Richie — January 19, 2008 @ 3:57 am

  17. Using your SPIEFolder tool to do extracts and it’s saving me tons of time. I do seem to have an issue on one my environments where the tool keeps saying “The server instance specified was not found. Please specify the server’s address and port.” I can’t seem to get around this problem — on a Project/Sharepoint server 2003 (WSS2).

    This may be helpful for others also. I wanted to generate a list of all sites and their libraries for export using SPIEFolder tool. I was looking at doing this manually but got some help on the SQL side of things and came up with this query to produce the list for me. Save me days of work.


    SELECT DISTINCT 'http:///' + W.FullUrl AS URL, L.tp_Title, L.tp_BaseType, W.FullUrl
    FROM dbo.Lists L INNER JOIN
    dbo.Webs W ON W.Id = L.tp_WebId
    WHERE (L.tp_BaseType = '1') AND (L.tp_Title NOT LIKE '%Gallery%') OR (L.tp_BaseType = '5')

    Comment by Trevor — April 17, 2008 @ 11:00 pm

  18. Repost: Comment scrubbed some code

    SELECT DISTINCT ‘http://{domain name}/’ + W.FullUrl AS URL, L.tp_Title, L.tp_BaseType, W.FullUrl
    FROM dbo.Lists L INNER JOIN
    dbo.Webs W ON W.Id = L.tp_WebId
    WHERE (L.tp_BaseType = ’1′) AND (L.tp_Title NOT LIKE ‘%Gallery%’) OR (L.tp_BaseType = ’5′)

    Comment by Trevor — April 17, 2008 @ 11:01 pm

  19. Trevor, that’s fine if you run it on a system that isn’t a live production copy of the database, just note that direct queries against live content databases is Very unsupported :)

    Comment by Keith Richie — April 18, 2008 @ 12:18 am

  20. Keith,

    I’m brand new to Sharepoint from an api perspective and have been given an assignment that requires writing files (from a Windows network file share) to specific locations in a Sharepoint 2007 server. We’re using Ruby and Ruby on Rails as our web development environment. I’ve figured out how to read files from Sharepoint, but am having real troubles getting my arms around how to do the writes to Sharepoint. I’m thinking that maybe, rather than ‘pushing’ the files into Sharepoint, there’s a ‘pull’ strategy available where Sharepoint gets set up to monitor a (set of) directory(ies) and fetches them. Your thoughts?

    Thanks,
    Bill

    Comment by Bill Walton — May 20, 2008 @ 1:09 am

  21. Hey Bill,

    Take a look at the code in the tool related to this posting, specifically at the ImportFilesToFolder() method.

    Basically, you read the contents of the file into a buffer, then add it to the appropriate list/folder etc.

    Funny you ask this question, because I’m actually starting a project that will monitor a folder and do a synch as you mention via a pull strategy.

    Sure there are other applicaitons out there for this type of work, but I want to do it as an excercise and actually extend the idea behind SPIEFolder.

    Comment by Keith Richie — May 21, 2008 @ 12:23 am

  22. Is that URL stamped as a host header for the web application?

    Comment by Keith Richie — January 15, 2009 @ 12:21 am

  23. Quick Links Krichie Edition…

    There were a few posts that I found interesting on Krichie – ……

    Trackback by SharePoint Buzz — April 13, 2009 @ 7:44 am

  24. thanks for the tool. couple quickies, first off, forgive my ignorance. not sure how to construct the files to work. am i assuming correctly that i modify the appropriate .cmd file with my info and execute that and let it call the .exe file? also, if i were to make mods to the source, how to recompile? and include the program.cs and assemblyinfo.cs files? also, looking forward to a sync tool if you are still working on and have the time.
    Thanks!

    Comment by doktorhallam — June 25, 2009 @ 6:36 pm

    • ok, thats what documentation os for. forgive me once again. thanks, great tool. will be looking to modify to maintain some issues, such as meta data, etc. also, would be very interested in seeing some maintenance of versioning and possibly even a way for the check out/in functioning, but not sure how to implement, and guessing would be rather extensive.
      but thanks again!

      Comment by doktorhallam — June 25, 2009 @ 6:57 pm

  25. I am getting the following message when I try to export

    Cannot open database “WSS_Content_75c297d01091435fbbd57b63dadd39cb” requested by
    the login. The login failed.
    Login failed for user ‘domain name\username

    Comment by Tim — October 9, 2009 @ 12:29 am

  26. Is it possible to get a version of this that is designed to upload a single file instead of requiring it to start as a dir structure?

    Comment by Paul D — January 20, 2010 @ 3:25 am

  27. I see that this tool is not exporting all the metadata for the file. Is there a way to fix that so all metadata is exported out.

    Comment by Israel Gonzalez — April 29, 2010 @ 9:59 pm

    • Israel,
      It currently does not export ANY metadata, and there are no plans to do so.

      Comment by Keith Richie — April 30, 2010 @ 7:33 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.