Blogger

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

    Recent items

Login

Tuesday, 05 February 2013 20:29

Download and Upload files via SharePoint Client Object Model

Written by
Rate this item
(6 votes)

Hi all, here I want describe two function that use the SharePoint Client Object Model:

  1. to download a file from a SharePoint website to a local path;
  2. to upload a file from a local path to a SharePoint website.

Show/Hidden csharp code

View source
 
 
 
public static bool DownloadFile(ClientContext context, string fileRelativeUrl, string destinationPath, string fileName)
{
    try
    {
        fileRelativeUrl = fileRelativeUrl.Replace("/Images/","/PublishingImages/");
        string sourceUrl = fileRelativeUrl + fileName;
        string completeDestinationPath=destinationPath  + fileName;
        
        if (!System.IO.File.Exists(completeDestinationPath))
        {
            Directory.CreateDirectory(destinationPath);
            FileInformation spFileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, sourceUrl);
 
            using (Stream destination = System.IO.File.Create(completeDestinationPath))
            {
                for (int a = spFileInfo.Stream.ReadByte(); a != -1; a = spFileInfo.Stream.ReadByte())
                    destination.WriteByte((byte)a);
            }
        }
    }
    catch (Exception ex)
    {
        Logger.WriteLog(string.Format("Error during copying file: {0} with error {1}", fileRelativeUrl, ex.StackTrace));
        return false;
    }
    return true;
}
 
 
 
 
 
public static void UploadFile(Web currWeb, string destRelativeUrl, string file, ClientContext destContext)
{
    try
    {
        Microsoft.SharePoint.Client.File destFile = currWeb.GetFileByServerRelativeUrl(destRelativeUrl);
        bool bExists = false;
        try
        {
            destContext.Load(destFile);
            destContext.ExecuteQuery(); //Raises exception if the file doesn't exist
            bExists = destFile.Exists;  //may not be needed - here for good measure
            Logger.WriteLog(string.Format("File already exist: {0}", destRelativeUrl));
        }
        catch
        {
        }
 
        if (!bExists)
        {
            using (FileStream fs = new FileStream(file, FileMode.Open))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(destContext, destRelativeUrl, fs, false);
                Logger.WriteLog(string.Format("File copied: {0}", destRelativeUrl));
            }
        }
    }
    catch (Exception ex)
    {
        Logger.WriteLog(string.Format("Error during upload file: {0} with error {1}", file, ex));
    }
}
 
 
 

Here a wiki to help to use the SharePoint Client Object Model.

Read 30850 times Last modified on Tuesday, 12 January 2016 10:42

1 comment

  • Comment Link Selma Wednesday, 03 May 2017 19:09 posted by Selma

    Sono stato più di due ore online, ma non avevo ancora trovato un articolo così piacevole da leggere.
    Se tutti i siti avessero articoli fatti così bene, internet sarebbe decisamente più interessante da leggere.
    Un caro saluto.

    Report

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.