3

We have a file system, with about 12 folder and 1300 documents in it. The top level has 3 folders, with 3 sub folders each. I am planning to create 3 libraries, with 3 folders in each, and transfer the content from the file system to SharePoint.

Is it possible to do this programmatically ? Can I implement a code that will iterate through the file system, create folders in the libraries accordingly and then transfer documents.

2 Answers 2

4

expanding on Annchal method you could use explorer view to do this. Time is money, your going to manually create the document library and then write and test the code which could take some time to get it correct as its over three libs on sharepoint.

opening the document library in explorer view will make it easy for you to move the whole folder or copy folder with content into explorer view.

so when you open the document library in explorer view it will be exactly like opening explorer view to view folders and documents on your desktop! its a no brainer and fast!

an example of what it looks like:

enter image description here

i know its for 2010 but same applies for 2013 or 2007

http://office.microsoft.com/en-gb/sharepoint-server-help/copy-or-move-library-files-by-using-open-with-explorer-HA101811182.aspx

1
  • no probs happy to help!
    – Ali Jafer
    Commented Oct 8, 2014 at 7:17
3

You have multiple ways of uploading a document, depending on where you code is running. Server object model:

var context = SPContext.Current;

// Get the web reference       
var web = context.Web;

// Get the library reference
var docLib = web.Lists.TryGetList("NAME OF THE LIBRARY HERE");    
if (docLib == null)
{
  return;
}

// Add the document. Y asume you have the FileStream somewhere
docLib.RootFolder.Files.Add(docLib.RootFolder.Url + "FILE NAME HERE", someFileStream);

With client object object model:

// Get the SharePoint context
ClientContext context = new ClientContext("URL OF THE SHAREPOINT SITE"); 

// Open the web
var web = context.Web;

// Create the new file  
var newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes("PATH TO YOUR FILE");
newFile.Url = "NAME OF THE NEW FILE";

// Get a reference to the document library
var docs = web.Lists.GetByTitle("NAME OF THE LIBRARY");
var uploadFile = docs.RootFolder.Files.Add(newFile);

// Upload the document
context.Load(uploadFile);
context.ExecuteQuery();

So you can first iterate the folder using following code:

string[] filePaths = Directory.GetFiles(@"c:\MyDir\");

From this you can iterate the files and upload to the document Library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.