Share via


Adding a date of attachment with attachment column

Question

Sunday, December 24, 2017 4:44 AM

As you know that attachment column is available in all lists in sharepoint. I want to add the date of each added attachment. For example in each item we can add more than one attachment, I want the date of each attachment visible under each attachment.How can I do this?

All replies (4)

Sunday, December 24, 2017 5:08 PM

Hi Zahra1234.

Adding one or more attachments to a list item is a bundled operation with item adding/editing and there is no out of the box way to display the date when an attachment was added to a SharePoint list's item.
You could write custom code in the SPItemEventReceiver.ItemAttachmentAdded method to save somewhere else (e.g.: a list of attachments' information) additional information related to each attachment added to the list.

Bye.

Luigi Bruno
MCP, MCTS, MOS, MTA


Thursday, December 28, 2017 7:02 AM

Hi Zahra,

How are things going?

As Luigi Bruno suggested, you can use SPItemEventReceiver.ItemAttachmentAdded method to save the added time of the attachment. Then create a new list for keeping these values and use code to add the date of each attachment under each attachment.

If you think his suggestion is helpful, you could mark it as an answer. It will help others who meet the similar question in this forum.

Best regards,

Allen Bai

Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected]


Thursday, December 28, 2017 10:39 AM

Hi Zahra1234.

Adding one or more attachments to a list item is a bundled operation with item adding/editing and there is no out of the box way to display the date when an attachment was added to a SharePoint list's item.
You could write custom code in the SPItemEventReceiver.ItemAttachmentAdded method to save somewhere else (e.g.: a list of attachments' information) additional information related to each attachment added to the list.

Bye.

Luigi Bruno
MCP, MCTS, MOS, MTA

Can you give a hint code that allow me when I add multiple attachments in one item to put the date of added attachment behind it?

What the code should do is: if any attachment added in any item it should give the date of adding of this added attachment, Even if the item has more than one attachment, it should give the date of any attachment added in the item.

Can you help me?


Wednesday, January 10, 2018 7:49 AM | 1 vote

Hi Zahra, 

Firstly, use SPItemEventReceiver.ItemAttachmentAdded method to get the added time of each attachment. 

Secondly, save these values into database(you need to manually create a table in database).

Thirdly, use code like Web API, Web Service or other method to read values from database.

Finally, use CSS or JavaScript to change the name of each attachment as you want.

I have write some code about first and second steps, which may be helpful to you(remember to change the database name and table name):

public class EventReceiver1 : SPItemEventReceiver
    {
        /// <summary>
        /// An attachment is being added to the item.
        /// </summary>
        public override void ItemAttachmentAdding(SPItemEventProperties properties)
        {
            // get the datetime of adding attachment 
            DateTime dateTime= DateTime.Now;
            SPList list  = properties.List;
            SPListItem item = properties.ListItem;
           HttpFileCollection files =  HttpContext.Current.Request.Files;
           HttpPostedFile file = files[0];
           string fileName  = file.FileName;
            // get URL of attachemnet(the url include filename)
           string fileURL = "SiteURL" + "/" + list.Title + "/Attachments/" + item.ID + "/" + fileName;
            //connect database
           using (SqlConnection conn = new SqlConnection("Data Source=SQL;Initial Catalog=database;Integrated Security=true;"))
           {
               SqlCommand command = conn.CreateCommand();

               //write sql to insert fileurl and datetime into the database
               command.CommandText = "insert into table ('fileURL', 'dateTime')";

              int success= command.ExecuteNonQuery();
              if (success > 0)
              {
                  //success
              }
              else
              {
                  //fail
              }
           }                               
        }
    }
}

Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected]