Loading...

Saturday, September 12, 2009

validation with standard sharepoint error Page on ListItem adding event

To add validation to listitem adding event( to restrict the no of items to be added per day)in list
1) Visual studio 2008 -->Sharepoint-->Empty Project
2)In solution explorer-->add new item --> content type
3)And check content type with event receiver, you will find three 2 .cs files and 1 xml file
a)itemeventreciver.cs

b)ListItemReciver.cs
in this file uncomment the ItemAdding event

add the following code required for the restriction

public override void ItemAdding(SPItemEventProperties properties)
{

int perday = GetItemCountPerDay();
string dt= properties.AfterProperties[CONST_FIELD_DATETIME].ToString();
SPQuery query = QueryItemForPerDay(dt);

using (SPWeb oWebsite = new SPSite(properties.SiteId).OpenWeb(properties.RelativeWebUrl))
{

SPListItemCollection collItems = oWebsite.Lists[properties.ListTitle].GetItems(query);

if ((collItems != null) && (collItems.Count > perday - 1))
{
properties.ErrorMessage = String.Format("Adding items to this list is not supported because per day only allowed add {0} items.", PER_DAY);
properties.Cancel = true;
}
}

}


c).xml file


4)Deploy the project
To enable the content type
go to the site Site actions -->create new custom list --> enable the content types for the site
list settings-->advanced settings--selct deployed content type

5)Go to wss --> virtual diectories-> site --> open the web.config file and do the following changes

sharepoint

safemode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false"

customerrors mode="On"

No comments: