Last week I wrote some code for migrating a networkdrive folder content into a SharePoint Library.
You cannot use the Lists.asmx for uploading the documents because this webservice does not provide
the functionality for uploading files.
But after a little bit of research I found quickly that you can use the copy.asmx webservice.
Normally it is used for copying files between two SharePoint Libraries but it also
work for copying files from a networkdrive into a SharePoint Library.
So you can use the code behind for copying a file on a network drive in a Sharepoint Library.
SPCopyService.Copy copy = new SPCopyService.Copy();
copy.Url = sSiteUrl + "/_vti_bin/copy.asmx";
try
{
if (bDefaultLogon)
copy.Credentials = System.Net.CredentialCache.DefaultCredentials;
else
{
copy.PreAuthenticate = true;
copy.Credentials = new System.Net.NetworkCredential(sTechnicalUserID, sTechnicalUserPW, sTechnicalUserDomain);
}
string destinationUrl = sSharePointDestinationFolder + "/" + sfilename;
// destinationUrl = SiteUrl + "/" + SiteLibrary + "/" + OptionalFolder + "/" + filename.Extention
string[] destinationUrls = { destinationUrl };
SPCopyService.FieldInformation info1 = new SPCopyService.FieldInformation();
info1.DisplayName = sfilename;
info1.InternalName = replaceSpecialChars(sfilename);
info1.Type = SPCopyService.FieldType.Text;
info1.Value = replaceSpecialChars(sfilename);
SPCopyService.FieldInformation[] info = { info1 };
SPCopyService.CopyResult copyResult = new SPCopyService.CopyResult();
SPCopyService.CopyResult[] copyResults = { copyResult };
//Reading the document contents in to stream
FileStream strm = new FileStream(sFilePath + @"\" + sfilename, FileMode.Open, 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();
// call the webservice procedure CopyIntoItems
uint result = copy.CopyIntoItems(sfilename, destinationUrls, info, fileContents, out copyResults);
// Check for Errors:
foreach (SPCopyService.CopyResult copyRes in copyResults)
{
string msg = "====================================" +
"\n " + "SharePoint Error:" +
"\n " + " Url: " + copyRes.DestinationUrl +
"\n " + " Error Code: " + copyRes.ErrorCode +
"\n " + " Message: " + copyRes.ErrorMessage +
"\n " + "====================================";
if (copyRes.ErrorCode.ToString().Equals("Success"))
{
// code for success if needed...
}
else
{
// code for a failure if needed...
}
Console.WriteLine(msg);
}
}
catch (Exception e)
{
iFailedUpdatedRecords++;
Console.WriteLine(" Error on upload...");
}
finally {
copy.Dispose();
}
Geen opmerkingen:
Een reactie posten