sourceUri
The full URI for the source item in the SharePoint server. This value cannot be null (Nothing in VB). It must be the URI to a valid SharePoint item, for example http://server/Shared Documents/File.ext
.
userState
The optional user-supplied state object that is used to identify the task that raised the DownloadCompleted event.
This method will return control immediately to your program and the DownloadCompleted event will occur when a stream is obtained (whether or not the operation is successfyk).
The DownloadCompleted event will receive, as data, an object of type SharePointClientDownloadData that contains the stream data.
You can use the SharePointClientDownloadData.Stream property of the object to get access to the stream containing the downloaded item data (buffer).
The SharePointClientDownloadData.ETag property will contain the SharePoint ETag that identifies a version of the file. This is obtained directly from SharePoint and is not used by SharePointClient.
In most cases, the stream is an HTTP stream and the Stream.CanSeek value will be set to false. Hence, you should read the data from the stream in a forward direction only and not make any assumption regarding the stream length as shown in the examples.
The SharePointClientDownloadData class implements the System.IDisposable interface. Follow the standard .NET dispose pattern when using the SharePointClientDownloadData class. For more information, refer to the System.IDisposable interface documentation in MSDN.
Note that you should not dispose the object in the SharePointClientDownloadData.Stream property yourself. Instead, the object will be disposed when you call dispose on the owner SharePointClientDownloadData class.
To get the download stream synchronously, use the GetDownloadStream method.
To download an item to a disk file, use the DownloadFile or DownloadFileAsync method.
To upload an item to SharePoint, use the UploadFile, UploadFileAsync, UploadStream or UploadStreamAsync method.
This example will get the stream for a document asynchronously at the specified SharePoint and then get the image information using Leadtools.Codecs.RasterCodecs.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;
using Leadtools.SharePoint.Client;
public void SharePointClientDownloadStreamAsyncExample()
{
// 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;
// Replace SHAREPOINT_DOCUMENT_NAME with a valid document on the folder above, for example
// MyDocument.tif
string documentName = SHAREPOINT_DOCUMENT_NAME;
// Build the full URL to the document are we going to download
UriBuilder builder = new UriBuilder(siteUri);
builder.Path = Path.Combine(builder.Path, folderName);
builder.Path = Path.Combine(builder.Path, documentName);
Uri sourceDocumentUri = builder.Uri;
SharePointClient spClient = new SharePointClient();
// 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);
// Download the SharePoint item to the temporary file asynchronously
spClient.DownloadCompleted += new EventHandler<SharePointClientDownloadCompletedEventArgs>(MyDownloadStreamAsyncCompleted);
spClient.GetDownloadStreamAsync(sourceDocumentUri, wait);
// Wait till the operation completes
wait.WaitOne();
wait.Close();
}
private static void MyDownloadStreamAsyncCompleted(object sender, SharePointClientDownloadCompletedEventArgs e)
{
// Remove our handler
SharePointClient spClient = sender as SharePointClient;
spClient.DownloadCompleted -= new EventHandler<SharePointClientDownloadCompletedEventArgs>(MyDownloadStreamAsyncCompleted);
if (e.Error == null && !e.Cancelled)
{
// All is OK, use RasterCodecs to get its info
using (RasterCodecs codecs = new RasterCodecs())
{
using (CodecsImageInfo imageInfo = codecs.GetInformation(e.DestinationData.Stream, true))
{
// Show the image info
Console.WriteLine("URL: {0}", e.SourceUri);
Console.WriteLine("Pages: {0}", imageInfo.TotalPages);
Console.WriteLine("Size: {0} by {1} pixels", imageInfo.Width, imageInfo.Height);
Console.WriteLine("Resolution: {0} by {1} dpi", imageInfo.XResolution, imageInfo.YResolution);
Console.WriteLine("Bits/Pixel: {0}", imageInfo.BitsPerPixel);
}
}
}
else
{
// Some error occured
if (e.Error != null)
Console.WriteLine(e.Error.Message);
else
Console.WriteLine("User cancelled");
}
// Dispose the SharePoint stream
if (e.DestinationData != null)
{
e.DestinationData.Dispose();
}
// Tell whoever is listening that we are done
EventWaitHandle wait = e.UserState as EventWaitHandle;
wait.Set();
}