Creating Item Level Security for an Event Receiver based upon metadata (Part 1)

clock June 3, 2009 01:39 by author steveboldt

First step in the process is to create a basic feature for MOSS. For more information in regarding the template, view the information here: http://www.stevenboldt.com/blog/post/2009/06/01/Creating-a-SharePoint-Custom-Event-Handler-Basics.aspx

The steps I took in creating item level security utilized a custom list in MOSS. This way, the user can customize the setting of the feature and also the group/user permissions that will be set. This can also be done using a config file or other possibilities, but for my example, I used a list.

Configuration List Setup

Browse to the SharePoint site you have the Security event handler feature activated and create a new list called: ItemLevelSecurityConfiguration(or whatever you want to call it). A basic example of what the list should include is as follows:

Column

Type

Required

WebURL

Single line of text

 

SharePointList

Single line of text

 

Group 1

Person or Group

 

Group 2

Person or Group

 

RoleType

Choice

yes

For the columns Group 1 and Group 2, I set the following:

  • Allow multiple section = Yes
  • Allow selection of = People and Groups
  • Choose From = All Users
  • Show Field = Name

For the column RoleType, I set the choice values as follows:

  • Administrator
  • Contribute
  • Guest
  • Reader
  • WebDesigner

For my example, I set the default value as Contribute.

To set the values of the configuration list, observe the following for reference:

  • Title = Name of the entry, usually the same name as the document library
  • WebURL = the web URL of the site that the feature is activated on
  • SharePointList = SharePoint Document library that will incorporate the security event handler feature
  • Group 1 = the groups(or user) that will be given access to the document.
  • Group 2 = the groups(or user) that will be given access to the document.
  • RoleType = type of permissions the groups are given access to the document (default is Contribute).

Connecting to the Configuration List

A method that I created that gathered the information from the configuration list is called SetConfiguration. This method allows the user to pass in 2 parameters, a SPWeb & document library name. 

Since the configuration list shouldn’t change often, I decided to cache the values, so that the setting of the configuration is kept to a minumum. Line 19 shows me inserting the values into the HttpRuntime.Cache, expiring after 1 day (DateTime.Now.AddHours(24)).  Therefore, next time this method is called, it will be able to populate the SPListItem without parsing through the MOSS list. The SPListItem specifies a specific row or item in a list. To determine this row, I used a SPQuery method utilizing CAML to retrieve the SharePointList values where it matches the current document library that the feature is running. I then call the SPList GetItems method to populate a list item collection.

   1: #region SetConfiguration
   2:         private void SetConfiguration(SPWeb sWeb, string docLibName)
   3:         {
   4:             SPListItem configurationListItemForDocLib = HttpRuntime.Cache.Get(docLibName) as SPListItem;
   5:             if (configurationListItemForDocLib == null)
   6:             {
   7:                 //List name of configuration settings
   8:                 SPList spListConfig = sWeb.Lists["ItemLevelSecurityConfiguration"];
   9:  
  10:                 //Query the ItemLevelSecurityConfiguration SharePoint list where the entry matches the current document library.
  11:                 SPQuery spQuery = new SPQuery();
  12:                 spQuery.Query = "<Where><Eq><FieldRef Name='SharePointList' /><Value Type='Text'>" + docLibName + "</Value></Eq></Where>";
  13:                 spQuery.RowLimit = 1;
  14:  
  15:                 SPListItemCollection collListItems = spListConfig.GetItems(spQuery);
  16:                 if (collListItems.Count != 0)
  17:                 {
  18:                     configurationListItemForDocLib = collListItems[0];
  19:                     HttpRuntime.Cache.Insert(docLibName, configurationListItemForDocLib, null, DateTime.Now.AddHours(24), System.Web.Caching.Cache.NoSlidingExpiration);
  20:                 }
  21:             }
  22:  
  23:             if (configurationListItemForDocLib != null)
  24:             {
  25:                 sharePointList = configurationListItemForDocLib["SharePointList"].ToString();
  26:                 Group1 = (SPFieldUserValueCollection)configurationListItemForDocLib["Group 1"];
  27:                 Group2 = (SPFieldUserValueCollection)configurationListItemForDocLib["Group 2"];
  28:                 roleType = configurationListItemForDocLib["RoleType"].ToString();
  29:  
  30:                 applySecurity = true;
  31:             }
  32:             else
  33:             {
  34:                 applySecurity = false;
  35:             }
  36:         }
  37:  
  38:         #endregion

Now we have a customizable list and a connection for this list. The next post will show the ways to apply the security to the individual documents.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Creating a SharePoint Custom Event Handler - Basics

clock June 1, 2009 14:06 by author steveboldt

