none
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: https://msdn.microsoft.com/en-us/pnp_articles/modern-experience-customizations  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://***.sharepoint.com/sites/dennis";
    var login = "dennis@***.onmicrosoft.com";
    var password = "****";
    
    var securePassword = new SecureString();
    
    foreach (char c in password)
    {
    	securePassword.AppendChar(c);
    }
    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);
    ctx.Load(newLib);
    ctx.ExecuteQuery();
    
    var myLibrary = ctx.Web.Lists.GetByTitle("Library");
    myLibrary.ContentTypesEnabled = true;
    myLibrary.ListExperienceOptions = ListExperience.NewExperience;
    myLibrary.Update();
    ctx.ExecuteQuery();
    

    https://msdn.microsoft.com/en-us/pnp_articles/modern-experience-customizations-customize-lists-and-libraries

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

    Install-Package Microsoft.SharePointOnline.CSOM
    https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/16.1.6112.1200

    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);
        cpx.ExecuteQuery();
        list.BreakRoleInheritance(true, false);
        RoleDefinitionBindingCollection collRoleDefinitionBinding = 
             new RoleDefinitionBindingCollection(cpx);
        collRoleDefinitionBinding.Add(roletypes);
        list.RoleAssignments.Add(GRoup1, collRoleDefinitionBinding);
        cpx.ExecuteQuery();
    }

    http://sharepoint.stackexchange.com/questions/167104/assign-unique-permission-to-sharepoint-2013-list-using-client-side-code-c/167109

    Best Regards,

    Dennis


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

    Wednesday, February 15, 2017 7:28 AM
    Moderator
  • 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