Creazione di cartelle a livello programmatico in SharePoint 2013
-
20-12-2019 - |
Domanda
Attualmente ho il codice che crea una cartella nella directory Documents
quando viene eseguito:
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;
}
.
Sto avendo problemi a creare sottocartelle nelle cartelle che ho creato utilizzando questo codice già.Così mi piace se volessi creare una cartella chiamata Feb
sotto Documents
, questo lo farebbe.Ma se volevo creare una cartella chiamata Week 2
sotto la nuova cartella Feb
.Non lo farà.Ottengo questo errore:
{"List 'Feb' does not exist at site with URL 'https://my.sharepoint.com/sites/labels'."}
Mi rendo conto che il problema è probabilmente docs.RootFolder.Folders.Add(folderName);
perché Feb
non sarebbe la cartella principale e quando lo cerca un'eccezione verrà lanciata.
Così speravo che qualcuno potesse aiutarmi con un codice per aggiungere le cartelle secondarie alle cartelle già create.Sto usando Visual Stuidos 2010 e non riesco a passare al 2012 altrimenti lo farei.Ho le DLL Microsoft.Sharepoint.Client
2013 che possono essere referenziate in VS 2010.
Soluzione
Come creare una cartella (incluso nidificato) tramite CSOM in SharePoint 2010/2013
/// <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;
}
.
Uso
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");
}
.
Come ottenere l'oggetto client della cartella
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;
}
.
Uso
var existingFolder = GetFolder(ctx.Web, "Shared Documents/FolderA/SubFolderA/SubSubFolderA");
. Altri suggerimenti
Puoi creare cartelle come questa:
Microsoft.SharePoint.Client.List docs = web.Lists.TryGetList("upper level folder");
FolderCollection folderColl = docs.RootFolder.SubFolders;
Folder newFolder = folderColl.Add("upper level folder/Feb/Week 2");
.