使用asp.net上传文档到sharepoint

本文关键字:文档 sharepoint asp net 使用 | 更新日期: 2023-09-27 18:03:06

我有一个表单,它有一个formview。我想包括一个文件上传控制上传文件到sharepoint存储库,并保存上传的文件为ID_1,ID_2等。我是asp.net的新手。

使用asp.net上传文档到sharepoint

您可以使用客户端对象模型上传文件到sharepoint:

public void UploadFileToSharePoint(string filePath)
{
        ClientContext context = new ClientContext("yoursite");
        Web web = context.Web;
        FileCreationInformation newFile = new FileCreationInformation();
        newFile.Content = System.IO.File.ReadAllBytes(filePath);
        newFile.Url = System.IO.Path.GetFileName(filePath);
        List docs = web.Lists.GetByTitle("LibName");
        Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
        context.Load(uploadFile);
        context.ExecuteQuery();
}

之后,您可以使用所需的ID更新列表项,或者您可以附加一个项更新事件处理程序来更新文件的Title

  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();
    //read Last item ID here refer my 2nd link bellow
    item.Attachments.Add("ID_1.doc", byteArray);
    item["Title"] = TextBox1.Text;
    item.Update();
    oWeb.AllowUnsafeUpdates = false;
   }
  }
}

又多了一种上传方式1) http://msdn.microsoft.com/en-us/library/lists.lists.addattachment.aspx

2)Get the First &Sharepoint List