Saturday, August 25, 2012

SharePoint: Copy document from one document library to file system programmatically

In this post I will explained how we can programmatically copy documents from one document library to file system.
Create a User interface as shown below and add the code snippet
protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SPSite oSite = SPContext.Current.Site;
                SPWeb oWeb = oSite.RootWeb;
                   
                CopyDocToFileSystem(oWeb, txtSourceLib.Text, txtLocation.Text);
                lblMsg.Text = " sucessfully copied";
                     
            }
            catch (Exception ex)
            { }
           
        }


        public void CopyDocToFileSystem(SPWeb oWeb, string sourceDocLib, string destFilePath)
        {
            SPFolder oSourceDocLibraryFolder = oWeb.GetFolder(sourceDocLib);

            SPFileCollection oFileCol = oSourceDocLibraryFolder.Files;
            foreach (SPFile oFile in oFileCol)
            {
                byte[] b = oFile.OpenBinary();
                FileStream oFileStream = new FileStream(destFilePath + "\\" + oFile.Name, FileMode.Create, FileAccess.ReadWrite);
                BinaryWriter oBinaryWriter = new BinaryWriter(oFileStream);
                oBinaryWriter.Write(b);
                oBinaryWriter.Close();
            }
        }



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)
            { }
           
        }

Wednesday, August 15, 2012

Add webpart using Toolpaneview

In this post I will explain how we can add web part from toolpaneview.
Create a webpart page and append ?ToolPaneView=2 to the page URL. The result page is as shown in below screenshot