views:

742

answers:

2

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.

+2  A: 

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"&gt;
        <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms"&gt;
          <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>
naivists
Outputting HTML code doesn't work anyway - the html is encoded before display. Which is all good and stops that nonsense.The event handler is attached to the content type used in the list - do you know if the edit form template can be linked to the content type instead of the list definition?
marcus.greasly
Yes, it can be done. Edited my answer to show how it's done
naivists
Thanks, I don't actually have access to the content type definitions, so I need to do this in code (feature receiver? to update content type). That should be possible..?
marcus.greasly
It seems like it is possible via API:`olist.ContentTypes[0].EditFormTemplateName = "SomethingElse";` or even `olist.ContentTypes[0].EditFormUrl = "other";` but I haven't tested it, don't know if it works.
naivists
A: 

Hello,

You can try to redirect using CopyUtil : http://weblogs.asp.net/jan/archive/2008/02/26/copyutil-aspx-a-little-sharepoint-gem.aspx

link = "http://yoursite/_layouts/CopyUtil.aspx?Use=id&amp;Action=dispform&amp;ItemId=X&amp;ListId=X&amp;WebId=X&amp;SiteId=X";

Page.Response.Redirect(link)

maybe this will work

Ciprian Grosu