ArticleDirectory
- Microsoft. SharePoint
- Upload documents to the document library
- Createdirectories (string path, spfoldercollection ofoldercollection)
- Local folder
- SharePoint Document Library
- Running result
- Restrictions
If you use SharePoint 2010, you can directly "upload multiple files ". In the displayed dialog box, you can drag and drop the entire folder. As shown in:
For SharePoint 2007, you can use the "Resource Manager" view to drag and drop folders like managing local files in the resource manager.
The method described below is the following. Of course, the next step is to manually create a folder to upload files one by one. You can use the SharePoint object model or web services to write a recursion program.ProgramTo upload files from a local folder to a Sharepoint library. Here we demonstrate how to upload an object model to a Sharepoint 2010 site.CodeIt is also available for Sharepoint 2007 sites. The only difference is 32-bit and 64-bit. The corresponding. NET Framework Version and Microsoft. Sharepoint. dll version.
Microsoft. SharePoint
Reference Microsoft. Sharepoint. DLL to access the SharePoint object model.
Upload documents to the document library
Try {localpath = txtlocalpath. text; // local path sharepointsite = txtserverurl. text; // SharePoint website url.doc umentlibraryname = txtlibrary. text; // SharePoint document library name. // obtain your SharePoint website object. using (spsite osite = new spsite (sharepointsite) {oweb = osite. openweb (); createdirectories (localpath, oweb. folders [documentlibraryname]. subfolders) ;}} catch (exception ex) {MessageBox. show ("error:" + ex. message );}
Createdirectories (string path, spfoldercollection ofoldercollection)
The createdirectories method is a recursive method that receives two parameters. The path parameter specifies the source location in the file system of the Local Computer; The ofoldercollection parameter specifies the target folder set. For usage of spfoldercollection, refer to here.
Private void createdirectories (string path, spfoldercollection ofoldercollection) {// upload multiple documents foreach (fileinfo ofi in new directoryinfo (PATH ). getfiles () {filestream = file. openread (OFI. fullname); spfile = ofoldercollection. folder. files. add (OFI. name, filestream, true); spfile. update ();} // upload multiple folders foreach (directoryinfo Odi in new directoryinfo (PATH ). getdirectories () {string sfoldername = Odi. fullname. split ('\') [Odi. fullname. split ('\\'). length-1]; spfolder spnewfolder = ofoldercollection. add (sfoldername); spnewfolder. update (); // recursively call to create a sub-Folder createdirectories (Odi. fullname, spnewfolder. subfolders );}}
Restrictions on running results of local folder SharePoint Document Library
- The size of the file to be uploaded cannot exceed 2 GB.
- The user executing the Code must have the upload permission for the corresponding document library.
References
Uploading files and folders recursively to a SharePoint site