Custom Webpart to Upload File in SharePoint Document Library

This article explains how to upload a file in a Document Library.

In SharePoint, library files are uploaded using the Add method on the Files property of a SPWeb like this:

SPContext.Current.Web.Files.Add(docLibrary.RootFolder.Url + "/" + fileUpload.FileName, contents);

I have created a webpart that uploads a file provided by the user to a SharePoint library.

Use the following procedure to create a sample of doing it.

1. Create an empty SharePoint project and add a webpart.



2. Modify the code as below:

  1. using System;  
  2. using System.ComponentModel;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Web.UI.WebControls.WebParts;  
  7. using Microsoft.SharePoint;  
  8. using Microsoft.SharePoint.WebControls;  
  9. using System.IO;  
  10.   
  11. namespace UploadFile.UploadFileWebPart  
  12. {  
  13.     [ToolboxItemAttribute(false)]  
  14.     public class UploadFileWebPart : WebPart  
  15.     {  
  16.         private FileUpload fileUpload = new FileUpload();  
  17.         private Button btnUpload = new Button();  
  18.   
  19.         private string docLibrary;  
  20.   
  21.         [WebBrowsable, Personalizable]  
  22.         public string DocumentLibrary  
  23.         {  
  24.             Get  
  25.             {  
  26.                 return docLibrary;  
  27.             }  
  28.             Set  
  29.             {  
  30.                 docLibrary = value;  
  31.             }  
  32.         }  
  33.         protected override void CreateChildControls()  
  34.         {  
  35.             Controls.Add(fileUpload);  
  36.             Controls.Add(btnUpload);  
  37.   
  38.             btnUpload.Text = "Upload";  
  39.             btnUpload.Click += new EventHandler(OnUploadClick);  
  40.         }  
  41.         void OnUploadClick(object sender, EventArgs e)  
  42.         {  
  43.             SPList docLibrary;  
  44.             Try  
  45.             {  
  46.                 docLibrary = ValidateList();  
  47.             }  
  48.             catch (Exception ex)  
  49.             {  
  50.                 Controls.Add(new LiteralControl(ex.Message));  
  51.                 return;  
  52.             }  
  53.             Stream fStream = fileUpload.PostedFile.InputStream;  
  54.             byte[] contents = new byte[fStream.Length];  
  55.             fStream.Read(contents, 0, (int)fStream.Length);  
  56.             fStream.Close();  
  57.   
  58.             SPSecurity.CatchAccessDeniedException = false;  
  59.             Try  
  60.             {  
  61.                 SPContext.Current.Web.Files.Add(docLibrary.RootFolder.Url + "/" + fileUpload.FileName, contents);  
  62.             }  
  63.             catch (UnauthorizedAccessException)  
  64.             {  
  65.                 Controls.Add(new LiteralControl("You are not authorized to upload files to the document library"));  
  66.             }  
  67.         }  
  68.         private SPList ValidateList()  
  69.         {  
  70.             if (string.IsNullOrEmpty(DocumentLibrary))  
  71.             {  
  72.                 throw new Exception("Please edit the webpart and fill the Document Library property!");  
  73.             }  
  74.   
  75.             if (!fileUpload.HasFile)  
  76.             {  
  77.                 throw new Exception("Please select a file to upload.");  
  78.             }  
  79.             Try  
  80.             {  
  81.                 return SPContext.Current.Web.Lists[DocumentLibrary];  
  82.             }  
  83.             catch (ArgumentException)  
  84.             {  
  85.                 throw new Exception("Library not found!");  
  86.             }  
  87.         }  
  88.   
  89.     }  

3. Build the solution and deploy.

4. Go to the Site Page library and create a page as in the following:



5. Add the Webpart.



6. Try to upload a document in the library. You will get the error as below:



7. Edit the webpart and fill in the name of the Document Library where you want to add the document through this page's webpart.



8. Now upload a file.



9. Navigate to the Document Library. You will find your uploaded document in the library.



Conclusion

This article explained how to upload a document in a Document Library using a custom webpart property.