我尝试在Sharepoint在线列表下创建文件夹结构。 我根据这篇文章创建了一个名为Shared1的列表:
我正在使用此代码尝试创建底层结构:
seed
执行后我得到了这个错误:
using (var clientContext = new ClientContext("https://myEnv.sharepoint.com"))
{
var passWord = new SecureString();
foreach (char c in "myPSW".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("myAcc@myenv.onmicrosoft.com", passWord);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
var folder = CreateFolder(clientContext.Web, "Shared1", "FolderA/SubFolderA/SubSubFolderA");
}
/// <summary>
/// Create Folder client object
/// </summary>
/// <param name="web"></param>
/// <param name="listTitle"></param>
/// <param name="fullFolderUrl"></param>
/// <returns></returns>
public static Folder CreateFolder(Web web, string listTitle, string fullFolderUrl)
{
if (string.IsNullOrEmpty(fullFolderUrl))
throw new ArgumentNullException("fullFolderUrl");
var list = web.Lists.GetByTitle(listTitle);
return CreateFolderInternal(web, list.RootFolder, fullFolderUrl);
}
private static Folder CreateFolderInternal(Web web, Folder parentFolder, string fullFolderUrl)
{
var folderUrls = fullFolderUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string folderUrl = folderUrls[0];
var curFolder = parentFolder.Folders.Add(folderUrl);
web.Context.Load(curFolder);
web.Context.ExecuteQuery();
if (folderUrls.Length > 1)
{
var subFolderUrl = string.Join("/", folderUrls, 1, folderUrls.Length - 1);
return CreateFolderInternal(web, curFolder, subFolderUrl);
}
return curFolder;
}
public static Folder GetFolder(Web web, string fullFolderUrl)
{
if (string.IsNullOrEmpty(fullFolderUrl))
throw new ArgumentNullException("fullFolderUrl");
if (!web.IsPropertyAvailable("ServerRelativeUrl"))
{
web.Context.Load(web, w => w.ServerRelativeUrl);
web.Context.ExecuteQuery();
}
var folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + fullFolderUrl);
web.Context.Load(folder);
web.Context.ExecuteQuery();
return folder;
}
我错过了什么?
答案 0 :(得分:1)
您需要在CreateFolder方法中加载列表。
// DW: remove e
std::string trim_real(std::string str) {
// DW: all to lowercase
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
// DW: find "e" and "."
size_t pe = str.find_first_of("e");
std::string se = str.substr(pe + 1, str.size() - (pe + 1));
int de = std::stoi(se);
// DW: get string without "e"
std::string sc = str.substr(0, pe);
std::cout << "sc is " << sc << std::endl;
// DW: remove uneccessary 0
size_t pf0 = sc.find_first_not_of("0");
size_t pl0 = sc.find_last_not_of("0");
std::string sc_trim = sc.substr(pf0, pl0 - pf0 + 1);
std::cout << "sc_trim is " << sc_trim << std::endl;
// DW: move "." according to e
size_t pp = sc_trim.find_first_of(".");
if (pp == std::string::npos) {
// DW: this means there is no "."
pp = 0;
}
int pp_tobe = pp + de;
sc_trim.erase(pp, 1);
if (de > 0) {
// DW: should move "." to the right
if (pp_tobe < sc_trim.size()) {
sc_trim.insert(pp_tobe, ".");
} else {
sc_trim += std::string(pp_tobe - sc_trim.size() + 1, '0');
sc_trim += ".0";
}
} else {
// DW: should move "." to the left
if (pp_tobe >= 0) {
sc_trim.insert(pp_tobe, ".");
} else {
sc_trim = "." + std::string(0 - pp_tobe, '0') + sc_trim;
}
}
if (sc_trim == ".") {
// DW: this means sc_trim is actually 0 now
sc_trim = "0.0";
}
return sc_trim;
}
答案 1 :(得分:1)
您收到这些错误的原因与指定的错误网址有关。虽然您的列表存在于子站点Shared1
上,但您尝试在根站点上访问它,下图说明了它
因此,解决方案是初始化适当网站的客户端上下文,因此请替换:
using (var clientContext = new ClientContext("https://myEnv.sharepoint.com/"))
{
//...
}
与
using (var clientContext = new ClientContext("https://myEnv.sharepoint.com/Shared1"))
{
//...
}
假设您要为列表中的列表创建文件夹
Shared1
子网站
示例强>
以下示例演示了如何在Documents
子网站News
子网站下的https://contoso.sharepoint.com/news
库中创建文件夹层次结构:( Archive
|
2009
|
09
)
using (var ctx = GetContext("https://contoso.sharepoint.com/news", userName, password))
{
var list = ctx.Web.Lists.GetByTitle("Documents");
var folder = list.CreateFolder("Archive/2015/09");
Console.WriteLine(folder.ServerRelativeUrl);
}
用法
public static ClientContext GetContext(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var ch in password) securePassword.AppendChar(ch);
return new ClientContext(webUri) { Credentials = new SharePointOnlineCredentials(userName, securePassword) };
}
,其中
ListExtensions.cs
using System;
using Microsoft.SharePoint.Client;
namespace SharePoint.ClientExtensions
{
public static class ListExtensions
{
/// <summary>
/// Create Folder in List
/// </summary>
/// <param name="list"></param>
/// <param name="folderUrl"></param>
/// <returns></returns>
public static Folder CreateFolder(this List list, string folderUrl)
{
if (string.IsNullOrEmpty(folderUrl))
throw new ArgumentNullException("folderUrl");
if (!list.IsPropertyAvailable("RootFolder"))
{
list.Context.Load(list.RootFolder);
list.Context.ExecuteQuery();
}
return CreateFolderInternal(list.RootFolder,folderUrl);
}
private static Folder CreateFolderInternal(Folder parentFolder, string folderUrl)
{
var folderUrlParts = folderUrl.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var curFolder = parentFolder.Folders.Add(folderUrlParts[0]);
parentFolder.Context.Load(curFolder);
parentFolder.Context.ExecuteQuery();
if (folderUrlParts.Length > 1)
{
var subFolderUrl = string.Join("/", folderUrlParts, 1, folderUrlParts.Length - 1);
return CreateFolderInternal(curFolder, subFolderUrl);
}
return curFolder;
}
}
}
档案:
JSON.stringify(err)