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();
}
}
No comments:
Post a Comment