1

How can one create a document library from custom library template with Folder structure inside using CSOM?

I have created my template and when creating an instance of that manually folders are created. The challenge is when doing it programatically the folder structure doesn't show.

This is what I have thus far:

var listTemplate = clientContext.Site.GetCustomListTemplates(clientContext.Site.RootWeb).GetByName("Test");

clientContext.Load(listTemplate, lt=> lt.Name, lt => lt.FeatureId, lt => lt.ListTemplateTypeKind);
clientContext.ExecuteQuery();

listCreationInfo.TemplateFeatureId = listTemplate.FeatureId;
listCreationInfo.TemplateType = listTemplate.ListTemplateTypeKind;

List newList = oWeb.Lists.Add(listCreationInfo);
newList.EnableFolderCreation = true;
newList.ContentTypesEnabled = true;

clientContext.ExecuteQuery();

Any help would be appreciated.

1

Make sure the custom library template include content, and then using the code below to create new library base on the custom library template.

string siteUrl = "https://tenant.sharepoint.com/sites/lz";
var login = "lz@tenant.onmicrosoft.com";
var password = "xx";

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

var listTemplate = clientContext.Site.GetCustomListTemplates(clientContext.Site.RootWeb).GetByName("Test");

clientContext.Load(listTemplate, lt => lt.Name, lt => lt.FeatureId, lt => lt.ListTemplateTypeKind);
clientContext.ExecuteQuery();

ListCreationInformation listCreationInfo = new ListCreationInformation();
listCreationInfo.Title = "NewTestLibrary";
listCreationInfo.ListTemplate = listTemplate;
List newList = clientContext.Web.Lists.Add(listCreationInfo);

newList.ContentTypesEnabled = true;
newList.Update();
clientContext.ExecuteQuery();
  • If my reply help you, please mark the reply as answer, thanks. – LZ_MSFT Jan 18 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.