Archive for September, 2009

SharePoint: Upload file using Web Service

We need to create a utility that runs on client machine and upload file on pre-defined schedule or as per requirement. The details of code to upload file is as under:

We added Copy web service (http://[ServerURL]/_vti_bin/copy.asmx) in our project assigning name “SPCopyService”.

using WSUploadFile.SPCopyService;

using System.IO; 

namespace WSUploadFile

{
class Program

{

static void Main(string[] args)

{

Copy CopyWS = new Copy();

CopyWS.Credentials = new System.Net.NetworkCredential(“login”, “password”, “domain”);

CopyWS.Url = http://ServerURL/prototype/_vti_bin/copy.asmx”;

String filePath = @”\\NetworkFolderPath\ABC.xls”;


String[] destURL = { http://ServerName/SiteName/Shared Documents/ABC.xls”};


FileStream fstrm = new
FileStream(filePath,FileMode.Open,FileAccess.Read);


byte[] contents = new
byte[fstrm.Length];

fstrm.Read(contents,0,Convert.ToInt32(fstrm.Length));

fstrm.Close();


FieldInformation fileFieldInfo = new
FieldInformation();

fileFieldInfo.DisplayName = “ABC”;

fileFieldInfo.Value = “ABC”;

fileFieldInfo.InternalName = “ABC”;

fileFieldInfo.Type = SPCopyService.FieldType.Text;


FieldInformation[] fileFieldInfoArray = { fileFieldInfo };

CopyResult result1 = new CopyResult();

CopyResult result2 = new CopyResult();

CopyResult[] resultArray = { result1, result2 };


uint resultCopy = CopyWS.CopyIntoItems(filePath, destURL, fileFieldInfoArray, contents, out resultArray);
}
}
}

http://msdn.microsoft.com/en-us/library/copy.copy.copyintoitems.aspx

Leave a Comment