Pages

Tuesday, March 6, 2012

Move/Migrate SharePoint List Attachments to Document Library with Created and Modified Date

Hello

We have faced one problem about SharePoint Search . We have SharePoint list in which we have items and it has attachments, but SharePoint Search could not look inside Documents which are stored as Attachments in List. So to make it work we have to move all attachments inside the document library so SharePoint Search can crawl and return the results :).

So to provide Keyword Search we need to Move the SharePoint Attachments to Document Library so that we can search in SharePoint Search.

Here is code which will move SharePoint List Attachments to Document Library.

SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               using (SPSite site = new SPSite("SiteURL"))
               {
                   SPList docDestination = site.RootWeb.Lists["DocName"];
                   SPFolder fldRoot = site.RootWeb.Folders[docDestination.Title];
                   SPFileCollection flColl = null;
                   SPList lstAttachment = site.RootWeb.Lists["ListName"];
                   foreach (SPListItem lstItem in lstAttachment.Items)
                   {
                       if (lstItem.Attachments != null && lstItem.Attachments.Count > 0)
                       {
                           foreach (String strName in lstItem.Attachments)
                           {
                               flColl = fldRoot.Files;
                               SPListItem listtem = docDestination.Items.Add();
                               SPFile FileCopy = lstItem.ParentList.ParentWeb.GetFile(lstItem.Attachments.UrlPrefix + strName);

                               string destFile = flColl.Folder.Url + "/" + FileCopy.Name;
                               byte[] fileData = FileCopy.OpenBinary();

                               SPFile flAdded = flColl.Add(destFile, fileData, site.RootWeb.CurrentUser, site.RootWeb.CurrentUser, Convert.ToDateTime(lstItem[SPBuiltInFieldId.Created]), Convert.ToDateTime(lstItem[SPBuiltInFieldId.Modified]));
                               SPListItem item = flAdded.Item;
                               item[SPBuiltInFieldId.Created] = Convert.ToDateTime(lstItem[SPBuiltInFieldId.Created]);
                               item[SPBuiltInFieldId.Modified] = Convert.ToDateTime(lstItem[SPBuiltInFieldId.Modified]);
                               flAdded.Item.Update();
                           }

                       }
                   }
               }
           });

Disha Shah

10 comments:

  1. I am new to sharepoint where do would u insert this code?

    ReplyDelete
    Replies
    1. Hi Mel

      As SharePoint List Attachment to Document Library is one time process , you could write this code in Console application or form Application.

      Thanks
      Disha Shah

      Delete
  2. This code could be implemented in a lists event receiver on item add.

    ReplyDelete
  3. Hi Disha,

    This is very nice, is there any chance you could post the full vb file from a visual studio project.. sorry very new and would like to see how to do this is Visual Studio Event Receiver :)

    thank you..

    ReplyDelete
    Replies
    1. Hi Matty

      I have added this code on a button click of a Form Applciation in Visual Studio which contains code behind as C#! You could do same.

      You could convert the same code to vb as there are so many code translators are avilable!

      Hope this helps!
      Thanks
      Disha Shah

      Delete
  4. this code is use for to copy documents from one document library to another document library will it was working on form application But it is not working on "Event Receiver" Will u say how to do

    using (SPSite site = new SPSite("http://sp2010:8080/personal/sss/"))
    {
    using (SPWeb web = site.RootWeb)
    {
    SPFileCollection collFile = web.GetFolder("aaa").Files;
    foreach (SPFile file in collFile)
    {

    file.CopyTo("ccc/" + file.Name, true);
    }
    }
    }

    ReplyDelete
    Replies
    1. Hi Anil

      Yes this code is going to work in form application - this code is speically for move bulk of documents. For event receiver we need to change some of lines of code. My First Question is why you would like to write this kind of code in event receiver.

      You have to refer web, site and folder objects with the current listitem or document library item like properties.ListItem
      For example
      SPFileCollection collFile = properties.ListItem.Web.GetFolder("aaa").Files;

      Let me know if you have any question

      Thank You
      Disha Shah

      Delete
  5. Hi Disha - We have multiple attachments attached t0 SharePoint List Item. And now the requirement is I need to move all the Attachments to SharePoint list and make them as list Items. How can I move Attachments to the same List and make them as list Items.

    Thanks
    Satish

    ReplyDelete
  6. Thank you Disha, This has helped us implement the functionality we need. We are seeing following error on some files, when I am trying to add them to library.
    Form submission failed. (User: xxxxx, Form Name: Template, IP: , Request: http://Site Url/Lists/List name/Task/editifs.aspx?List=e6647e38-cb01-46b3-8bee-308357045532&ID=116&Source=http://Site URL/Lists/List Name/AllItems.aspx?View={CB9F3C2B-49DF-4D75-9720-1E7E000FD229}&FilterField1=Status&FilterValue1=Completed&IsDlg=1&Web=706d51c9-e509-40f3-884a-7d40e126ff14, Form ID: urn:schemas-microsoft-com:office:infopath:list:-AutoGen-2013-12-06T16:11:54:747Z, Type: DataAdapterException, Exception Message: The remote server returned an error: (500) Internal Server Error. Cannot access a closed file. Cannot access a closed file.
    at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
    The remote server returned an error: (500) Internal Server Error.)

    Any ideas on what would be causing it.
    Thanks

    ReplyDelete
  7. I need help on copying the attachments of current list item to document library. And attachments should be copied in new folder in document library and that folder should be named on the user who created the item.

    ReplyDelete