文档使用asp.net上传到sharepoint

时间:2014-05-13 05:50:22

标签: c# asp.net sharepoint

我有一张有形式视图的表单。我想包含一个fileupload控件来将文件上传到sharepoint存储库并将上传的文件保存为ID_1,ID_2等。我是asp.net的新手。我需要帮助来解决这个问题

2 个答案:

答案 0 :(得分:1)

您可以使用客户端对象模型将文件上传到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更新listitem,也可以附加项目更新事件处理程序以更新文件的标题。

答案 1 :(得分:0)

  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 & Last ListItem in Sharepoint List