1

In my research and coding I can programmatically create a document library/list on 0365 SharePoint Online in this fashion:

using (var ctx = new ClientContext("Http://..."))
{
    ctx.Credentials = new SharePointOnlineCredentials("UserName", "SecurePwd");

    var list = new ListCreationInformation()
    {
          Title = "My Title",
          Description = "My Desicription",
          TemplateType = asDocumentLibrary ? 101 : 100, 
          QuickLaunchOption = QuickLaunchOptions.On
    };

    ctx.Web.Lists.Add(list);

    ctx.ExecuteQuery();
}

In the above code the advance option of QuickLaunchOption is a property which I can change, and do.

But there are no other advance options. Do I have to query after the ExecuteQuery and pull down that newly created list in SPList form? Or is there a better way to change advance attributes of the list during creation?

1

You do not have to execute query after ctx.Web.Lists.Add(list);. Simply store the list into a variable, and change the settings you need to change. You can do a single ExecuteQuery(); at the end. I wish I had some links to support this, but all I can say is that I've been able to create a list, break role inheritance and add custom role assignments all before calling ExecuteQuery(); in the past:

var listCreationInformation = new ListCreationInformation
{
    Title = title,
    Description = description,
    ListTemplate = listTemplate,
    TemplateFeatureId = listTemplate.FeatureId,
    TemplateType = listTemplate.ListTemplateTypeKind
};
var list = web.Lists.Add(listCreationInformation);
list.BreakRoleInheritance(false, true);
list.RoleAssignments.Add(owners, new RoleDefinitionBindingCollection(ctx) {contribute});
list.RoleAssignments.Add(members, new RoleDefinitionBindingCollection(ctx) { contribute });
list.RoleAssignments.Add(bhtg, new RoleDefinitionBindingCollection(ctx) { contribute });
ctx.ExecuteQuery();
  • In my testing, one must also call Update() on the list before the ExecuteQuery. I was not changing any of the properties you demonstrated, so I don't know if it is a requirement for those to call update or not. Regardless thanks for the correct answer. – ΩmegaMan May 19 '17 at 22:37
  • This is just speculation, but perhaps because my updates were done with methods, I didn't need the list.Update() call. Perhaps it's only needed for property assignment. – wjervis May 21 '17 at 3:42

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.