Downloading Sitefinity Documents Programmatically

December 13, 2013 Digital Experience
The samples provided in this blog post show how to download different types of Sitefinity documents programmatically.

In the code we get a specific document by title, download its stream and then save the stream to a file in a specified directory of the project.

Sample 1: Downloading a pdf file:

protected void Page_Load(object sender, EventArgs e)
        {
            DownloadDocument("pdf");
        }
  
        public void SaveStreamToFile(string fileFullPath, Stream stream)
        {
            if (stream.Length == 0) return;
  
            // Create a FileStream object to write a stream to a file
            using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
            {
                // Fill the bytes[] array with the stream data
                byte[] bytesInStream = new byte[stream.Length];
                stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
  
                // Use FileStream object to write to the specified file
                fileStream.Write(bytesInStream, 0, bytesInStream.Length);
            }
        }
  
  
        public void DownloadDocument(string masterDocumentTitle)
        {
            LibrariesManager librariesManager = LibrariesManager.GetManager();
            librariesManager.Provider.SuppressSecurityChecks = true;
            Document document = librariesManager.GetDocuments().Where(d => d.Title == "Test").FirstOrDefault();
  
            if (document != null)
            {
                string filepathh = Path.Combine(Server.MapPath("~/MyFiles/"), document.Title + ".pdf");
                Stream stream = librariesManager.Download(document.Id);
  
                SaveStreamToFile(filepathh, stream);
            }
        }

 "Test" is the name of the document which is going to be downloaded and MyFiles is a folder located in the main project folder.

Sample 2: Downloading an Excel file:

protected void Page_Load(object sender, EventArgs e)
        {
            DownloadDocument("excel");
        }
   
        public void SaveStreamToFile(string fileFullPath, Stream stream)
        {
            if (stream.Length == 0) return;
   
            // Create a FileStream object to write a stream to a file
            using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
            {
                // Fill the bytes[] array with the stream data
                byte[] bytesInStream = new byte[stream.Length];
                stream.Read(bytesInStream, 0, (int)bytesInStream.Length);
   
                // Use FileStream object to write to the specified file
                fileStream.Write(bytesInStream, 0, bytesInStream.Length);
            }
        }
   
   
        public void DownloadDocument(string masterDocumentTitle)
        {
            LibrariesManager librariesManager = LibrariesManager.GetManager();
            librariesManager.Provider.SuppressSecurityChecks = true;
            Document document = librariesManager.GetDocuments().Where(d => d.Title == "TestExcel").FirstOrDefault();
   
            if (document != null)
            {
                string filepathh = Path.Combine(Server.MapPath("~/MyFiles/"), document.Title + ".xlsx");
                Stream stream = librariesManager.Download(document.Id);
   
                SaveStreamToFile(filepathh, stream);
            }
        }

"TestExcel" is the name of the document which is going to be downloaded and MyFiles is a folder located in the main project folder.

Stefani Tacheva