0

I have created a Document Library named Attachments Library. In the library I have created an additional mandatory column of File Details. I intened to use this field to capture some metadata about the file being uploaded.

When I click on the "new document" link in Attachments Library it shows me dialog to upload the file. Once the file is uploaded another dialog is shown with field Name Title and File Details. The fields Name and File Details are shown as mandatory (with asterisk symbol). What I want is that the field File Details should be auto-populated with some metadata of the uploaded file. For this purpose I have created an Event Receiver.

This is how my Elements.xml file looks like:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListUrl="Attachments Library">
      <Receiver>
        <Name>EventReceiver1ItemAttachmentAdding</Name>
        <Type>ItemAttachmentAdding</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>AttachmentLibEventReceiver.EventReceiver1.EventReceiver1</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>EventReceiver1ItemAttachmentAdded</Name>
        <Type>ItemAttachmentAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>AttachmentLibEventReceiver.EventReceiver1.EventReceiver1</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>
</Elements>

And this is how I written my event receiver:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace AttachmentLibEventReceiver.EventReceiver1
{
    public class EventReceiver1 : SPItemEventReceiver
    {
        public override void ItemAttachmentAdding(SPItemEventProperties properties)
        {
            base.ItemAttachmentAdding(properties);
        }

        public override void ItemAttachmentAdded(SPItemEventProperties properties)
        {
            SPFile file = properties.ListItem.File;
            //properties.AfterProperties["File_x0020_Details"] = file.Name; // Did not work
            properties.ListItem["File_x0020_Details"] = file.Name; // Also did not work

            base.ItemAttachmentAdded(properties);
        }
    }
}

I tried to put the below code in ItemAttachmentAdding event also but no luck.

SPFile file = properties.ListItem.File;
//properties.AfterProperties["File_x0020_Details"] = file.Name; // Did not work
properties.ListItem["File_x0020_Details"] = file.Name; // Also did not work

How can I get this to work?

1

From your description you need ItemAdding event receiver, not ItemAttachmentAdding event receiver.

  • I tried it, but it didn't work. – Naveen Dec 17 '13 at 11:27
  • Sorry,I was thinking ItemAdded, not ItemAdding. In ItemAdded try this properties.ListItem["File Details"] = properties.ListItem.Name; – Naim Murati Dec 17 '13 at 11:40

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.