How to Copy a SharePoint List with Attachments

Posted: April 4, 2010 in Development, SharePoint

I know it is simple job for a developer but someone may need this one. With this Code you can copy all items with their attachments in a list to another site or web.  This is the code :

    protected void CopyList(SPList _sourceList, SPList _destList)


        foreach (SPListItem item in _sourceList.Items)


            SPListItem newDestItem = _destList.Items.Add();

            foreach (SPField field in _sourceList.Fields)


                if (!field.ReadOnlyField && field.Type != SPFieldType.Attachments)


                    newDestItem[field.InternalName] = item[field.InternalName];




            if (item.Attachments.Count > 0)


                SPFolder sourceItemAttachFolder = item.Web.Folders["Lists"].SubFolders[item.ParentList.Title]


                foreach (SPFile file in sourceItemAttachFolder.Files)


                    byte[] binFile = file.OpenBinary();

                    newDestItem.Attachments.AddNow(file.Name, binFile);





  1. Brijesh says:

    which type of project we should select to run this code? and on which site we should deploy that old or new ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s