C#WPF如何从Sharepoint Teamsite文档库下载和上载文件 [英] C# WPF How to download and upload file from Sharepoint Teamsite Document Library

查看:137
本文介绍了C#WPF如何从Sharepoint Teamsite文档库下载和上载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个Sharepoint Teamsite文档库,我有一个存储在文件夹中的文件,我想下载它,并将一个新文件上传回同一站点上的另一个文件夹。



确切地说,我的网站网址是  https:// share point.wsx.sg/sites/op/SitePages/Home.aspx
我想下载文件  conf.json  其中
位于  / files / config / conf.json  ,
然后我的C#WPF程序就可以了,并将新文件上传到  / files / result / result.json  。



如何使用C#代码实现这一目标?我读到了WebDav协议,但无法弄清楚如何使用它,或者我是否需要下载任何软件包。



所有这些都是针对C#WPF程序的,一旦启动,它将尝试下载  conf.json
并读入它来实例化一些对象,如果失败,它只会读取本地存储的

conf.json



想知道是否有人可以提供替代方法,或者指向一些有用的链接,我将非常感激!



谢谢!

解决方案


我们可以使用客户端对象模型(CSOM C#)来实现它。


下载文件:

 string siteUrl =" https://sharepoint.wsx.sg/sites/op" ;; 
string fileRelativePath = @" /sites/op/files/config/conf.json" ;;
string downloadFilePath =" c:\\ temp\\conf.json" ;;

var login =" test@tenant.onmicrosoft.com" ;;
var password =" xxx";

ClientContext context = new ClientContext(siteUrl);
var securePassword = new SecureString();
foreach(char c in password.ToCharArray()) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(login,securePassword);
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context,fileRelativePath);
using (流目的地= System.IO.File.Create(downloadFi lePath))
{
for(int a = fileInfo.Stream.ReadByte(); a!= -1; a = fileInfo.Stream.ReadByte())
destination.WriteByte((byte)a);
}

上传文件。

 string siteUrl =" https:// sharepoint。 wsx.sg/sites/op" ;; 

var login =" test@tenant.onmicrosoft.com" ;;
var password =" xxx" ;;

ClientContext context = new ClientContext(siteUrl);
var securePassword = new SecureString();
foreach(charc in password.ToCharArray())securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(login,securePassword);

Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(" c:\\ temp\\result.json");

//文件网址是名称
newFile.Url = @" result.json" ;;
列出docs = web.Lists.GetByTitle(" Files");

//获取文件夹并添加到
文件夹= docs.RootFolder.Folders.GetByUrl(" result");
Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(newFile);

context.Load(docs);
context.Load(uploadFile);
context.ExecuteQuery();

更多信息:


http://www.lucacostante.com/item/15-download-and-upload-files- via-sharepoint-client-object-model.html


最好的问候,


Dennis



I have a Sharepoint Teamsite Document Library where I have a file stored in a folder, I want to download it, and upload a new file back to another folder on the same site.

To be precise, my site url is https://sharepoint.wsx.sg/sites/op/SitePages/Home.aspx, I would like to download the file conf.json which is located at /files/config/conf.json , then my C# WPF program will do it's thing, and upload a new file to /files/result/result.json .

How would I be able to achieve this using C# code? I read about a WebDav protocol, but couldn't really figure out how to use it, or if I need to download any packages.

All this is for a C# WPF program, upon launch it will  attempt to download conf.json and read it in to instantiate some objects, if this fails, it will just read a locally stored conf.json.

Wondering if anyone could offer an alternative method of doing so, or point me to some helpful links, would greatly appreciate it!

Thanks!

解决方案

Hi,

We can use client side object model(CSOM C#) to achieve it.

Download the file:

string siteUrl = "https://sharepoint.wsx.sg/sites/op";
string fileRelativePath = @"/sites/op/files/config/conf.json";
string downloadFilePath = "c:\\temp\\conf.json";

var login = "test@tenant.onmicrosoft.com";
var password = "xxx";

ClientContext context = new ClientContext(siteUrl);
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(login, securePassword);
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRelativePath);
using (Stream destination = System.IO.File.Create(downloadFilePath))
{
	for (int a = fileInfo.Stream.ReadByte(); a != -1; a = fileInfo.Stream.ReadByte())
		destination.WriteByte((byte)a);
}

Upload the file.

string siteUrl = "https://sharepoint.wsx.sg/sites/op";

var login = "test@tenant.onmicrosoft.com";
var password = "xxx";

ClientContext context = new ClientContext(siteUrl);
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(login, securePassword);

Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes("c:\\temp\\result.json");

//file url is name
newFile.Url = @"result.json";
List docs = web.Lists.GetByTitle("Files");

//get folder and add to that
Folder folder = docs.RootFolder.Folders.GetByUrl("result");
Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(newFile);

context.Load(docs);
context.Load(uploadFile);
context.ExecuteQuery();

More information:

http://www.lucacostante.com/item/15-download-and-upload-files-via-sharepoint-client-object-model.html

Best Regards,

Dennis


这篇关于C#WPF如何从Sharepoint Teamsite文档库下载和上载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