UploadStreamAsync Method
Summary
Uploads the data in a stream to a SharePoint server folder asynchronously.
Syntax
Parameters
sourceStream
The stream containing the item data to upload. This value cannot be null (Nothing in VB).
siteUri
Full URL to the destination SharePoint site. This could be http://MySite
or http://MySiteCollection/MySite
. This value cannot be null (Nothing in VB).
destinationPath
Destination path (folder and file name) of the item to be created in the SharePoint server. See the remarks section for more information. This value cannot be null (Nothing in VB).
userState
The optional user-supplied state object that is used to identify the task that raised the UploadCompleted event.
Example
This example will create a stream of an image file and uploads it to SharePoint asynchronously.
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Color
Imports Leadtools.SharePoint.Client
Private Shared Sub SharePointClientUploadStreamAsyncExample()
Dim sourceFileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"
' Replace SHAREPOINT_SITE_URI with a valid URL to a SharePoint site, for example
' http://SiteCollection/MySite
Dim siteUri As New Uri(SHAREPOINT_SITE_URI)
' Replace SHAREPOINT_FOLDER_NAME with a valid folder on the site above, for example
' "Documents" or "Documents\Sub Documents"
Dim folderName As String = SHAREPOINT_FOLDER_NAME
' Get a stream to the file
Using stream As FileStream = File.OpenRead(sourceFileName)
Dim spClient As New SharePointClient()
spClient.OverwriteExistingFiles = True
' Optional: Set the credentials:
spClient.Credentials = New NetworkCredential(USER_NAME, PASSWORD, DOMAIN)
' If this is a console application demo, we might exit the program before the operation completes,
' so use a wait handle to not exit this method till the opreation completes
Dim wait As New AutoResetEvent(False)
' Build the upload document full path (folder + file name)
Dim destinationPath As String = Path.Combine(folderName, Path.GetFileName(sourceFileName))
' Upload the document
AddHandler spClient.UploadCompleted, AddressOf UploadStreamAsyncCompleted
spClient.UploadStreamAsync(stream, siteUri, destinationPath, wait)
' Wait till the operation completes
Console.WriteLine("Waiting to upload to finish")
wait.WaitOne()
wait.Close()
Console.WriteLine("Upload completed")
End Using
End Sub
Private Shared Sub UploadStreamAsyncCompleted(ByVal sender As Object, ByVal e As SharePointClientUploadCompletedEventArgs)
' Remove our handler
Dim spClient As SharePointClient = CType(sender, SharePointClient)
RemoveHandler spClient.UploadCompleted, AddressOf UploadStreamAsyncCompleted
If IsNothing(e.Error) AndAlso Not e.Cancelled Then
' All OK, the file is in SharePoint
Else
' Some error occurred
If Not IsNothing(e.Error) Then
Console.WriteLine(e.Error.Message)
Else
Console.WriteLine("User cancelled")
End If
End If
' Tell whoever is listening that we are done
Dim wait As EventWaitHandle = CType(e.UserState, EventWaitHandle)
wait.Set()
End Sub
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;
using Leadtools.SharePoint.Client;
private static void SharePointClientUploadStreamAsyncExample()
{
string sourceFileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif";
// Replace SHAREPOINT_SITE_URI with a valid URL to a SharePoint site, for example
// http://SiteCollection/MySite
Uri siteUri = new Uri(SHAREPOINT_SITE_URI);
// Replace SHAREPOINT_FOLDER_NAME with a valid folder on the site above, for example
// "Documents" or "Documents\Sub Documents"
string folderName = SHAREPOINT_FOLDER_NAME;
// Get a stream to the file
using (FileStream stream = File.OpenRead(sourceFileName))
{
SharePointClient spClient = new SharePointClient();
spClient.OverwriteExistingFiles = true;
// Optional: Set the credentials:
spClient.Credentials = new NetworkCredential(USER_NAME, PASSWORD, DOMAIN);
// If this is a console application demo, we might exit the program before the operation completes,
// so use a wait handle to not exit this method till the opreation completes
AutoResetEvent wait = new AutoResetEvent(false);
// Build the upload document full path (folder + file name)
string destinationPath = Path.Combine(folderName, Path.GetFileName(sourceFileName));
// Upload the document
spClient.UploadCompleted += new EventHandler<SharePointClientUploadCompletedEventArgs>(UploadStreamAsyncCompleted);
spClient.UploadStreamAsync(stream, siteUri, destinationPath, wait);
// Wait till the operation completes
Console.WriteLine("Waiting to upload to finish");
wait.WaitOne();
wait.Close();
Console.WriteLine("Upload completed");
}
}
private static void UploadStreamAsyncCompleted(object sender, SharePointClientUploadCompletedEventArgs e)
{
// Remove our handler
SharePointClient spClient = sender as SharePointClient;
spClient.UploadCompleted -= new EventHandler<SharePointClientUploadCompletedEventArgs>(UploadStreamAsyncCompleted);
if (e.Error == null && !e.Cancelled)
{
// All OK, the file is in SharePoint
}
else
{
// Some error occurred
if (e.Error != null)
Console.WriteLine(e.Error.Message);
else
Console.WriteLine("User cancelled");
}
// Tell whoever is listening that we are done
EventWaitHandle wait = e.UserState as EventWaitHandle;
wait.Set();
}