Question

I have an event receiver for a content type to validate some data in the ItemUpdating event. If I cancel the event (some of the data isn't valid for example), I set the properties cancel to true:

properties.Cancel = true;
properties.ErrorMessage = "...";

SharePoint cancels the updating event ok, but shows the standard SharePoint error page (with the specified message). Only problem is, I've got a complaint that this isn't actually very useful - we should return to the EditForm page so the details can be updated.

Has anyone done this, is there an easy way? The only suggestion I've had is that I can implement my own error page, but that's sounding quite a heavy solution to a (theoretically) simple process.

Was it helpful?

Solution

You could try to output HTML code (which includes javascript as well) in the ErrorMessage. BUT even if you do, the problem is that you have no safe way back to the data the user has entered. Either you make a HTTP/301 redirect and then it's a new page load, or you make the client go history.back() with JavaScript and then the browser may reload the page.

The official way of doing this is that you create a list definition and customize the list template. Then you edit the edit form template and include as many ASP.Net validator controls as needed. Then, implement the server side logic as you need. This article explains the technique: http://msdn.microsoft.com/en-us/library/aa543922.aspx

EDIT: To attach a custom control for editing of a specific contenttype, you add an XmlDocuments section to your ContentType definition. For instance, like this

<ContentType
    ..........

    <XmlDocuments>
      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
        <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
          <Display>ContentTypeName_DispForm</Display>
          <Edit>ContentTypeName_EditForm</Edit>
          <New>ContentTypeName_NewForm</New>
        </FormTemplates>
      </XmlDocument>
    </XmlDocuments>
  .......

Then you create your own yoursolution_controltemplates.ascx file, which contains as well such blocks:"

<SharePoint:RenderingTemplate ID="ContentTypeName_DispForm" runat="server"> 
<Template>
      <!-- put whatever controls you need here, we typically create a 
           separate custom control which implements everything-->
</Template>
</SharePoint:RenderingTemplate>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top