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

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.