How to override the default item save event using custom fields
2011-09-08 15:19
585 查看
In my former post I wrote about the approach
to use custom field types as an alternative way for list item event handling in SharePoint. In the current post I would like to show you how to inject code into the edit item or new item forms using a custom field that intercepts the default item save event.
You can put the event handling in a custom field that has some other role on the user interface, or it can be a custom field without its own UI that serves only the special role of event handling. I will show you an example for the latter one in a later
post.
You might ask why is it useful to be able to do that. Let’s see some examples for the usage.
Do actions before saving the item, like:
· Cross-field or even cross-list validation of the item, for example, the start date of the event must be less than the end date, or the value of the Affected tasks field for an item must be less than the count of the task items in another list.
· Extra permission checks, for example, based on the item fields, like only the creator of the item can modify the status.
· Add validation errors to the affected fields and cancel the save, if the above validations fail.
· Update field values on the fly for the same item or even an item in another list, like call a web service using the login name of the modifier and store the results in one of the fields.
Do actions after saving the item, like:
· Notify back-end systems about the change, for example, call a web service, update / insert a record in an SQL server table or create a new SharePoint list / site based on the field values.
· Redirect the user to another page.
· Set permissions on the item, for example, set write permissions using elevated privileges for the users or groups that were specified in a Person or Group field. This may be useful since it enables users with edit permissions to set item security.
There are two similar ways to intercept the base item save process, both of them is provided by the
BaseFieldControl class, so you should put the code in the field control class of the custom field.
First approach is to override the UpdateFieldValueInItem method:
public override
void UpdateFieldValueInItem()
{
Page.Validate();
if (Page.IsValid)
{
// do actions before save
// do the save if required
base.UpdateFieldValueInItem();
// do actions after save
}
else
{
// do actions instead of save
}
}
The another alternative is to attach your own event handler to the OnSaveHandler property of the
SPFormContext class:
protected override
void OnInit(EventArgs e)
{
base.OnInit(e);
// add save handler only in New and Edit modes
if ((SPContext.Current.FormContext.FormMode ==
SPControlMode.New) || (SPContext.Current.FormContext.FormMode ==
SPControlMode.Edit))
SPContext.Current.FormContext.OnSaveHandler +=
new EventHandler(MyHandler);
}
protected
void MyHandler(object sender,
EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
// do actions before save
// do the save if required
SPContext.Current.ListItem.Update();
// or you can save the item using this line of code either
//SaveButton.SaveItem();
// do actions after save
}
else
{
// do actions instead of save
}
}
If you don’t call the methods that save the item to the SharePoint list your form can be used as a front-end for a back-end system as well, for example, you can call a web service or insert SQL records without affecting the content
of the SharePoint list. But that is probably not the most ergonomic usage of SharePoint and the easiest way to create web based user interface for back-end systems.
to use custom field types as an alternative way for list item event handling in SharePoint. In the current post I would like to show you how to inject code into the edit item or new item forms using a custom field that intercepts the default item save event.
You can put the event handling in a custom field that has some other role on the user interface, or it can be a custom field without its own UI that serves only the special role of event handling. I will show you an example for the latter one in a later
post.
You might ask why is it useful to be able to do that. Let’s see some examples for the usage.
Do actions before saving the item, like:
· Cross-field or even cross-list validation of the item, for example, the start date of the event must be less than the end date, or the value of the Affected tasks field for an item must be less than the count of the task items in another list.
· Extra permission checks, for example, based on the item fields, like only the creator of the item can modify the status.
· Add validation errors to the affected fields and cancel the save, if the above validations fail.
· Update field values on the fly for the same item or even an item in another list, like call a web service using the login name of the modifier and store the results in one of the fields.
Do actions after saving the item, like:
· Notify back-end systems about the change, for example, call a web service, update / insert a record in an SQL server table or create a new SharePoint list / site based on the field values.
· Redirect the user to another page.
· Set permissions on the item, for example, set write permissions using elevated privileges for the users or groups that were specified in a Person or Group field. This may be useful since it enables users with edit permissions to set item security.
There are two similar ways to intercept the base item save process, both of them is provided by the
BaseFieldControl class, so you should put the code in the field control class of the custom field.
First approach is to override the UpdateFieldValueInItem method:
public override
void UpdateFieldValueInItem()
{
Page.Validate();
if (Page.IsValid)
{
// do actions before save
// do the save if required
base.UpdateFieldValueInItem();
// do actions after save
}
else
{
// do actions instead of save
}
}
The another alternative is to attach your own event handler to the OnSaveHandler property of the
SPFormContext class:
protected override
void OnInit(EventArgs e)
{
base.OnInit(e);
// add save handler only in New and Edit modes
if ((SPContext.Current.FormContext.FormMode ==
SPControlMode.New) || (SPContext.Current.FormContext.FormMode ==
SPControlMode.Edit))
SPContext.Current.FormContext.OnSaveHandler +=
new EventHandler(MyHandler);
}
protected
void MyHandler(object sender,
EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
// do actions before save
// do the save if required
SPContext.Current.ListItem.Update();
// or you can save the item using this line of code either
//SaveButton.SaveItem();
// do actions after save
}
else
{
// do actions instead of save
}
}
If you don’t call the methods that save the item to the SharePoint list your form can be used as a front-end for a back-end system as well, for example, you can call a web service or insert SQL records without affecting the content
of the SharePoint list. But that is probably not the most ergonomic usage of SharePoint and the easiest way to create web based user interface for back-end systems.
相关文章推荐
- Adding Custom Actions to the List Item Menu in SharePoint 2010 Using SharePoint Designer 2010[Sharepoint中添加自定义菜单]
- How to Change the Default Boot Device by Using the Boot PROM
- How to add icon for the item of ListCtrl using C++ (Mail Attachment ListCtrl) - 如何用C++为ListCtrl的Item添加图标(邮件附件ListCtrl)
- How to create fully custom Role, User, Event, Resource classes for use with the Security and Scheduler modules
- How to display fields in the frontend item view
- Using the itemDoubleClick event to open nodes in a Flex Tree control
- How to reset the ILOM root password back to the default 'changeme' using ipmitool
- how to get rid of the UITabBarItem padding
- 继续摘抄:How to fix Apache – “Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName”
- Tip/Trick: How to Run a Root “/” Site with the Local Web Server using VS 2005 SP1
- 32-bit Assembler is Easy, why and how to develop using the assembler; start learning to program in Assembly now!
- How to access HBase from spark-shell using YARN as the master on CDH 5.3 and Spark 1.2
- How to access the features in an in-memory output layer using an IFeatureCursor
- How to capture video frames from the camera as images using AV Foundation on iOS
- [转]how to split the ng-repeat data with three columns using bootstrap
- How to automate PowerPoint by using Visual C++ 5.0 or Visual C++ 6.0 with The Microsoft Foundation Classes
- how to add custom objects to the process instance?
- How to Change Default Grub Theme or Splash Image in Ubuntu 10.10
- How To Uninstall Software Using The Ubuntu Command Line
- [转]SP 2010: How To – Event Receivers and Custom Error Pages