我想使用用户凭据从 Office 365 共享点获取文件(具有完全访问权限) 但是在运行时执行程序时,我收到以下错误。
对于这个错误,我已经阅读了这篇文章,但没有给出正确的知识。我已经按照接受的答案尝试过,但没有工作。
Microsoft.SharePoint.Client.ClientRequestException:"无法通过指定的 URL https://xxxx.sharepoint.com/sites/my_files/all%20Files 联系网站。没有名为"/sites/my_files/all Files/_vti_bin/sites.asmx"的网站
下面是我的代码:
static void Main(string[] args)
{
string url = "https://xxxxx.sharepoint.com/sites/my_files/all%20Files";
string folderpath = "https://xxxxxxx.sharepoint.com/sites/my_files/all%20Files/Task";
string templocation = @"c:DownloadsSharepoint";
Program.DownloadFilesFromSharePoint(url, folderpath, templocation);
}
static void DownloadFilesFromSharePoint(string siteUrl, string siteFolderPath, string localTempLocation)
{
string userName = "aaaa@xxxxxx.com";
string pswd = "ssssss@1043";
SecureString password = new SecureString();
foreach (var c in pswd.ToCharArray()) password.AppendChar(c);
var ctx = new ClientContext(siteUrl);
//ctx.Credentials = new NetworkCredential(userName, password, "smtp-mail.outlook.com");
ctx.Credentials = new SharePointOnlineCredentials(userName, password);
FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(siteFolderPath).Files;
ctx.Load(files);
if (ctx.HasPendingRequest)
{
ctx.ExecuteQuery(); //getting error here : Cannot contact site at the specified URL. There is no Web named "*.asmx"
}
foreach (File file in files)
{
FileInformation fileInfo = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
ctx.ExecuteQuery();
var filePath = localTempLocation + "\" + file.Name;
System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
fileInfo.Stream.CopyTo(fileStream);
}
}
建议我如何实现这一目标,问题是什么?
当你调用ctx.Web.GetFolderByServerRelativeUrl(siteFolderPath)
时,你会传递一个绝对路径。siteFolderPath
"https://xxxxxxx.sharepoint.com/sites/my_files/all%20Files/Task"
.
将siteFolderPath
更改为相对路径:
static void Main(string[] args)
{
string url = "https://xxxxx.sharepoint.com/sites/my_files/all%20Files";
// absolutePath: https://xxxxxxx.sharepoint.com/sites/my_files/all%20Files/Task
string folderRelativePath = "Task";
string templocation = @"c:DownloadsSharepoint";
Program.DownloadFilesFromSharePoint(url, folderRelativePath, templocation);
}
根据我的测试,请将站点 URL 更改为https://xxxxx.sharepoint.com/sites/sitename
.
例如:https://xxxx.sharepoint.com/sites/zellatest
.在您的代码中,它应该是:
static void Main(string[] args)
{
string url = "https://xxxxx.sharepoint.com/sites/my_files";
string folderpath = "https://xxxxxxx.sharepoint.com/sites/my_files/all%20Files/Task";
string templocation = @"c:DownloadsSharepoint";
Program.DownloadFilesFromSharePoint(url, folderpath, templocation);
}