Since releasing the new Advanced SharePoint Development course, I have been working on various items to add to it! One of them is using Enterprise Library with SharePoint Event Handlers to implment document encryption! Here is the lab!
Module #15: SharePoint Event Handlers Lab #3
Course: Advanced SharePoint Development Estimated Time to Complete: 45 minutes Objectives: · Create an Event Handler feature that encrypts documents Operating Notes: none Deliverables: · None
|
Overview: SharePoint has exposed several events that we can tie into with our own code. Let’s see how to do this!
Exercise 1 – Create a new SharePoint Event Handler
Purpose: Create and register an event handler for SharePoint events
Result: A new event handler
Task 1 – Create a new Visual Studio Class Library
- Open Visual Studio, select New->Project
- Select Class Library from the project templates
- Name the project EncryptDocumentEventReceiver
- For location, c:asp
- Add a reference to
- Microsoft.SharePoint.dll
- Microsoft.Practices.EnterpriseLibrary.Common
- Microsoft.Practices.EnterpriseLibrary.Security
- Microsoft.Practices.EnterpriseLibrary.Security.Cryptography
- Add a using statement to
- Micorosft.SharePoint
- Microsoft.Practices.EnterpriseLibrary.Common;
- Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
- Microsoft.Practices.EnterpriseLibrary.Security;
- Microsoft.Practices.EnterpriseLibrary.Security.Configuration;
- Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
- Rename class1 to EncryptDocument
- Make EncryptDocument inherit from SPItemEventReceiver
- Add the following methods:
|
- Right Click the project, select Properties
- Select the Signing tab
- Click “Sign the assembly”
- Select <New…>, type EncryptDocumentEventReceiver.snk
- Uncheck “Password…”
- Build the project, Press Ctrl-Shift-B
- Copy the C:ASPEncryptDocumentEventReceiverEncryptDocumentEventReceiverinDebugEncryptDocumentEventReceiver.dll to the c:windowsassembly directory (this installs the assembly in the gac)
Task 2 – Extend the web.config file
- Open Enterprise Library console
- Click the open button, browse to the c:inetpubwwwrootwssvirtualdirectories100web.config file
- Click Open
- Right click the “C:inetpub…” node, select New->Cryptography Application Block
- Right click the Symmetric Provider node, select New->Symmetric Algorithm Provider
- Select TripleDESCryptoServiceProvider, Click Ok
- In the Cryptographic key wizard, Click Next
- Click Generate
- Click Next
- Name the key “EncryptIt.key”, Click Save
- Click Next
- Select Machine mode, Click Finish
- Click the Cryptography Application Block node
- For DefaultSymmetricCryptoProvider, select TripleDESCryptoServiceProvider
Task 3 – Create a console application to register the event handler
- In Visual Studio, click Add->New->Project
- Select a Windows Console Application
- Modify the Main method to this:
|
- Compile the project (Ctrl-Shift-B)
- Run the Console Project
- Reset IIS
- Open the http://local
host:100 site - Create a text document, upload it to the document library
- Open the document, notice it is encrypted
- Check out the document
- Open the document, it is now decrypted!