How to Copy/Upload Files into SharePoint Document Library
We may have to add a service reference to SharePoint Copy Web Service [
http://SharePointSite/_vti_bin/copy.asmx] before coding this.
Here are two functions to copy/upload files into SharePoint Document Library. To call the functions, we may have to use following syntaxes:
CopyFromFileToSP("ASLog.txt", "C:\\ASLog.txt", "http://dcftsd1/Policy/ASLog.txt"); CopyFromSPtoSP("http://dcftsd1/Policy/Book1.xls", "http://dcftsd1/Policy/Book3.xls");
//Function to Copy a File from One Document Library to Another Document Library
private void copyFromSPtoSP(string _SourceFile,string _DestFile)
{
try
{
SPCopyService.FieldInformation myFieldInfo = new
SPCopyService.FieldInformation();
SPCopyService.FieldInformation[] myFieldInfoArray = { myFieldInfo };
SPCopyService.CopyResult myCopyResult1 = new SPCopyService.CopyResult();
SPCopyService.CopyResult myCopyResult2 = new SPCopyService.CopyResult();
SPCopyService.CopyResult[] myCopyResultArray = { myCopyResult1, myCopyResult2 };
string copySource = _SourceFile;
string[] copyDest = { _DestFile };
using (SPCopyService.Copy CopyService = new SPCopyService.Copy())
{
CopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Using Custom Credentials
// CopyService.Credentials = new NetworkCredential(UserName, Password,Domain);
byte[] myByteArray;
uint myGetUint = CopyService.GetItem(copySource, out myFieldInfoArray, out myByteArray);
uint myCopyUint = CopyService.CopyIntoItems(copySource, copyDest, myFieldInfoArray, myByteArray, out myCopyResultArray);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Function to Upload a File to SharePoint Document Library
private void CopyFromFileToSP(string _FileName, string _SourceFile, string _DestFile)
{
try
{
string destinationUrl = Uri.EscapeDataString(_DestFile);
string[] destinationUrls = { destinationUrl };
SPCopyService.FieldInformation myFieldInfo = new
SPCopyService.FieldInformation();
SPCopyService.FieldInformation[] myFieldInfoArray = { myFieldInfo };
SPCopyService.CopyResult cResult1 = new SPCopyService.CopyResult();
SPCopyService.CopyResult cResult2 = new SPCopyService.CopyResult();
SPCopyService.CopyResult[] result = { cResult1, cResult2 };
using (SPCopyService.Copy CopyService = new SPCopyService.Copy())
{
CopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Using Custom Credentials
// CopyService.Credentials = new NetworkCredential(UserName, Password,Domain);
System.IO.FileStream strm = new System.IO.FileStream(_SourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] fileContents = new Byte[strm.Length];
byte[] r = new Byte[strm.Length];
int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
strm.Close();
uint ret = CopyService.CopyIntoItems(_SourceFile, destinationUrls, myFieldInfoArray, fileContents, out result);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
http://SharePointSite/_vti_bin/copy.asmx] before coding this.
Here are two functions to copy/upload files into SharePoint Document Library. To call the functions, we may have to use following syntaxes:
CopyFromFileToSP("ASLog.txt", "C:\\ASLog.txt", "http://dcftsd1/Policy/ASLog.txt"); CopyFromSPtoSP("http://dcftsd1/Policy/Book1.xls", "http://dcftsd1/Policy/Book3.xls");
//Function to Copy a File from One Document Library to Another Document Library
private void copyFromSPtoSP(string _SourceFile,string _DestFile)
{
try
{
SPCopyService.FieldInformation myFieldInfo = new
SPCopyService.FieldInformation();
SPCopyService.FieldInformation[] myFieldInfoArray = { myFieldInfo };
SPCopyService.CopyResult myCopyResult1 = new SPCopyService.CopyResult();
SPCopyService.CopyResult myCopyResult2 = new SPCopyService.CopyResult();
SPCopyService.CopyResult[] myCopyResultArray = { myCopyResult1, myCopyResult2 };
string copySource = _SourceFile;
string[] copyDest = { _DestFile };
using (SPCopyService.Copy CopyService = new SPCopyService.Copy())
{
CopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Using Custom Credentials
// CopyService.Credentials = new NetworkCredential(UserName, Password,Domain);
byte[] myByteArray;
uint myGetUint = CopyService.GetItem(copySource, out myFieldInfoArray, out myByteArray);
uint myCopyUint = CopyService.CopyIntoItems(copySource, copyDest, myFieldInfoArray, myByteArray, out myCopyResultArray);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Function to Upload a File to SharePoint Document Library
private void CopyFromFileToSP(string _FileName, string _SourceFile, string _DestFile)
{
try
{
string destinationUrl = Uri.EscapeDataString(_DestFile);
string[] destinationUrls = { destinationUrl };
SPCopyService.FieldInformation myFieldInfo = new
SPCopyService.FieldInformation();
SPCopyService.FieldInformation[] myFieldInfoArray = { myFieldInfo };
SPCopyService.CopyResult cResult1 = new SPCopyService.CopyResult();
SPCopyService.CopyResult cResult2 = new SPCopyService.CopyResult();
SPCopyService.CopyResult[] result = { cResult1, cResult2 };
using (SPCopyService.Copy CopyService = new SPCopyService.Copy())
{
CopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Using Custom Credentials
// CopyService.Credentials = new NetworkCredential(UserName, Password,Domain);
System.IO.FileStream strm = new System.IO.FileStream(_SourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] fileContents = new Byte[strm.Length];
byte[] r = new Byte[strm.Length];
int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
strm.Close();
uint ret = CopyService.CopyIntoItems(_SourceFile, destinationUrls, myFieldInfoArray, fileContents, out result);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Comments
Post a Comment