c# - 在 Sharepoint 服务器中添加/上传/更新文件的问题
问题描述
我正在使用以下代码将本地文件添加到共享点服务器:
using Microsoft.Azure.ActiveDirectory.GraphClient;
using Microsoft.SharePoint.Client;
using MSharp.Framework.Services;
using System;
using System.IO;
using System.Net;
using System.Security;
namespace SharepointFileSend
{
class Program
{
private const string V = "XXXX!";
private static string hostWeb="company.sharepoint.com";
static void Main(string[] args)
{
string siteUrl = "https://company.sharepoint.com/site/";
string password = "XXXX!";
//Insert Credentials
ClientContext context = new ClientContext(siteUrl);
var securePassword = new SecureString();
foreach (var c in password.ToCharArray() ) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials("xyz@company.com", securePassword);
Web site = context.Web;
//Get the required RootFolder
string barRootFolderRelativeUrl = "Documents/Global Procurement";
Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl);
//Create new subFolder to load files into
string newFolderName = "SPTest";
barFolder.Folders.Add(newFolderName);
barFolder.Update();
//Add file to new Folder
Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + newFolderName);
string sharePointDocPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), "Sharepoint.xml");
FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(sharePointDocPath), Url = Path.GetFileName(sharePointDocPath), Overwrite = true };
currentRunFolder.Files.Add(newFile);
currentRunFolder.Update();
context.ExecuteQuery();
//Return the URL of the new uploaded file
// string newUrl = siteUrl + barRootFolderRelativeUrl + "/" + newFolderName + "/" + Path.GetFileName(@p);
}
}
}
But, i am getting below exception:
using Microsoft.Azure.ActiveDirectory.GraphClient;
using Microsoft.SharePoint.Client;
using MSharp.Framework.Services;
using System;
using System.IO;
using System.Net;
using System.Security;
namespace SharepointFileSend
{
class Program
{
private const string V = "PennyRoyal8696!";
private static string hostWeb="company.sharepoint.com";
static void Main(string[] args)
{
string siteUrl = "https://company.sharepoint.com/site/";
string password = "PennyRoyal8696!";
//Insert Credentials
ClientContext context = new ClientContext(siteUrl);
var securePassword = new SecureString();
foreach (var c in password.ToCharArray() ) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials("XXX@XXX.com", securePassword);
Web site = context.Web;
//Get the required RootFolder
string barRootFolderRelativeUrl = "Documents/Global";
Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl);
//Create new subFolder to load files into
string newFolderName = "SPTest";
barFolder.Folders.Add(newFolderName);
barFolder.Update();
//Add file to new Folder
Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + newFolderName);
string sharePointDocPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), "Sharepoint.xml");
FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(sharePointDocPath), Url = Path.GetFileName(sharePointDocPath), Overwrite = true };
currentRunFolder.Files.Add(newFile);
currentRunFolder.Update();
context.ExecuteQuery();
//Return the URL of the new uploaded file
// string newUrl = siteUrl + barRootFolderRelativeUrl + "/" + newFolderName + "/" + Path.GetFileName(@p);
}
}
}
在最后一行 ExecuteQuery(); 我得到以下异常:System.Net.WebException:'远程服务器返回错误:(400)错误请求。'
请建议我改进代码以避免此异常以及更新 Sharepoint 中现有文件的逻辑。
解决方案
示例测试代码供您参考。
string SiteURL = @"https://xxx.sharepoint.com/sites/lee/";
var login = "user@xxx.onmicrosoft.com";
var password = "Password";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
using (var context = new ClientContext(SiteURL))
{
context.Credentials = onlineCredentials;
Web site = context.Web;
//Get the required RootFolder
string barRootFolderRelativeUrl = "/sites/lee/LibDS/Folder";
Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl);
//Create new subFolder to load files into
string newFolderName = "SPTest";
barFolder.Folders.Add(newFolderName);
//barFolder.Update();
context.ExecuteQuery();
//Add file to new Folder
Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + newFolderName);
string sharePointDocPath = @"C:\Lee\test.docx";//System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), "Sharepoint.xml");
FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(sharePointDocPath), Url = Path.GetFileName(sharePointDocPath), Overwrite = true };
var uploadedFile=currentRunFolder.Files.Add(newFile);
//currentRunFolder.Update();
context.Load(uploadedFile);
context.ExecuteQuery();
//update
var item = uploadedFile.ListItemAllFields;
item["Title"] = "Updated Title";
item.Update();
context.ExecuteQuery();
Console.WriteLine("---------");
}
推荐阅读
- html - 我如何解决我在 CSS 中的嵌套 div 悬停问题
- javascript - 未捕获(承诺):CKEditorError:ckeditor-duplicated-modules(Angular)
- java - 如何在 Android Studio 中启动 Android 模拟器?
- blazor - 是否可以使用 Bootstrap 模式窗口在 Blazor 页面中使用由 HTML 组成的字符串变量?
- upload - 使用上传范围生成访问令牌的问题
- c# - EF Core,如何更新与同一实体具有一对多和多对多关系的表中的记录
- python - 如何计算具有 3 个有效值的 NxM 网格的不同组合
- git - Git 在推拉后删除了错误包含在 gitignore 中的文件,它们可以恢复吗?
- sqlalchemy - SQL Alchemy Alembic 卡住了运行迁移
- c++ - Prim算法应用