0

I want to build an event handle to limit user to upload file to SharePoint list or Library and than shows an error message.

public override void ItemAdding(SPItemEventProperties properties)
{
    try
    {
        properties.Status = SPEventReceiverStatus.CancelWithError;
        properties.ErrorMessage = "you cannot upload any file!";
        properties.Cancel = true;
    }
    catch (Exception ex)
    {
        properties.ErrorMessage = ex.Message;
        properties.Status = SPEventReceiverStatus.CancelWithError;
    }
}

the xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListUrl="Lists/LibraryA">    // or <Receivers ListTemplateId="101"> for global libary
    <Receiver>
        <Name>EventReceiver1ItemAdding</Name>
        <Type>ItemAdding</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>eventhandle.EventReceiver1.EventReceiver1</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>
</Elements>

I choose list item events in what type... box and Document Library in what item... box

result: upload file success :(

  • you want to cancel the upload? – Raghavendra C Nov 21 '16 at 10:48
  • @RaghavendraC yes! but i cannot cancel it – king yau Nov 22 '16 at 1:50
  • I have try to set it global! <Receivers ListTemplateId="101"> but it doesn't work – king yau Nov 22 '16 at 2:23
  • did below answer worked? – Raghavendra C Nov 23 '16 at 6:29
0

Hi place below code in the beggining of the ItemAdding method may be this is causing issue.

base.ItemAdding(properties);

if you place this it might work, modified code may look like this.

public override void ItemAdding(SPItemEventProperties properties)
{
    base.ItemAdding(properties);
    try
    {
        properties.Status = SPEventReceiverStatus.CancelWithError;
        properties.ErrorMessage = "you cannot upload any file!";
        properties.Cancel = true;
    }
    catch (Exception ex)
    {
        properties.ErrorMessage = ex.Message;
        properties.Status = SPEventReceiverStatus.CancelWithError;
    }
}

sience you are canceling with error then page may stop uploading and show sommthing like An application error occurred on the server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.