I’ll review the basic steps on creating a SharePoint custom event handler.

You begin by opening up Microsoft Visual Studio 2005 and create a new class project. Reference the Microsoft.SharePoint assembly and then rename the default.cs class to the name that you want to give to your event handler. Also, remember to add your using statement for Microsoft.SharePoint.

Depending on which type of event handler you are wanting to write, will determine the proper SharePoint class to derive from. A breakdown of the available SharePoint classes to derive from are as follows:

  • List Items – SPItemEventReceiver
  • List Columns – SPListEventReceiver
  • Site – SPWebEventReceiver
  • Email - SPEmailEventReceiver

At this point, you need to make a decision on whether you want to use Asynchronous or Synchronous events in your event handler. In order to determine which one you want, first decide if you want the event to fire before or after the event.

Asynchronous calls occur after the event, and do not block any code being executed in SharePoint. On the flipside, synchronous calls occur before the event, will block code being executed in SharePoint until your custom event handler is completed.

For a list of the available methods for each class, refer to Microsoft’s site:

SPItemEventReceiver Methods
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver_methods.aspx

SPListEventReceiver Methods
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splisteventreceiver_methods.aspx

SPWebEventReceiver Methods
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spwebeventreceiver_methods.aspx

SPEmailEventReceiver Methods
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spemaileventreceiver_methods.aspx

Once you make your decision on what type of event receiver and also what type of method call you will be using, write out the override method . I will discuss further on manipulating particular methods.

Creating a Strong Name Key
After completing your method, you will need to sign the assembly. You can do this by going into solution explorer and right clicking your project and selecting properties. Then, click the Signing tab, then select Sign the Assembly, then select Choose a strong name key file and click “<New…>”. Type a name into the Key file name box (usually the name of your project). It needs to be in the format of xxxxxx.snk. You can set a password if you want, and click OK.

Creating Event Receiver to be installed as a Feature
Right click on your event handler project and select Add / New Folder. Name the folder as the same name as the project or similar. Add to blank XML files under the folder. Name the XML files as elements.xml and the other feature.xml.

In the feature.xml file, it should follow the following format:

<Feature Scope="Web"
    Title="My Event Handler"
    Description ="Description goes here."
    Id="F141E5EA-796E-4807-AB86-43F5DC560371"
    xmlns="http://schemas.microsoft.com/sharepoint/">
    <ElementManifests>
        <ElementManifest Location="elements.xml"/>
    </ElementManifests>
</Feature>

For further explanation, refer to the Microsoft site for all the available nodes.
http://msdn.microsoft.com/en-us/library/ms436075.aspx

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Receivers ListTemplateId="101">
        <Receiver>
            <Name>MyEventHandler</Name>
            <Type>ItemAdded</Type>
            <SequenceNumber>10000</SequenceNumber>
            <Assembly>MyEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1cd942f82aed8c82</Assembly>
            <Class>MyEventHandler.EventHandler</Class>
            <Data></Data>
            <Filter></Filter>
        </Receiver>
    </Receivers>
</Elements>

In the elements.xml, the ListTemplateId refers to which list to associate the event handler. 101 = document library. For a description of the available id, see here: http://msdn.microsoft.com/en-us/library/ms462947.aspx

The <Type> node refers to the method that will be raised when the action takes place. For multiple methods, you will need multiple <Receiver> nodes. <SequenceNumber>node refers to the order of execution.

Add a text file to the root of the project and rename it to cab.ddf and input the following:

;** MyEventHandler.wsp **
.OPTION EXPLICIT
.Set CabinetNameTemplate= MyEventHandler.wsp
.Set DiskDirectoryTemplate=CDROM
.Set CompressionType=MSZIP
.Set UniqueFiles="ON"
.Set Cabinet=on
.Set DiskDirectory1=Package
;**************************************************
manifest.xml manifest.xml
MyEventHandler\elements.xml MyEventHandler \elements.xml
MyEventHandler \feature.xml MyEventHandler \feature.xml
bin\debug\ MyEventHandler.dll MyEventHandler.dll

Then add another text file and rename that one to installer.bat. The following should be inputted:

makecab /f cab.ddf

 

Add a new xml file to the root of the project and rename it to manifest.xml and input the following:

<?xml version="1.0" encoding="utf-8" ?>
<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="BF2A762B-A158-456c-BCA5-38120E87D982">
    <FeatureManifests>
        <FeatureManifest Location="MyEventHandler\feature.xml"/>
    </FeatureManifests>
    <Assemblies>
        <Assembly DeploymentTarget="GlobalAssemblyCache"  Location="MyEventHandler.dll">
        </Assembly>
    </Assemblies>
</Solution>

Save all your work and build your project.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5