0

I am creating a visual web part to allow users to submit a form which would allow them to upload a document into a sharepoint list. My issue is uploading that document into the list. I am using the asp fileupload control and a "submit" button on click event to get that started. After that I am a bit lost

What would be the back end code to upload the file into my list?

1 Answers1

1

Bellow is the code snippet.

private static void UploadBtn_click(Object sender, EventArgs e)
{
  using (SPSite osite = new SPSite("URL"))
  {
    using (SPWeb oWeb = osite.OpenWeb())
    {
      oWeb.AllowUnsafeUpdates = true;
      SPList list = oWeb.TryGetList("ListName");
      SPListItem item = list.AddItem();
      FileStream stream = new FileStream(UploadBtn.FileName, FileMode.Open) ;
      byte[] byteArray = new byte[stream.Length];
      stream.Read(byteArray, 0, Convert.ToInt32(stream.Length));
      stream.Close();
      item.Attachments.Add("myDoc.doc", byteArray);
      item["Title"] = TextBox1.Text;
      item.Update();
      oWeb.AllowUnsafeUpdates = false;
    }
  }
}

One more example from MSDN http://msdn.microsoft.com/en-us/library/lists.lists.addattachment.aspx

vinayak hegde
  • 2,117
  • 26
  • 26