I've been struggling a lot to create custom web part that looks similar to "Document library" in share point. I've tried several approaches but finally figured out it.

Here's my piece of code:

// Adding custom document library to existing site
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true; 

web.Lists.Add("MyLibrary", "My Custom Document Library", SPListTemplateType.DocumentLibrary);
// Show this library on quick launch bar
web.Lists["MyLibrary"].OnQuickLaunch = true;

output of this code is empty custom document library. fine. I already have a "Shared Document Library" with a list of documents in it. My requirement is to copy the docs to "MyLibrary" from "Shared Document Library"

My approach is to get items of "Shared Document Library" and just refer to "MyLibrary"

// Retrieve items from "shared document library"
SPDocumentLibrary sharedDocLibrary = (SPDocumentLibrary) web.Lists["Shared Documents"];
SPListItemCollection sharedDocList = sharedDocLibrary.Items;

// Adding each oldItem to cusomItemCollection
foreach (SPListItem oldItem in sharedDocList)
    SPListItemCollection customCollection = web.Lists["MyLibrary"].Items;
    // Add a new Item to the customCollection
    SPListItem newItem = customCollection.Add();
    // assign oldItem to this; this should do the work 
    newItem = oldItem;

but I didn't achieve what I wanted. Please suggest some solution.

  • is it SharePoint foundation(2010) or WSS-3.0 as you added both of tags Mar 20, 2013 at 6:51
  • oh sorry, its wss-3.0 only. Mar 20, 2013 at 6:52
  • Your code is totally wrong. Mar 20, 2013 at 7:17

3 Answers 3


Use SPFile.CopyTo

SPList sourceLib = web.Lists["SourceLibrary"]
SPFileCollection files = sourceLib.RootFolder.Files;

for(int i=0;i<files.Count;i++)
 files[i].CopyTo("DestinationLibrary/" + files[i].Name, true);

In your code you are trying to add a SPListItem collection to a single list item,

SPListItem newItem = customCollection.Add(); //this will never going to work.

There are different approaches to copy one SharePoint list to another like,

Normally we use first two when we only need copy items for just once, however while using code we can do it as many times as we want,

private SPListItem CopyItem(SPListItem sourceItem, string destinationListName) {
        //Copy sourceItem to destinationList
        SPList destinationList = sourceItem.Web.Lists[destinationListName];
        SPListItem targetItem = destinationList.Items.Add();
        foreach (SPField f in sourceItem.Fields) {
            //Copy all except attachments.
            if (!f.ReadOnlyField && f.InternalName != "Attachments"
                && null != sourceItem[f.InternalName])
                targetItem[f.InternalName] = sourceItem[f.InternalName];
        //Copy attachments
        foreach (string fileName in sourceItem.Attachments) {
            SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
            byte[] imageData = file.OpenBinary();
            targetItem.Attachments.Add(fileName, imageData);

        return targetItem;

Source for more information

In SharePoint 2010 we got new features that copy or move documents automatically once configured. e.g. SendTo

  • Thank you so much. I'll try it and let you know the result. I've a question : Will changes to Source document do reflect in destination document also with this approach ? Mar 20, 2013 at 7:17
  • yes if changes are already been made to document in source document. Mar 20, 2013 at 8:09
  • for some reason this code is not working for me. Mar 20, 2013 at 10:34
  • can you debug to provide the error you receiving as without it I don't know what's wrong. Mar 20, 2013 at 11:08
  • It does not display any errors. The problem is i'm not able to debug it. when I attach my project to "w3wp.exe" nothing happens :( though I'm following all the instructions here. I'm going mad seriously Mar 20, 2013 at 12:00

I've approached my problem this way:
1. Instead of SPListTemplateType.DocumentLibrary I've created SPGridView.
2. Iterated through list of documents in Shared Documents library and added each entry to the SPGridView.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.