Today I am trying to write an eventhandler about the document library.
Requirement Source: users can create or upload documents in the document library, but cannot edit or delete them. Now they want to edit and delete their own documents.
I created a console application first.ProgramWhen this program is executed, it cyclically grants full control permissions to the creators of all files in the document library, and I will continue to complete the subsequent eventhandler, please help me to test whether such writing is feasible. Note that during the test, create a console application and copyCodeThen, replace the site URL with the actual URL, and the document library name with the actual document library name.
Program source code:
Using system;
Using system. Collections. Generic;
Using system. text;
Using Microsoft. SharePoint;
namespace testlistfolder {
class program {
static void main (string [] ARGs) {
spsecurity. codetorunelevated mycode = new spsecurity. codetorunelevated (testeven thandler);
spsecurity. runwithelevatedprivileges (mycode);
}
Public static void testeven thandler (){
Spsite site = new spsite (site URL );
Spweb web = site. rootweb; // the top-level website is opened.
Splist list = web. Lists [document library name];
For (Int J = 0; j <list. Items. Count; j ++ ){
Splistitem item = List. items [J];
Spfile file = List. Items. file;
Spuser user = file. Author;
Sproleassignment SA = new sproleassignment (spprincipal) User );
Sproledefinition role = web. roledefinitions ["Full Control"];
SA. roledefinitionbindings. Add (role );
Item. breakroleinheritance (true );
Item. roleassignments. Add (SA );
}
}
}
}
This is all the code of the console application, and pay attention to adding a reference to SharePoint