我有一个Web窗体,它将一些输入的数据作为新的列表项上载到SharePoint列表中,效果很好。我试图添加新代码,以通过两个单独的<asp:FileUpload>
控件上传两个文件。 protected void sendToSharePoint() {}
中的以下代码无法将任何一个文件上传到指定的SharePoint文档库,更不用说两者了:
Default.aspx:
//Existing code
<asp:FileUpload ID="upldGradeReport" runat="server" />
<asp:FileUpload ID="upldExpenseReceipt" runat="server" />
<asp:Button ID="btnSubmitForm" OnClick="SubmitButton_Click" runat="server" Text="Submit" />
Default.aspx.cs:
using System;
using System.DirectoryServices;
using System.IO;
using System.Security;
using System.Web.UI;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;
//I left out the NameSpace and default public partial class wrapper, but they're here.
public ClientContext SPClientContext { get; set; }
public string SPErrorMsg { get; set; }
protected void SubmitButton_Click(object sender, EventArgs e) {
sendToSharePoint();
Response.BufferOutput = true;
Response.Redirect("Submission.aspx");
}
protected void sendToSharePoint() {
try {
string siteUrl = "<sharepoint site url>";
ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = new SharePointOnlineCredentials("<my username>", "<my password>");
string sDocName = string.Empty;
string sDocName1 = string.Empty;
Uri uri = new Uri(siteUrl);
string sSPSiteRelativeURL = uri.AbsolutePath;
sDocName = UploadFile(upldGradeReport.FileContent, upldGradeReport.FileName, sSPSiteRelativeURL, "<sharepoint document library>");
sDocName1 = UploadFile(upldExpenseReceipt.FileContent, upldExpenseReceipt.FileName, sSPSiteRelativeURL, "<sharepoint document library>");
//prior CSOM code to insert values into a new List Item exists here
clientContext.ExecuteQuery();
} catch (Exception ex) {
String ThisError = ex.Message;
}
}
public String UploadFile(Stream fs, string sFileName, string sSPSiteRelativeURL, string sLibraryName) {
string sDocName = string.Empty;
try {
var sFileURL = String.Format("{0}/{1}/{2}", sSPSiteRelativeURL, sLibraryName, sFileName);
ClientOM.File.SaveBinaryDirect(SPClientContext, sFileURL, fs, true);
sDocName = sFileName;
} catch (Exception ex) {
sDocName = string.Empty;
SPErrorMsg = ex.Message;
}
return sDocName;
}
创建新的ListItem并将表单输入的其余数据上传到单独的SharePoint列表上的代码在提交后仍然可以使用,并且我已经确认凭据正确并且所使用的帐户具有上传文件的特权到文档库。
我在做什么错了?
答案 0 :(得分:1)
我在本地环境中测试了以下代码;效果很好。
<div>
<asp:FileUpload ID="upldGradeReport" runat="server" />
<asp:FileUpload ID="upldExpenseReceipt" runat="server" />
<asp:Button ID="btnSubmitForm" OnClick="SubmitButton_Click" runat="server" Text="Submit" />
</div>
protected void SubmitButton_Click(object sender, EventArgs e)
{
sendToSharePoint();
Response.BufferOutput = true;
Response.Redirect("Submission.aspx");
}
protected void sendToSharePoint()
{
try
{
string siteUrl = "https://tenant.sharepoint.com/sites/lee";
ClientContext clientContext = new ClientContext(siteUrl);
SecureString securePassword = new SecureString();
foreach (char c in "password".ToCharArray()) securePassword.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("lee@tenant.onmicrosoft.com", securePassword);
string sDocName = string.Empty;
string sDocName1 = string.Empty;
Uri uri = new Uri(siteUrl);
string sSPSiteRelativeURL = uri.AbsolutePath;
sDocName = UploadFile(clientContext,upldGradeReport.FileContent, upldGradeReport.FileName, sSPSiteRelativeURL, "MyDoc");
sDocName1 = UploadFile(clientContext,upldExpenseReceipt.FileContent, upldExpenseReceipt.FileName, sSPSiteRelativeURL, "MyDoc");
//prior CSOM code to insert values into a new List Item exists here
//clientContext.ExecuteQuery();
}
catch (Exception ex)
{
String ThisError = ex.Message;
}
}
public String UploadFile(ClientContext clientContext,Stream fs, string sFileName, string sSPSiteRelativeURL, string sLibraryName)
{
string sDocName = string.Empty;
try
{
var sFileURL = String.Format("{0}/{1}/{2}", sSPSiteRelativeURL, sLibraryName, sFileName);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, sFileURL, fs, true);
sDocName = sFileName;
}
catch (Exception ex)
{
sDocName = string.Empty;
//SPErrorMsg = ex.Message;
}
return sDocName;
}