Saturday, August 25, 2012

SharePoint: Copy document from one document library to another programmatically

In this post I will explained how we can programmatically copy documents from one document library to another.
Firstly create two document library.Then create a Visual studio solution with User Interface as shown below
Add the below code snippet
protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
              SPSite oSite = SPContext.Current.Site;
              SPWeb oWeb = oSite.RootWeb;
                   
              SPFileCollection oFileCol = oWeb.GetFolder(txtSourceLib.Text).Files;

              foreach (SPFile oFile in oFileCol)
              {
                oFile.CopyTo(txtTargetLib.Text + "/" + oFile.Name, true);
                }
lblMsg.Text = "sucessfully copied";
            }
            catch (Exception ex)
            { }
           
        }

2 comments:

  1. Hi, Can you please post code for..

    if the source and target library are in different site collection? then how the write?

    ReplyDelete
  2. Hi, Can you please post code for..

    if the source and target library are in different site collection? then how the write?

    ReplyDelete