Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
    newItem.Update();
}

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

share|improve this question
    
is it SharePoint foundation(2010) or WSS-3.0 as you added both of tags –  Muhammad Raja Mar 20 '13 at 6:51
    
oh sorry, its wss-3.0 only. –  Laxman Mar 20 '13 at 6:52
    
Your code is totally wrong. –  Muhammad Raja Mar 20 '13 at 7:17
add comment

3 Answers

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);
}
share|improve this answer
add comment

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

share|improve this answer
    
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 ? –  Laxman Mar 20 '13 at 7:17
    
yes if changes are already been made to document in source document. –  Muhammad Raja Mar 20 '13 at 8:09
    
for some reason this code is not working for me. –  Laxman Mar 20 '13 at 10:34
    
can you debug to provide the error you receiving as without it I don't know what's wrong. –  Muhammad Raja Mar 20 '13 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 –  Laxman Mar 20 '13 at 12:00
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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