How to "programmatically" create document libraries with unique permissions in modern SharePoint sites? RRS feed

  • Question

  • Hello Forum

    I have two questions about the modern SharePoint sites (Which comes with O365 Groups):

    1) How can I programmatically create document libraries in a modern SharePoint site ?

    2) How can I programmatically set unique permissions on document libraries inside a modern SharePoint site?

    I have already checked:  But, I didn't find my answers there.  

    Tuesday, February 14, 2017 7:17 AM

All replies

  • Hi,

    To create a document library with new experience, the following code for your reference:

    var targetSiteURL = @"https://***";
    var login = "dennis@***";
    var password = "****";
    var securePassword = new SecureString();
    foreach (char c in password)
    SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
    ClientContext ctx = new ClientContext(targetSiteURL);
    ctx.Credentials = onlineCredentials;
    Web web = ctx.Web;
    ListCreationInformation lci = new ListCreationInformation();
    lci.Description = "My own DocLib";
    lci.Title = "Library";
    lci.TemplateType = 101;
    List newLib = ctx.Web.Lists.Add(lci);
    var myLibrary = ctx.Web.Lists.GetByTitle("Library");
    myLibrary.ContentTypesEnabled = true;
    myLibrary.ListExperienceOptions = ListExperience.NewExperience;

    To use ListExperienceOptions, please run the following command in the Package Manager Console in your solution.

    Install-Package Microsoft.SharePointOnline.CSOM

    About setting permission, please check this:

    public static void AddPermission(ClientContext cpx, List list,
                                     string groupName, string permissionName)
        //  Web web = cpx.Web;
        //  Site site = cpx.Site;
        var GRoup1 = cpx.Web.SiteGroups.GetByName(groupName);
        var roletypes= cpx.Web.RoleDefinitions.GetByType(RoleType.Contributor);
        list.BreakRoleInheritance(true, false);
        RoleDefinitionBindingCollection collRoleDefinitionBinding = 
             new RoleDefinitionBindingCollection(cpx);
        list.RoleAssignments.Add(GRoup1, collRoleDefinitionBinding);

    Best Regards,


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Wednesday, February 15, 2017 7:28 AM
  • Hi Dennis,

    Maybe you missed my question. My question was not about creating a document library with new experience.

    My question is: When you create Office 365 Group, you get a Modern SharePoint site: How to programmatically create document library and assign unique permissions to it in the Modern SharePoint site ?

    Have you worked with these Modern Team sites which come with Groups? They are built on Graph API and your code above doesn't work with them !

    Friday, February 17, 2017 8:06 PM