• Home
  • About
Subscribe: Posts | Comments | E-mail
  • SharePointTopics on Microsoft SharePoint Technology
  • Silverlight, WPFSilverlight, Windows Presentation Foundation
  • MiscellaneousRandom bits and rants

Rouslan Grabar on NET, SharePoint, SilverLight, WPF and other technology related topics

Posts Tagged ‘SPItemEventReceiver’


Posted on October 19, 2009 - by Rouslan Grabar

How to allow only Folder items to be created in the List root folder

Sometimes you need to prohibit creation of list items in the root folder of the list. For example,  you invented some pretty looking,  folder-based, issue cataloguing system, but end users create issues in list’s root folder no matter how hard you beg them not to.

Below you will find the code that looks into HttpContext for a “RootFolder” and “Type” parameters and makes use of SPItemEventProperties.Cancel property.

public class IssueItemEventReceiver : SPItemEventReceiver
{
	private HttpContext _current;

	public IssueItemEventReceiver()
	{
		_current = HttpContext.Current;
	}
	public override void ItemAdding(SPItemEventProperties properties)
	{
		using (SPSite site = new SPSite(properties.SiteId))
		{
			using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
			{
				SPList list = web.Lists["Issues"];
				string rootRelUrl = string.Format("{0}/{1}",
						properties.RelativeWebUrl,
						list.RootFolder);

				bool isFolder = (_current.Request.Params["Type"] != null
										&& _current.Request.Params["Type"] == "1");
				bool isInRoot = (_current.Request.Params["RootFolder"] == rootRelUrl);
				properties.Cancel = !(isFolder && isInRoot);
				//// here you can redirect to your very own page in _layouts folder
				//if (properties.Cancel)
				//{
				//	SPUtility.Redirect("myFeatureFolderInLayoutsFolder/MyCustomErrorPage.aspx",
				//				SPRedirectFlags.RelativeToLayoutsPage | SPRedirectFlags.Trusted,
				//				_current);
				//}
			}
		}
	}
}


  • Recent Posts

    • Доклад по PerformancePoint Server 2007 на сайте TechDays.ru
    • How to allow only Folder items to be created in the List root folder
    • Move list item into a subfolder in the same list
    • Краткий обзор возможностей SharePoint 2010 для конечных пользователей
    • Get Free Typemock licenses – ASP.NET bundle launch
  • Tags

    Для новичков Customization Developer Utility Fileds For Beginners IIS mac os x Misc MOSS MSDN photosynth Powershell Russian Screencast SharePoint SharePoint Designer Silverlight, WPF Snippet SPItemEventReceiver techdays timer job Toolbox tricks troubleshooting Virtualisation VSeWSS wcf web service Windows windows authentication WPF WSS
  • Blogroll

    • My personal journal in Russian.
    • Picture Downloader Software
    • Russian SharePoint Community
  • Archives

    • November 2009
    • October 2009
    • July 2009
    • May 2009
    • April 2009
    • March 2009
© 2009 Rouslan Grabar. All Rights Reserved.