locked
how to add default attachement when item added in sharepoint list? RRS feed

  • Question

  • I have list with four column.I need when I add list item in sharepoint list automatically attach default document .
    Monday, October 10, 2011 10:11 AM

Answers

  • You can create EventReceiver in Visual Studio, for ItemAdded event.
    public class MyEventReceiver : SPItemEventReceiver
      {
        HttpContext currentContext;
        public MyEventReceiver()
        {
          currentContext = HttpContext.Current;
        }  
    
        public override void ItemAdding(SPItemEventProperties properties)
        {    
     if (currentContext != null)
        {
         if (currentContext.Request.Files.Count > 0)
         {
    
    for (int i = 0; i < currentContext.Request.Files.Count; i++)
                        {
                          if (!string.IsNullOrEmpty(currentContext.Request.Files[i].FileName))
                          {
                            FileStream file = null;
                            string path = currentContext.Request.Files[i].FileName.ToString();
    
                            // Read the file. This appears to be the offending line
                            file = File.OpenRead(path);
                            byte[] Content = new byte[file.Length];
                            file.Read(Content, 0, (int)file.Length);
                            file.Close();
                            file.Dispose();
                            itemToAdd.Attachments.Add(currentContext.Request.Files[i].FileName, Content);
    
                          }
                        }
         }
        }  
        }
       }
    Monday, October 10, 2011 10:22 AM
  • OK,

    Best way would be to create a workflow that adds this document to the list item. You can then store the document within a SharePoint doc library. If and when you need to update/change this file you can with realtive ease.

    You can create an event reciever but that just adds custom code for no real purpose and will cause issues with backups/restores and future upgrades.


    Regards | Aaron www.aaron-rendell.co.uk | SharePoint Consultant | Microsoft Gold Partner - Silversands, Poole, Dorset, England
    • Proposed as answer by Babasaheb Monday, October 10, 2011 12:22 PM
    • Marked as answer by Wayne Fan Thursday, October 20, 2011 11:53 AM
    Monday, October 10, 2011 10:26 AM

All replies

  • Hi,

    Is the document always going to be changing, or will it be consistant?


    Regards | Aaron www.aaron-rendell.co.uk | SharePoint Consultant | Microsoft Gold Partner - Silversands, Poole, Dorset, England
    Monday, October 10, 2011 10:20 AM
  • consistant
    Monday, October 10, 2011 10:21 AM
  • You can create EventReceiver in Visual Studio, for ItemAdded event.
    public class MyEventReceiver : SPItemEventReceiver
      {
        HttpContext currentContext;
        public MyEventReceiver()
        {
          currentContext = HttpContext.Current;
        }  
    
        public override void ItemAdding(SPItemEventProperties properties)
        {    
     if (currentContext != null)
        {
         if (currentContext.Request.Files.Count > 0)
         {
    
    for (int i = 0; i < currentContext.Request.Files.Count; i++)
                        {
                          if (!string.IsNullOrEmpty(currentContext.Request.Files[i].FileName))
                          {
                            FileStream file = null;
                            string path = currentContext.Request.Files[i].FileName.ToString();
    
                            // Read the file. This appears to be the offending line
                            file = File.OpenRead(path);
                            byte[] Content = new byte[file.Length];
                            file.Read(Content, 0, (int)file.Length);
                            file.Close();
                            file.Dispose();
                            itemToAdd.Attachments.Add(currentContext.Request.Files[i].FileName, Content);
    
                          }
                        }
         }
        }  
        }
       }
    Monday, October 10, 2011 10:22 AM
  • OK,

    Best way would be to create a workflow that adds this document to the list item. You can then store the document within a SharePoint doc library. If and when you need to update/change this file you can with realtive ease.

    You can create an event reciever but that just adds custom code for no real purpose and will cause issues with backups/restores and future upgrades.


    Regards | Aaron www.aaron-rendell.co.uk | SharePoint Consultant | Microsoft Gold Partner - Silversands, Poole, Dorset, England
    • Proposed as answer by Babasaheb Monday, October 10, 2011 12:22 PM
    • Marked as answer by Wayne Fan Thursday, October 20, 2011 11:53 AM
    Monday, October 10, 2011 10:26 AM