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: