SharePoint, Document Library and SQL Server

Setting Up

1. Download the zip file ("art1041806.zip" - see "Downloads" link on the left) and unzip it to the root of your system drive. Zip file contains the complete source code and the installation scripts.

2 .You must create a database before you start working with the code. Open your SQL Server and create a blank database and name it as “SPSDocLibHandler”.

3. Add a table to this database. Table name is “DocInfo”. Create following fields in the table:

 

* Please note that “id” is an identity column.

Here is the SQL script to create this table:

CREATE TABLE [dbo].[DocInfo] ( 
[id] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Modified] [datetime] NULL ,
[ModifiedBy] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[ApproverComments] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[ApprovalStatus] [int] NULL
) ON [PRIMARY]

You will find this script in “db.sql” file that is located in the “DB” folder inside the application folder. “db.sql” file also contains the script to create a stored procedure that will be used in our code. I would suggest that you just create the database and then run the “db.sql” file to create the required table and stored procedure but that’s up to you. You can follow the steps listed here to do it manually.

4. Create a stored procedure called as “sp_AddDocInfo”. Here is the SQL script:

CREATE PROCEDURE dbo.sp_AddDocInfo 
(
@Name varchar(50),
@Modified datetime,
@ApprovalStatus int,
@ModifiedBy varchar(50),
@ApproverComments varchar(255)
)
as
insert into DocInfo values (@Name,@Modified,@ModifiedBy,@ApproverComments,@ApprovalStatus)

5. There is a file called as “db.config” in the root of the application folder. This one line configuration file contains connection string to your SQL Server. Edit this file in your notepad and add your SQL Server connection details.

Data Source = YourSQLServerName (Replace this with your SQL Server’s name) 
Initial Catalog = SPSDocLibHandler (This is the database name)
User Id = sa (Replace this with your own user id)
Password = (Add password here, if any)

6. Copy this file “db.config” to the root of your system drive. For example, if your system drive is “C:”, copy the file to the root of “C:” drive. If your system drive is “D:” then copy the file to the root of “D:” drive.

7. There is a file named as “Install SPSDocLibHandler.bat” in your application folder. Open this file in notepad and see if you need to make any changes. This file installs your DLL into GAC, especially check the path where gacutil.exe is located on your computer. Run this file to install DLL into GAC. You can install the assembly into GAC manually as well. “SharePoint.DocLib.Handler.dll” can be found in the “bin\debug” folder inside your application folder.

8. After installing the DLL into GAC, you need to bind your assembly to the document library where this assembly will be used. Before doing that, you also need to enable “Event Handlers” on the virtual server where your SharePoint resides. If you don’t know how to do this then read this article on Microsoft’s site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/tsptDocLibEvents_SV01034973.asp

For your convenience, I am listing the steps required to enable the “Event Handlers” on your virtual server.

a) Go to SharePoint Central Administration page.

b) Click “Configure virtual server settings from the virtual server list page” under “Portal Site and Virtual Server Configuration”. This will open the “Virtual Server List” page:

c) Click on your virtual server from the list shown on this page.

d) Click on “Virtual server general settings” link.

On the bottom of the page, you will find “Event Handlers” section. Select “On” in the Event Handlers section to enable use of event handlers on the virtual server.

Now that you have enabled “Event Handlers” on your virtual server, your next step is to bind Your assembly to the document library. Following are the steps required to accomplish this:

a) From a view of the document library to which you want to attach the event handler assembly, click Modify settings and columns.

b) Click “Change advanced settings” on the page that opens for customizing the document library.

c) On the Document Library Advanced Settings page, type the strong name for the assembly in the Assembly Name box. In our case assembly name box would contain:

SharePoint.DocLib.Handler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=32bb15c33a2f8eca. 

You can get the values used in the strong name by right-clicking the DLL of your assembly in the Local_Drive:\WINDOWS\assembly directory and then clicking Properties on the shortcut menu.

d) Type the fully qualified, case-sensitive name of the class in the Class Name box, which in this case is SharePoint.DocLib.Handler.LibraryEventSink

e) Reset IIS by typing iisreset at a command prompt.

9. You are now ready to test the application. Insert a document in your library and then go to your SQL Server, open the table and you will see the details of the newly added document. You can create reports based on the data in this table for the senior management. This is just one example of what you can do with SharePoint Document Libraries and the “Event Handler Toolkit”.

You might also like...

Comments

About the author

S.S. Ahmed United States

S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Java is to JavaScript what Car is to Carpet.” - Chris Heilmann