0215: SP Event Handler
- Asynchronous vs Synchronous Events: The "ing" and the "ed"
- Asynchronous : Occour after event
- Synchronous :” Occour before event
- Microsoft.SharePoint.SPWebEventReceiver : "Site Level"
- Microsoft.SharePoint.SPListEventReceiver : "List Level"
- Microsoft.SharePoint.SPItemEventReceiver : "List Item Level:”
- Bulk operations In SP
- The MSDN documentation mentions that event handlers will not fire when bulk operation is occurring. For example, when a new list is created, the FieldAdding event will not fire.
- Deploy event handler
Example to Add an itemadded event handler
- Create a empty sharepoint project named SetDocFieldsLib and select GAC deployment option(need to install Sharepoint developer Tools 9.0)
- Add SetDocFields.cs to project :
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace SetDocFieldsLib
{
- Deploy to gac by clicking deploy in VS
- Create a console program named InstallEventHandlertext
- Add sharepoint.dll reference
- Add program.cs
- Open reflector to get public token value and modify it in program.cs
- Run the console program
public class SetDocFields : SPItemEventReceiver
{
// Name of hidden item that contains default properties
const string DEFAULT_FILE_TITLE = "[defaults]";
// local varialble to contain data passed in from SharePoint
private SPItemEventProperties _properties;
// Method called AFTER a new item has been added
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
_properties = properties;
// Run under system account
SPSecurity.RunWithElevatedPrivileges(setFields);
}
private void setFields()
{
// Step 1: Get a handle to the item that raised the event
SPListItem item = _properties.ListItem;
// Step 2: Get a handle to folder containing the
// document just uploaded
SPFolder folder = null;
try
{
if (item.File.ParentFolder != null)
{
folder = item.File.ParentFolder;
}
else
{
folder = item.ParentList.RootFolder;
}
}
catch (Exception ex)
{
// No op
}
// Step 3: Assuming a folder was found (which
// should be in all cases, find the associated
// [defaults] document.
if (folder != null)
{
SPFileCollection files = folder.Files;
string client = "";
string matter = "";
// Step 4: Find the document containing defaults, and
// use its "Client" and "Matter" values for
// new document
foreach (SPFile file in files)
{
if (file.Title.ToLower() == DEFAULT_FILE_TITLE)
{
client = file.Item["Client"].ToString();
matter = file.Item["Matter"].ToString();
break;
}
}
item["Client"] = client;
item["Matter"] = matter;
// Step 5: Save changes without updating the
// MODIFIED, MODIFIED BY fields
item.SystemUpdate(false);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace InstallEventHandlertext
{
class Program
{
static void Main(string[] args)
{
try
{
SPSite site = new SPSite("http://dayang-49u5xli7:6666/");//指定网站
SPWeb web = site.OpenWeb();
SPList list = web.Lists["List Docs"];//指定List
// list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, "EventHandlerText, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cf7ff21d37419004", "EventHandlerText.ItemLogEvent");//添加Event Handler
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "SetDocFieldsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0baa325f9c0cba8f", "SetDocFieldsLib.SetDocFields");//添加Event Handler
Console.WriteLine("发布完成");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
Debug eventhandler
- set breakpoint
- attach wswp.exe process
- In SP, execute changes to trigger event .
0 Comments:
Post a Comment
<< Home