Sunday, November 23, 2008

How to upload document from one document library into another document library

public void UploadChart()
{
try
{
String GenerateNo=”ABC_0001”;
String Vertical= Request.QueryString["Vertical"];
sourceWeb = mySite.OpenWeb();
destinationWeb = mySite.AllWebs[Vertical];
destinationWeb.AllowUnsafeUpdates = true;
destinationWeb.Update();
sourceDocuments = sourceWeb.Lists["List Template Gallery"];
destinationDocuments = destinationWeb.Lists["Chart"];
currentSourceDocument = sourceDocuments.Items.GetItemById(25);
fileBytes = currentSourceDocument.File.OpenBinary();
fileNameandExtn = currentSourceDocument.File.Name.Split(".".ToCharArray());
NameofDocument = fileNameandExtn[0] + "_" + GenerateNo + "." + fileNameandExtn[1];
ht1 = new Hashtable();
ht1.Add("ContractNumber", GenerateNo);
//GenerateNo is the method to generate number
relativeDestinationUrl = destinationDocuments.RootFolder.Url + "/" + NameofDocument;
destinationFile = ((SPDocumentLibrary)destinationDocuments).RootFolder.Files.Add(relativeDestinationUrl,fileBytes, ht1,OverwriteDestinationFile);
destinationFile.CheckIn("New Contract", SPCheckinType.MajorCheckIn);
}
catch (Exception ex)
{
//System.Diagnostics.EventLog.WriteEntry("Contract Details Exception", ex.Message);
}
finally
{
mySite.Dispose();
}
}

No comments: