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

[News! I’ve kicked off a crowdfunding campaign for the launch of my next album Xenogenesis Volume 1: Dawn!  Want to be part of it and have a chance to have your name in the liner notes and get an autographed copy? Be sure to check it out by clicking right here!]

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 )

(Update 06-02-2011: See this link SPIEFolder 1.0 removed and SPIEFolder for SharePoint 2010 is now published at https://blog.krichie.com/my-sharepoint-tools/#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

35 Replies to “Importing/Exporting SharePoint Document Libraries to/from the File System”

  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.

  2. 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

  3. 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

  4. 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 🙂

  5. 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!

  6. 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?

  7. 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

  8. 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')

  9. 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’)

  10. 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 🙂

  11. 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

  12. 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.

  13. Pingback: SharePoint Buzz
  14. 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!

    1. 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!

  15. 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

  16. 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?

  17. 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.

  18. Hello Keith,

    I think your tool is brilliant, it has been working very well for us to use as a tool to backup student documents.

    We have run into a slight problem and hope you can give us some pointers. Exporting documents work all but .dwt files (Dreamweaver Template):

    Exporting file Template.dwt to x:\test\Personal Documents\WEBSITE BACKUP\Weba
    site\Web work 2011\Templates\Template.dwt
    Could not access server.

    Rick
    Thanks,

  19. Hi Keith,

    Trying to import a folder to Sharepoint 2007 I’m getting this error, your kind advice would be most appreciated:
    Could not load file or assembly ‘Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ or one of its dependencies. The system cannot find the file specified.

    1. Simon, that’s because you’re trying to run a tool built for SharePoint 2010 on a SharePoint 2007 system. I have a 2007 version of SPIEFolder 2.0 in the works, and will post to my tools page when it’s ready.

    1. Hey Keith, Heard a lot of great things about your tool on the internet. I would like to run your tool on my 2007 env. Anyway i can get ver 1.0, until you have 2.0 ready for 2007?

      Thanks
      Satish

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: