我想将文件从URL上传到Sharepoint Online。我查找了各种类,我找到的那个看起来不错的类WebClient
,我试图将已上传到我的应用程序网站的文件拉到一个流中,然后将该流上传到我的目标列表在Sharepoint中,我得到的只是一个" 403 Forbidden"异常。
WebClient wc = new WebClient();
wc.UseDefaultCredentials = true;
wc.Headers.Add("User-Agent:Other");
byte[] response = wc.DownloadData
("www.mysite-12345.sharepoint.com/sites/dev/myapp/lists/doclib/themefile.spfont");
var uploadUrl =
"www.mysite.sharepoint.com/_catalogs/theme/15/themefile.spfont";
wc.UploadData
("www.mysite.sharepoint.com/_catalogs/theme/15/themefile.spfont", "POST", response);
}
如果有人可以就我出错的地方或上传文件的替代方法给我一些建议,我会非常感激。
答案 0 :(得分:1)
我猜您不仅需要将文件上传到SharePoint Online,还要将一个站点复制到另一个站点,对吗?
在SharePoint中有多种方法可以下载/上传文件,您可以将CSOM等特定的SharePoint API用于此目的,或者使用vanilla .NET功能(例如HttpClient Class,WebClient Class)如下所示:
var sourceWebUri = new Uri("https://sourcesite.sharepoint.com");
var targetWebUri = new Uri("https://targetsite.sharepoint.com");
var userName = "username@tenant.onmicrosoft.com";
var password = "password";
using(var sourceClient = GetClient(sourceWebUri,userName,password))
{
var fileContent = sourceClient.DownloadData(sourceWebUri + "/documents/SharePoint User Guide.docx");
using(var targetClient = GetClient(targetWebUri,userName,password))
{
targetClient.UploadData(targetWebUri + "/documents/SharePoint User Guide.docx,"PUT" ,fileContent);
}
}
,其中
private static WebClient GetClient(Uri webUri, string userName, string password)
{
var client = new WebClient();
client.Credentials = GetCredentials(webUri, userName, password);
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
return client;
}
private static SharePointOnlineCredentials GetCredentials(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var ch in password) securePassword.AppendChar(ch);
return new SharePointOnlineCredentials(userName, securePassword);
}
SharePoint CSOM API包含用于从SharePoint下载文件的File.OpenBinaryDirect method和用于将文件上载到SharePoint的File.SaveBinaryDirect method,以下示例演示如何在站点之间复制文件:
public static void CopyFile(ClientContext sourceCtx, string sourceFileUrl, ClientContext targetCtx, string targetFileUrl)
{
if (sourceCtx.HasPendingRequest)
sourceCtx.ExecuteQuery();
using (var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(sourceCtx, sourceFileUrl))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(targetCtx, targetFileUrl, fileInfo.Stream, true);
}
}
用法
using (var sourceCtx = GetSPOContext(sourceWebUri, userName, password))
{
using (var targetCtx = GetSPOContext(targetWebUri, userName, password))
{
CopyFile(sourceCtx, "/sourcesite/documents/SharePoint User Guide.docx", targetCtx, "/targetsite/documents/SharePoint User Guide.docx");
}
}
,其中
public static ClientContext GetSPOContext(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var ch in password) securePassword.AppendChar(ch);
return new ClientContext(webUri) { Credentials = new SharePointOnlineCredentials(userName, securePassword) };
}
先决条件
SharePointOnlineCredentials class:
的一部分表示提供访问SharePoint的凭据的对象 在线资源。