在 SharePoint 2013 中以编程方式创建文件夹
目前我有代码在运行时在 Documents
目录中创建一个文件夹:
Currently I have code that creates a Folder in the Documents
directory when run:
using (var context = new Microsoft.SharePoint.Client.ClientContext(sharePointSite))
{
context.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(user, password);
Web web = context.Web;
Microsoft.SharePoint.Client.List docs = web.Lists.GetByTitle(<upper level folder>);
docs.EnableFolderCreation = true;
docs.RootFolder.Folders.Add(folderName);
context.ExecuteQuery();
return true;
}
我在使用此代码创建的文件夹中创建子文件夹时遇到问题.因此,如果我想在 Documents
下创建一个名为 Feb
的文件夹,就可以这样做.但是如果我想在新文件夹 Feb
下创建一个名为 Week 2
的文件夹.它不会那样做.我收到此错误:
I am having troubles creating sub folders in folders that I have created using this code already. So like if I wanted to create a folder called Feb
under Documents
this would do that. But if I wanted to create a folder called Week 2
under the new folder Feb
. It won't do that. I get this error:
{网址为https://my.sharepoint.com/sites/labels"的站点上不存在列表Feb"."}
我意识到问题可能出在 docs.RootFolder.Folders.Add(folderName);
因为 Feb
不会是根文件夹,当它寻找它时将抛出异常.
I realize that the problem is probably docs.RootFolder.Folders.Add(folderName);
because Feb
wouldn't be the root folder and when it looks for it an exception would be thrown.
所以我希望有人能用一些代码帮助我将子文件夹添加到已经创建的文件夹中.我正在使用 Visual Stuidos 2010,否则无法升级到 2012.我有可以在 VS 2010 中引用的 2013 Microsoft.Sharepoint.Client
dll.
So I was hoping that someone could help me out with some code to add sub folders to already created folders. I am using Visual Stuidos 2010 and can't upgrade to 2012 otherwise I would. I have the 2013 Microsoft.Sharepoint.Client
dll's that can be referenced in VS 2010.
如何在 SharePoint 2010/2013 中通过 CSOM 创建文件夹(包括嵌套)
/// <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;
}
用法
using (var ctx = new ClientContext("https://contoso.onmicrosoft.com/"))
{
ctx.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials("username", "password");
var folder = CreateFolder(ctx.Web, "Shared Documents", "FolderA/SubFolderA/SubSubFolderA");
}
如何获取文件夹客户端对象
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;
}
用法
var existingFolder = GetFolder(ctx.Web, "Shared Documents/FolderA/SubFolderA/SubSubFolderA");