locked
How to Copy list item attachment to document library in SharePoint 2010 RRS feed

  • Question

  • Hi,

    How to Create a folder ("List Item - Title Name") in Document library and copy list items Attachments to the same folder in SharePoint 2010,thanks in advance.

    Regards,

    Selvan.J


    Selvan J

    Thursday, March 6, 2014 6:44 AM

Answers

  • Hi,

    You should first check whether the folder exists in the library, if the folder not exists, then create it.

    I had modified the code, you can use the following code snippet to achieve it.

    private void EventCopyFileWhenItemCreatedOrUpdated(SPItemEventProperties properties)
    
            {
    
                SPSite site = new SPSite(http://YourSiteName);
    
                SPWeb web = site.OpenWeb();
    
                SPList doclibList=properties.Web.Lists["YourLibName"];
    
    
                bool foundFolder = false;
    
                if (doclibList.Folders.Count>0)
    
                {
    
                    foreach (SPListItem fitem in doclibList.Folders)
    
                    {
    
                        if (fitem.Title.Equals("FolderA"))
    
                        {
    
                            foundFolder = true;
    
                            break;
    
                        }
    
                    }
    
                }
    
                if (foundFolder == false)
    
                {
    
                    SPListItem folder = doclibList.Folders.Add(doclibList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder,"FolderA");
    
                    folder.Update();
    
                }
    
                string fUrl = doclibList.RootFolder.ServerRelativeUrl+"/FolderA";
    
                SPFolder myLibrary = web.GetFolder(fUrl);
    
    
    
                if (properties.ListTitle == "YourListName")
    
                {
    
                    SPListItem sourceItem = properties.ListItem;
    
                    
    
                    properties.Web.AllowUnsafeUpdates = true;
    
    
                    //get the folder with the attachments for the source item
    
                    SPFolder sourceItemAttachmentsFolder = 
    
                             sourceItem.Web.Folders["Lists"].SubFolders[sourceItem.ParentList.Title].SubFolders["Attachments"].SubFolders[sourceItem.ID.ToString()];
    
    
                    //Loop over the attachments, and add them to the target item    
    
                    foreach (SPFile file in sourceItemAttachmentsFolder.Files)
    
                    {
    
                        if (CheckFileNameExist(file.Name , properties) == false)
    
                        {
    
                            byte[] binFile = file.OpenBinary();
    
                            myLibrary.Files.Add(System.IO.Path.GetFileName(file.Url) , binFile);
    
                        }
    
                    }
    
                }
    
            }
    
    
            private bool CheckFileNameExist(string fileNameInFileAttach, SPItemEventProperties properties)
    
            {
    
                bool flag = false;
    
    
                SPList myDocumentLib = properties.Web.Lists["YourLibName"];
    
                SPQuery spQuery = new SPQuery();
    
                SPListItemCollection items = myDocumentLib.GetItems(spQuery);
    
    
                foreach (SPListItem item in items)
    
                {
    
                    if (fileNameInFileAttach == item["Name"].ToString())
    
                    {
    
                        flag = true;
    
                        break;
    
                    }
    
                }
    
    
                return flag;
    
            }

    Thanks & Regards,

    Jason


    Jason Guo
    TechNet Community Support


    • Edited by JasonGuo Tuesday, March 18, 2014 5:58 AM
    • Marked as answer by Lindali Monday, March 24, 2014 4:00 AM
    Tuesday, March 18, 2014 3:33 AM

All replies

  • Hello Selvan,

    Check these links:

    http://social.technet.microsoft.com/Forums/en-US/e87d9242-a490-4b10-b150-f9074b64e54f/copy-attachments-to-document-library?forum=sharepointgeneralprevious

    http://social.msdn.microsoft.com/Forums/en-US/271d0239-36df-4534-82ea-5362bd615332/copy-list-item-attachment-to-document-library-using-event-receiver

    --------------------------------------------------------------

    ***If my post is answer for your query please mark as answer***

    ***If my answer is helpful please vote***

    Thursday, March 6, 2014 7:05 AM
  • Hi,

    According to your post, my understanding is that you wanted to copy list item attachment to a library folder in SharePoint.

    We can use the event receiver to achieve it.

    Create the ItemAdded and ItemUpdated event receiver, create a custom method to copy the attachment to the library, then use the event receiver to call the custom method.

    You can use the following custom method to copy the attachment.

      

      private void EventCopyFileWhenItemCreatedOrUpdated(SPItemEventProperties properties)
    
            {
    
                SPSite site = new SPSite("http://YourSiteName");
    
                SPWeb web = site.OpenWeb();
    
                string url = web.ServerRelativeUrl + "YourLibraryName/YourFolderName";
    
                SPFolder myLibrary = web.GetFolder(url);
    
    
                if (properties.ListTitle == "YourListName")
    
                {
    
                    SPListItem sourceItem = properties.ListItem;
    
                    
    
                    // Set AllowUnsafeUpdates = true to avoid security error
    
                    properties.Web.AllowUnsafeUpdates = true;
    
    
                    //get the folder with the attachments for the source item
    
                    SPFolder sourceItemAttachmentsFolder = 
    
                             sourceItem.Web.Folders["Lists"].SubFolders[sourceItem.ParentList.Title].SubFolders["Attachments"].SubFolders[sourceItem.ID.ToString()];
    
    
                    //Loop over the attachments, and add them to the target item    
    
                    foreach (SPFile file in sourceItemAttachmentsFolder.Files)
    
                    {
    
                        if (CheckFileNameExist(file.Name , properties) == false)
    
                        {
    
                            byte[] binFile = file.OpenBinary();
    
                            myLibrary.Files.Add(System.IO.Path.GetFileName(file.Url) , binFile);
    
                        }
    
                    }
    
                }
    
            }
    
    
            private bool CheckFileNameExist(string fileNameInFileAttach, SPItemEventProperties properties)
    
            {
    
                bool flag = false;
    
    
                SPList myDocumentLib = properties.Web.Lists["YourLibraryName"];
    
                SPQuery spQuery = new SPQuery();
    
                SPListItemCollection items = myDocumentLib.GetItems(spQuery);
    
    
                foreach (SPListItem item in items)
    
                {
    
                    if (fileNameInFileAttach == item["Name"].ToString())
    
                    {
    
                        flag = true;
    
                        break;
    
                    }
    
                }
    
    
                return flag;
    
            }

    For more information: http://www.microsofttechnology.net/2012/03/how-to-copy-file-attachment-from-custom.html

    Thanks & Regards,

    Jason

    Jason Guo
    TechNet Community Support


    Friday, March 7, 2014 10:44 AM
  • Hi Jason,

    Thanks for your reply,

    Now I can able to copy list item attachment files to the library, but unable to create folder based on (list item-Title name) in the document library.

    Kindly help on this,

    Thanks in advance.

    Best Regards,

    Selvan.J

     


    Selvan J



    • Edited by Selvan J Monday, March 10, 2014 8:45 AM
    Monday, March 10, 2014 7:52 AM
  • Hi,

    You should first check whether the folder exists in the library, if the folder not exists, then create it.

    I had modified the code, you can use the following code snippet to achieve it.

    private void EventCopyFileWhenItemCreatedOrUpdated(SPItemEventProperties properties)
    
            {
    
                SPSite site = new SPSite(http://YourSiteName);
    
                SPWeb web = site.OpenWeb();
    
                SPList doclibList=properties.Web.Lists["YourLibName"];
    
    
                bool foundFolder = false;
    
                if (doclibList.Folders.Count>0)
    
                {
    
                    foreach (SPListItem fitem in doclibList.Folders)
    
                    {
    
                        if (fitem.Title.Equals("FolderA"))
    
                        {
    
                            foundFolder = true;
    
                            break;
    
                        }
    
                    }
    
                }
    
                if (foundFolder == false)
    
                {
    
                    SPListItem folder = doclibList.Folders.Add(doclibList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder,"FolderA");
    
                    folder.Update();
    
                }
    
                string fUrl = doclibList.RootFolder.ServerRelativeUrl+"/FolderA";
    
                SPFolder myLibrary = web.GetFolder(fUrl);
    
    
    
                if (properties.ListTitle == "YourListName")
    
                {
    
                    SPListItem sourceItem = properties.ListItem;
    
                    
    
                    properties.Web.AllowUnsafeUpdates = true;
    
    
                    //get the folder with the attachments for the source item
    
                    SPFolder sourceItemAttachmentsFolder = 
    
                             sourceItem.Web.Folders["Lists"].SubFolders[sourceItem.ParentList.Title].SubFolders["Attachments"].SubFolders[sourceItem.ID.ToString()];
    
    
                    //Loop over the attachments, and add them to the target item    
    
                    foreach (SPFile file in sourceItemAttachmentsFolder.Files)
    
                    {
    
                        if (CheckFileNameExist(file.Name , properties) == false)
    
                        {
    
                            byte[] binFile = file.OpenBinary();
    
                            myLibrary.Files.Add(System.IO.Path.GetFileName(file.Url) , binFile);
    
                        }
    
                    }
    
                }
    
            }
    
    
            private bool CheckFileNameExist(string fileNameInFileAttach, SPItemEventProperties properties)
    
            {
    
                bool flag = false;
    
    
                SPList myDocumentLib = properties.Web.Lists["YourLibName"];
    
                SPQuery spQuery = new SPQuery();
    
                SPListItemCollection items = myDocumentLib.GetItems(spQuery);
    
    
                foreach (SPListItem item in items)
    
                {
    
                    if (fileNameInFileAttach == item["Name"].ToString())
    
                    {
    
                        flag = true;
    
                        break;
    
                    }
    
                }
    
    
                return flag;
    
            }

    Thanks & Regards,

    Jason


    Jason Guo
    TechNet Community Support


    • Edited by JasonGuo Tuesday, March 18, 2014 5:58 AM
    • Marked as answer by Lindali Monday, March 24, 2014 4:00 AM
    Tuesday, March 18, 2014 3:33 AM
  • Hi Janson,

    Thanks for your replay, now I can able to create static folder called “Folder A” and move the list item attachment to that particular folder. Kindly help me on this I want to create dynamic folder based on title name and copy list item attachment to that particular folder.  Thanks in advance.   


    Selvan J

    Tuesday, April 8, 2014 7:21 AM
  • Are you able to fix this issue? if so, can you please share your solution?
    Wednesday, August 27, 2014 6:19 PM
  • How can i create a folder in document library based on Item title and copy list item attachments to that particular folder? Please help
    • Edited by Pasa_Light Wednesday, August 27, 2014 7:01 PM
    Wednesday, August 27, 2014 6:45 PM