问题描述
我需要在msword应用程序中从SharePoint在线库中打开一个文件。所以我有用户名&密码和其他细节,需要在.net c#console应用程序中完成工作。所以基本上以编程方式在客户端应用程序中打开给定的文件URL。
甚至可能吗?请提出你的想法。感谢 
谢谢,Saravanan PRS
I need to open a file from SharePoint online library in msword application. So I have username & password and other details, and need to do the job in .net c# console application . So basically open a given file URL in client application programatically. Is that even possible ? Please give your idea. Thanks
Thanks, Saravanan PRS
推荐答案
请使用SharePoint Online CSOM下载文件,然后使用System.Diagnostics.Process.Start方法在Word应用程序中打开,这里有一个代码片段供您参考:
Please use SharePoint Online CSOM to download the file and then open in Word application with System.Diagnostics.Process.Start method, here is a code snippet for your reference:
string userName = "xxx@xxx.onmicrosoft.com";
string password = "xxxx";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
using (var clientContext = new ClientContext("https://xxx.sharepoint.com/sites/xxx"))
{
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
Web web = clientContext.Web;
clientContext.Load(web, a => a.ServerRelativeUrl);
clientContext.ExecuteQuery();
Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByServerRelativeUrl("/sites/xxx/Shared%20Documents/test.docx");
clientContext.Load(file);
clientContext.ExecuteQuery();
if (file != null)
{
FileInformation fileInfo = File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);
clientContext.ExecuteQuery();
var folderpath = @"c:\downloadfolder";
if (!System.IO.Directory.Exists(folderpath))
{
System.IO.Directory.CreateDirectory(folderpath);
}
using (var fileStream = new System.IO.FileStream(folderpath + @"\" + file.Name, System.IO.FileMode.Create))
{
fileInfo.Stream.CopyTo(fileStream);
}
System.Diagnostics.Process.Start(folderpath + @"\" + file.Name);
}
}
使用Nuget控制台使用以下行安装SharePoint Online CSOM dll:
Install the SharePoint Online CSOM dll using Nuget Console with this line:
Install-Package Microsoft.SharePointOnline.CSOM -Version 16.1.7723.1200
请记住在上面的代码中更改用户名,密码,站点URL,库名称以使其正常工作。
And remember to change the user name, password, site url , library name in the code above to make it work.
谢谢
最好的问候
这篇关于以编程方式在客户端应用程序中打开文件 - SharePoint Online的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!