Creating Event receivers in SharePoint 2010 is very easy With Visual Studio 2010. Here are the steps:
1. Create a New Visual Studio Project, Choose SharePoint >> 2010 >> Event Receiver >> Give it a Name. Say "EventReceiver1"
2. Make it as a Farm solution, choose the event receiver properties as in the below screen.
3. On clicking "Finish" button, Visual Studio will create the project structure as and Now, in the Elements.xml file, change the ListTemplateID="101" (Which means all document Libraries) to ListURL="RelativePathOfDocLibrary" say: "CV".
Change the <Receivers attribute
So, The complete code would be:using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace ERTest.EventReceiver1
{
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// An item is being added.
/// </summary>
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
if (!properties.AfterUrl.EndsWith("pdf"))
{
properties.ErrorMessage = "You are allowed to upload only jpg Images!";
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;
}
}
}
}
See the the result in action!SharePoint document library restricts file type other than PDF.
1. Create a New Visual Studio Project, Choose SharePoint >> 2010 >> Event Receiver >> Give it a Name. Say "EventReceiver1"
2. Make it as a Farm solution, choose the event receiver properties as in the below screen.
3. On clicking "Finish" button, Visual Studio will create the project structure as and Now, in the Elements.xml file, change the ListTemplateID="101" (Which means all document Libraries) to ListURL="RelativePathOfDocLibrary" say: "CV".
Change the <Receivers attribute
So, The complete code would be:using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace ERTest.EventReceiver1
{
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// An item is being added.
/// </summary>
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
if (!properties.AfterUrl.EndsWith("pdf"))
{
properties.ErrorMessage = "You are allowed to upload only jpg Images!";
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;
}
}
}
}





No comments:
Post a Comment