Downloading Sitefinity Documents Programmatically

Downloading Sitefinity Documents Programmatically

December 13, 2013 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

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

View all posts from Stefani Tacheva on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

More From Progress
progress-next-logo
ProgressNEXT: Premier Event for Modern Application Development
Read More
 
sitefinity-11-1-webinar
Seven Reasons to Check Out Sitefinity 11.1
Read More
 
omnichannel
Getting Started with Your Omnichannel Content Strategy
Read More