LEADTOOLS SharePoint Client (Leadtools.SharePoint.Client assembly)
LEAD Technologies, Inc

UploadFileAsync Method

Example 





The file containing the item data to upload. This value cannot be null (Nothing in Visual Basic) and must contain the location of an existing file on disk.
Full URL to the destination SharePoint site. This could be http://MySite or http://MySiteCollection/MySite. This value cannot be null (Nothing in Visual Basic).
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 Visual Basic).
The optional user-supplied state object that is used to identify the task that raised the Leadtools.SharePoint.Client.SharePointClient.UploadCompleted event.
Uploads an item in a disk file to a SharePoint server folder asynchronously.
Syntax
public void UploadFileAsync( 
   string sourceFileName,
   Uri siteUri,
   string destinationPath,
   object userState
)
'Declaration
 
Public Sub UploadFileAsync( _
   ByVal sourceFileName As String, _
   ByVal siteUri As Uri, _
   ByVal destinationPath As String, _
   ByVal userState As Object _
) 
'Usage
 
Dim instance As SharePointClient
Dim sourceFileName As String
Dim siteUri As Uri
Dim destinationPath As String
Dim userState As Object
 
instance.UploadFileAsync(sourceFileName, siteUri, destinationPath, userState)
public void UploadFileAsync( 
   string sourceFileName,
   Uri siteUri,
   string destinationPath,
   object userState
)
 function Leadtools.SharePoint.Client.SharePointClient.UploadFileAsync( 
   sourceFileName ,
   siteUri ,
   destinationPath ,
   userState 
)
public:
void UploadFileAsync( 
   String^ sourceFileName,
   Uri^ siteUri,
   String^ destinationPath,
   Object^ userState
) 

Parameters

sourceFileName
The file containing the item data to upload. This value cannot be null (Nothing in Visual Basic) and must contain the location of an existing file on disk.
siteUri
Full URL to the destination SharePoint site. This could be http://MySite or http://MySiteCollection/MySite. This value cannot be null (Nothing in Visual Basic).
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 Visual Basic).
userState
The optional user-supplied state object that is used to identify the task that raised the Leadtools.SharePoint.Client.SharePointClient.UploadCompleted event.
Remarks

This method will return control immediately to your program and the Leadtools.SharePoint.Client.SharePointClient.UploadCompleted will occur when the upload operation is completed (successfully or if an error occurred).

This method will upload the content of sourceFileName to SharePoint server. The location and name of the destination item in the server is determined as follows:

    
            DestinationFileFullUrl = siteUri + destinationPath
            

  

For example, assume the SharePoint server contains a folder with the following name:

    
            http://site/Shared Documents/
            

  

And you want the uploaded item with the name File.ext into this folder. You can perform this using this code:

    
            Uri siteUri = new Uri(@"http://site");
            string destinationPath = @"Shared Documents/File.ext";
            sharePointClientObject.UploadFile(sourceFileName, siteUri, destinationPath);
            

  

You can use the .NET System.IO.Path and System.UriBuilder classes to build the path and URI of the items. No that destinationPath is allowed to contain slashes (/) as in HTTP URI's or forward slashes (\) as in Windows paths or a mix of both.

To upload a file synchronously, use UploadFile.

To upload the data in a stream to the server, use UploadStream or UploadStreamAsync.

To down an item from SharePoint, use DownloadFile, DownloadFileAsync, GetDownloadStream or GetDownloadStreamAsync.

Example
 
Private Shared Sub SharePointClientUploadFileAsyncExample()
   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

   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 UploadFileAsyncCompleted
   spClient.UploadFileAsync(sourceFileName, siteUri, destinationPath, wait)

   ' Wait till the operation completes
   Console.WriteLine("Waiting to upload to finish")
   wait.WaitOne()
   wait.Close()

   Console.WriteLine("Upload completed")
End Sub

Private Shared Sub UploadFileAsyncCompleted(ByVal sender As Object, ByVal e As SharePointClientUploadCompletedEventArgs)
   ' Remove our handler
   Dim spClient As SharePointClient = CType(sender, SharePointClient)
   RemoveHandler spClient.UploadCompleted, AddressOf UploadFileAsyncCompleted

   If IsNothing(e.Error) AndAlso Not e.Cancelled Then
      ' All OK, the file is in SharePoint
   Else
      ' Some error occured
      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
private static void SharePointClientUploadFileAsyncExample()
{
   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;

   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>(UploadFileAsyncCompleted);
   spClient.UploadFileAsync(sourceFileName, 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 UploadFileAsyncCompleted(object sender, SharePointClientUploadCompletedEventArgs e)
{
   // Remove our handler
   SharePointClient spClient = sender as SharePointClient;
   spClient.UploadCompleted -= new EventHandler<SharePointClientUploadCompletedEventArgs>(UploadFileAsyncCompleted);

   if (e.Error == null && !e.Cancelled)
   {
      // All OK, the file is in SharePoint
   }
   else
   {
      // Some error occured
      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();
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

SharePointClient Class
SharePointClient Members

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.