I want to get the file from the office 365 share point using user credentials (have full access) but while executing the program at runtime I'm getting the following error.
For this error I have read this post but not given the proper knowledge. And I have tried as per accepted answer but not working.
Microsoft.SharePoint.Client.ClientRequestException: 'Cannot contact site at the specified URL https://xxxx.sharepoint.com/sites/my_files/all Files. There is no Web named "/sites/my_files/all Files/_vti_bin/sites.asmx"
below is my code:
static void Main(string[] args)
{
string url = "https://xxxxx.sharepoint.com/sites/my_files/all Files";
string folderpath = "https://xxxxxxx.sharepoint.com/sites/my_files/all Files/Task";
string templocation = @"c:\Downloads\Sharepoint\";
Program.DownloadFilesFromSharePoint(url, folderpath, templocation);
}
static void DownloadFilesFromSharePoint(string siteUrl, string siteFolderPath, string localTempLocation)
{
string userName = "[email protected]";
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);
}
}
Suggest me how to achieve this and what is the issue ?
CodePudding user response:
When you call ctx.Web.GetFolderByServerRelativeUrl(siteFolderPath)
you pass an absolute path. siteFolderPath
is "https://xxxxxxx.sharepoint.com/sites/my_files/all Files/Task"
.
Change siteFolderPath
to relative path:
static void Main(string[] args)
{
string url = "https://xxxxx.sharepoint.com/sites/my_files/all Files";
// absolutePath: https://xxxxxxx.sharepoint.com/sites/my_files/all Files/Task
string folderRelativePath = "Task";
string templocation = @"c:\Downloads\Sharepoint\";
Program.DownloadFilesFromSharePoint(url, folderRelativePath, templocation);
}
CodePudding user response:
According to my testing, please change the site Url as https://xxxxx.sharepoint.com/sites/sitename
.
For example: https://xxxx.sharepoint.com/sites/zellatest
.In your code it should be:
static void Main(string[] args)
{
string url = "https://xxxxx.sharepoint.com/sites/my_files";
string folderpath = "https://xxxxxxx.sharepoint.com/sites/my_files/all Files/Task";
string templocation = @"c:\Downloads\Sharepoint\";
Program.DownloadFilesFromSharePoint(url, folderpath, templocation);
}