/*It is very common requirement that you need to upload files from a folder to Sharepoint. The OOB Copy web service can be used for the same,The below code can be used to create a console application that can be used to schedule transfers*/
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Configuration;namespace
{ToSharePointclass Program{
{static void Main(string[] args)try{
CopyLIST.
myCopyService.Url =
string passWord = ConfigurationManager.AppSettings["Password"].ToString();
System.Net.NetworkCredential nc2 = new System.Net.NetworkCredential(userID, passWord);*/Copy myCopyService = new CopyLIST.Copy();ConfigurationManager.AppSettings["SitePath"].ToString() + "_vti_bin/Copy.asmx";/*string userID = ConfigurationManager.AppSettings["UserID"].ToString();myCopyService.AllowAutoRedirect =
myCopyService.PreAuthenticate =
myCopyService.Credentials = System.Net.
CopyLIST.
CopyLIST.
{
{
lcopyDest[0] = copyDest +
{true;true;CredentialCache.DefaultCredentials;string copySource = ConfigurationManager.AppSettings["SourcePath"].ToString();string copyDest = ConfigurationManager.AppSettings["DestinationPath"].ToString();FieldInformation myFieldInfo = new CopyLIST.FieldInformation();FieldInformation[] myFieldInfoArray = { myFieldInfo };byte[] myByteArray;if (System.IO.Directory.Exists(copySource))string[] files = System.IO.Directory.GetFiles(copySource);string[] lcopyDest = new string[1];foreach (string s in files)int inv = s.LastIndexOf(@"\");string flname = s.Substring(inv + 1);@"\" + flname;string src = copySource + @"\" + flname;using (FileStream fileStreamSource = new FileStream(src, FileMode.Open, FileAccess.Read))// Read the source file into a byte array.myByteArray =
{
{
}
numBytesRead += n;
numBytesToRead -= n;
}
numBytesToRead = myByteArray.Length;
{
}
}
CopyLIST.
CopyLIST.new byte[fileStreamSource.Length];int numBytesToRead = (int)fileStreamSource.Length;int numBytesRead = 0;while (numBytesToRead > 0)int n = fileStreamSource.Read(myByteArray, numBytesRead, numBytesToRead);if (n == 0)break;if (myByteArray.Length > 0)CopyResult myCopyResult1 = new CopyLIST.CopyResult();CopyResult[] myCopyResultArray = { myCopyResult1 };try{
myFieldInfoArray, myByteArray,
{
{
{
myCopyResultArray[idx].DestinationUrl);
}uint myCopyUint = myCopyService.CopyIntoItems(copySource, lcopyDest,out myCopyResultArray);if (myCopyUint == 0)int idx = 0;foreach (CopyLIST.CopyResult myCopyResult in myCopyResultArray)string opString = (idx + 1).ToString();if (myCopyResultArray[idx].ErrorMessage == null)Console.WriteLine("Copy operation " + opString +"completed.\r\n" + "Destination: " +else{
myCopyResultArray[idx].ErrorMessage +
}
idx++;
}
}
}
{
}
}
}
}
{
}
}
}
}Console.WriteLine("Copy operation " + opString +" failed.\r\n" + "Error: " +"\r\n" +"Code: " + myCopyResultArray[idx].ErrorCode);catch (Exception exc)Console.WriteLine(exc.ToString());Console.Read();catch(Exception ex)Console.WriteLine(ex.ToString());Console.Read();
hi muralidharan,
ReplyDeletei have sitution where i have to upload file to sharepoint 2010 document library in a snadboxed solution.is there anyway i could achieve this as fileupload not supported in sandbox?
Use FileStream
ReplyDeleteuse fold.Files.Add method
I hope this will help.
using (FileStream fileStreamSource = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
byte[] bytes = new byte[fileStreamSource.Length];
filename = Refine(filename);
SPEventManagerWrapper.DisableEventFiring();
SPFile spfile = fold.Files.Add(string.Format("{0}/{1}", fold.Url, filename), fileStreamSource, true);
SPListItem currentItem = spfile.Item;
currentItem.SystemUpdate(false);
currentItem.Update();