次の方法で共有


Add 1000 folders with unique permission in a List

Once one of my friends in our SharePoint admin team got a requirement to test some permission setting by adding 1000 folders and a file in each folder with the corresponding template which that library was using. Also, he wants to add each folder with unique permission by breaking the inheritance from the parent list.

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.breakroleinheritance.aspx

What he was doing manually was,

1. Creating a SharePoint group with view only permission.

2. Add some users in to that group

3. Break the inheritance of the List and add only that group to the permission list.

4. Thus the users can only view the list they can’t see any menus except Actions menu.

5. Now he want to add more than 1000 folders with unique permission in that list

6. Unique permission was – Every users in that group will get contribute permission in each folders

7. Thus the users in that group can only view the list but they can edit the items.

For this, I have developed a .NET console based application to automate the 2-6 steps.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Text;
    4: using Microsoft.SharePoint;
    5:  
    6: namespace ConsoleApplication1
    7: {
    8:     class Program
    9:     {
   10:         static void Main(string[] args)
   11:         {
   12:             Console.WriteLine("Enter your site URL" );
   13:             string strSiteName = Console.ReadLine();
   14:  
   15:             Console.WriteLine("Enter your document library name");
   16:             string strDocumentLibrary = Console.ReadLine();
   17:  
   18:             Console.WriteLine("Enter your group name");
   19:             string strGroup = Console.ReadLine();
   20:             
   21:             //creating the 1000 folder and breaking the security permission.
   22:             DotheFunctionality(strSiteName, strDocumentLibrary, strGroup);
   23:             
   24:             Console.WriteLine("Done! 1000 folders are created successfully with unique permissions!!!");
   25:             Console.ReadLine();
   26:  
   27:         }       
   28:  
   29:         public static void DotheFunctionality(string strSiteName, string strDocumentLibrary, string strGroup)
   30:         {
   31:             SPSite WebApp = new SPSite(strSiteName);
   32:  
   33:             SPWeb Site = WebApp.OpenWeb();
   34:  
   35:             SPList list = Site.Lists[strDocumentLibrary];
   36:  
   37:             SPContentTypeCollection oContentTypes = list.ContentTypes;
   38:  
   39:             String url = list.RootFolder.ServerRelativeUrl.ToString();
   40:  
   41:  
   42:             for (int i = 0; i <= 999; i++)
   43:             {
   44:                 WebApp.AllowUnsafeUpdates = true;
   45:                 Site.AllowUnsafeUpdates = true;
   46:  
   47:                 string strFolderName = "Folder" + i.ToString();
   48:                 SPListItem newFolder = list.Items.Add(url, SPFileSystemObjectType.Folder, strFolderName);
   49:                 newFolder.Update();
   50:  
   51:                 AddOneDocument(newFolder.Folder, oContentTypes, Site);
   52:  
   53:                 if (!newFolder.HasUniqueRoleAssignments)
   54:                 {
   55:                     
   56:                     newFolder.ResetRoleInheritance();
   57:                     newFolder.BreakRoleInheritance(false);
   58:                     
   59:                 }
   60:  
   61:                 SPRoleDefinitionCollection roleDefinitions = Site.RoleDefinitions;
   62:                 SPRoleAssignmentCollection roleAssignments = newFolder.RoleAssignments;
   63:  
   64:                 SPGroup grp = Site.Groups[strGroup];
   65:                 SPRoleAssignment roleAssignment = new SPRoleAssignment(grp);
   66:                 SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
   67:  
   68:                 roleDefBindings.Add(roleDefinitions["Contribute"]);
   69:                 roleAssignments.Add(roleAssignment);
   70:  
   71:                 WebApp.AllowUnsafeUpdates = false;
   72:                 Site.AllowUnsafeUpdates = false;
   73:             }
   74:         }
   75:             private static void AddOneDocument(SPFolder newFolder,SPContentTypeCollection oContentTypes, SPWeb oWeb)
   76:             {
   77:                 
   78:                 //loop through each content type and create a file based on that content type    
   79:  
   80:                 foreach (SPContentType oContentType in oContentTypes)
   81:                 {
   82:  
   83:                     if (oContentType.DocumentTemplateUrl != string.Empty)
   84:                     {
   85:  
   86:                         // this line does the addition of files . DocumentTemplateUrl will give the template.doc's location which is /LastTest/Shared%20Documents/Forms/template.doc
   87:  
   88:                         SPFile oFile = newFolder.Files.Add("TestDoc1.doc", oWeb.GetFile(oContentType.DocumentTemplateUrl).OpenBinary(), true);
   89:                         
   90:                         SPListItem oItem = oFile.Item;
   91:                         //just updating the title 
   92:                         oItem["Title"] = "Test";
   93:                         oItem.Update();
   94:  
   95:                     }
   96:  
   97:                 }   
   98:  
   99:             }
  100:         }
  101:  
  102:     }
  103:  

I hope any part of the above code may help you in some scenario J

Comments