Application Scenario: when creating a folder, you want to automatically generate subfolders and files under the folder. The subfolders and files are placed in a document library.
Solution: Apply event handler. Code Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using Microsoft. SharePoint;
Namespace ***.***.***
{
Public Class Incluenthandler: spitemeventreceiver
{
// Function: automatically generates subfolders and files under the newly created folder when creating a folder. subfolders and files are placed in a template document library.
Public Override Void Itemadded (spitemeventproperties properties)
{
// Base. itemadded (properties );
Spsecurity. runwithelevatedprivileges ( Delegate ()
{
Using (Spsite site = New Spsite (properties. siteid ))
{
Using (Spweb = Site. openweb (properties. openweb (). Id ))
{
Try
{
// Get template Document Library
Spdocumentlibrary templetedoclib = (Spdocumentlibrary) web. Lists [ " Project acceptance document template " ];
// Obtain the document library for the operation.
Spdocumentlibrary targetdoclib = (Spdocumentlibrary) web. Lists [properties. listid];
// Obtain the path of the new folder.
String Targetdoclibpath = Properties. weburl + " / " + Properties. afterurl + " / " ;
Web. allowunsafeupdates= True;
// When creating a folder, you can create a folder in the root directory of the document library.
If (Properties. listitem. contenttype. Name = " Folder " | Properties. listitem. contenttype. Name = " Project folder " ) && Properties. listitem. Folder. parentfolder. tostring () = Targetdoclib. rootfolder. tostring ())
{
// Set the project folder permissions to allow some people to view, edit, delete, and perform other operations.
If ( ! Properties. listitem. hasuniqueroleassignments)
{
Properties. listitem. breakroleinheritance ( False );
}
Else
{
//Later, we use the following. After the first update, item has its own permissions. hasuniqueroleassignments is 1.
//First, re-inherit the upper-level permissions, and then remove all permissions and re-assign them
properties. listitem. resetroleinheritance ();
properties. listitem. breakroleinheritance ( false );
}
Spuser user=Web. Users. getbyid (properties. currentuserid );
// Obtain the login name of the user who triggered the event
Sproleassignment ra1 = New Sproleassignment (Web. ensureuser (user. loginname ));
Ra1.roledefinitionbindings. Add (Web. roledefinitions [ " Full Control " ]);
Properties. listitem. roleassignments. Add (ra1 );
// The following describes how to assign full control permissions to individual users.
// First, obtain the project owner's value. The original value is as follows: 0758 # km \ test.
String Projectman = Properties. listitem [ " Project leader " ]. Tostring ();
String [] Userid = Project. Split ( ' # ' );
Sproleassignment ra3 = New Sproleassignment (Web. ensureuser (userid [ 1 ]);
Ra3.roledefinitionbindings. Add (Web. roledefinitions [ " Full Control " ]);
Properties. listitem. roleassignments. Add (ra3 );
Copy (Web, templetedoclib, templetedoclib. rootfolder,0, Targetdoclibpath );
// itemupdated is executed 10 times, use the following method to solve
This . disableeventfiring ();
// set the approval status of the newly created folder to pass
// properties. listitem. moderationinformation. status = spmoderationstatustype. approved;
properties. listitem. update ();
This. Enableeventfiring ();
Targetdoclib. Update ();
}
// Spfolder newfolder = web. getfolder (targetdoclibpath );
// Newfolder
}
Catch (Exception ex)
{
}
}
}
});
}
// Traverse folders and files, and create folders and copy files
Static Void Copy (spweb, spdocumentlibrary doclib, spfolder folder, Int Level, String Targetdoclib)
{
Splistitemcollection items = Doclib. getitemsinfolder (doclib. defaultview, folder );
Foreach(Splistitem itemInItems)
{
If(Item. filesystemobjecttype=Spfilesystemobjecttype. folder)
{
Spfolder targetfolder=Web. getfolder (targetdoclib );
Spfolder newfolder=Targetfolder. subfolders. Add (item. Name );
Newfolder. item. Update ();
// Copy the files in this directory to the target folder.
Spfilecollection spfiles = Item. Folder. files;
For ( Int I = Spfiles. Count - 1 ; I > - 1 ; I -- )
{
spfiles [I]. copyto (targetdoclib + item. name + " / " + spfiles [I]. name);
}
Copy (Web, doclib, item. folder, Level+ 1, Targetdoclib+ "/" +Item. Name );
}
}
}
}
}< br>