Ashish Gupta

I want to do custom server side validation in NewForm.aspx for one of my SharePoint list.

I want to validate the SharePoint site Url given by the user in one text box during creation of new list item by calling method of web service. I want to validate that on click of "OK" button of NewForm.aspx list form page.

Any help would be great!!!

Ashish Gupta



Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

ShajiV

Hi Ashish -- One option is to use SharePoint Event handlers. The item added or item adding event handler. Are you not able to call the web service from the event handler code

Regards

Shaji.





Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

MKeeper

Hi, If you are developing within the SharePoint server itself I would HIGHLY recommend NOT using the Web Services. They are designed for remote access to sharepoint (from another web application, or a windows application on a remote machine).

You should be using the SharePoint Object Model API.

Start off with the Microsoft.SharePoint.dll as a reference (it will be in the Global Assembly Cache [C:\Windows\Assembly] .. otherwise you can find it in the ..\12\ISAPI\ folder in your SharePoint file system)

That allows you to access almost anything in SharePoint.

I also agree with the suggestion from ShajiV.

If you are validating, then I reccommend you use an Event Handler (see this blog here which describes how to get going with event handlers).

With Event Handlers you can check column values each time a user tries to add or edit an item. Using the new Synchronous events (which are new in WSS 3.0 / MOSS 2007) you can even cancel events before the change has been saved to the list!

regards

MKeeper






Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

Ashish Gupta

Thanks guys.

I tried with event handler "ItemAdding" but how do I take values of the form page in code. I tried with following code:

string siteName = Convert.ToString(properties.ListItem["Site_x0020_Name_x0020_for_x0020_Url"], System.Threading.Thread.CurrentThread.CurrentCulture);

but when I debugged the code, I saw that "properties.ListItem" is null. In fact I saw all properties of "properties" object, I didn't find any form values in any property.

One more thing, if I cancel the event and set ErrorMessage, I get error page of WSS but then user is not able to do any thing on that browser. On each action user gets "An unexpected error occured". There should be some proper way to cancel the event and after showing the error message user should remain on the same page so that user can try alternate options of a particular column.

Please help!

Many Thanks.

Ashish Gupta





Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

MKeeper

The "ErrorMessage" is standard functionality for the cancelling of an item event I'm afraid, so I don't know of any workaround for that off the top of my head.

For the properties, please see the following MSDN link:

http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver.aspx

Code Snippet

The following example uses the SPItemEventReceiver class to add an item to a specified Announcements list whenever an attachment is added to a list.

using System;
using Microsoft.SharePoint;

namespace Example_Namespace
{
public class Class_Name : SPItemEventReceiver
{
public override void ItemAttachmentAdded(SPItemEventProperties properties)
{
SPSite siteCollectionEvent = new SPSite(properties.SiteId);
SPWeb siteEvent = siteCollectionEvent.OpenWeb(properties.RelativeWebUrl);
SPListItemCollection itemsEvent = siteEvent.Lists[properties.ListTitle].Items;

SPSite siteCollection = new SPSite(http://Top_Site);
SPWeb site = siteCollection.OpenWeb("/Site");
SPList list = site.Lists["Announcements"];
SPListItemCollection items = list.Items;

SPListItem item = items.Add();
item["Title"] = properties.UserDisplayName + " added an attachment to " + itemsEvent[properties.ListItemId].Title + " in list " + properties.ListTitle + " at " + properties.WebUrl;
item.Update();
}
}
}






Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

Ashish Gupta

I got the way to retrieve property values in "ItemAdding" event handler.

Using "AfterProperties" ItemEventDataCollection. By specifying the key name (i.e. column name e.g. properties.AfterProperties["Description"].ToString()), we can retrieve column value.

But cancelling the event and showing error message in a friendly way is still a problem to me.

User will get annoying by closing browsers and opening new browser each time he gets error.

Thanks a lot.

Ashish





Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

MKeeper

You could just tell them to use the "back" button on their browser.

I don't think there is a "redirect" method for event handlers (because they don't have to be activated through the user interface .. event handlers will fire even if the item is updated from a Workflow or code or something).






Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

MKeeper

Another idea ...

Have you tried adding encoded HTML to the error message

In such an instance, could you enclose JavaScript (i.e. to go "back" )






Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

Ashish Gupta

I tried with SPEnode.HtmlEncode(str) and SPEncode.HtmlDecode(str) both, but whatever I am giving as argument, its coming as plain text on error page. Isn't there any way to show error on NewForm.aspx page itself, I don't want to redirect the user to error page.

I tried to do register client side script but there is no "Page" object in the class. Any other way to do this Actually I was thinking to show the error in form of JavaScript alert but couldn't do.

Thanks.

Ashish





Re: SharePoint - Development and Programming Custom server side validation in NewForm.aspx

GregorioRojas

Setting the SPItemEventProperties.Cancel = true and setting the ErrorMessage is not working for me during an ItemAdding event. the page shown is the standard error page with a "generic" error message that indicates that the action could not take place. how do i get my ErrorMessage to display