none
office 365 API with Visual studio 2013 using file REST API RRS feed

  • Question

  • Hi,

    I have an created web application to access files (only .doc files) from VS 2013 using office 365 API, so I am able to see the all files of specific location of one drive by using below article

    http://msdn.microsoft.com/en-us/library/office/dn605900.aspx#File2

    but I trying to download the file by use of these below request

    GET ../_api/files(<file_path>)/download

    I am unable to download the file content...

    can anyone help me the other article for download the files or any example code in C#...plz share with me...

    • Moved by Cindy Meister MVP Tuesday, June 10, 2014 3:12 PM not using VSTO technology
    Tuesday, June 10, 2014 7:31 AM

Answers

  • Hi,

    You can try the code below to download file from your site using Client Object Model:

    public static void DownloadFile()
    {
        using (ClientContext clientContext = new ClientContext("yoursite"))
        {
            string libName = "Documents";
            string pwd = "yourpasswordhere";
            string account = "youraccount";
    
            SecureString passWord = new SecureString();
            foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
            clientContext.Credentials = new SharePointOnlineCredentials(account, passWord);
                            
            List list = clientContext.Web.Lists.GetByTitle(libName);
            var listItem = list.GetItemById(2);
            clientContext.Load(listItem, i => i.File);
            clientContext.ExecuteQuery();
    
            var fileRef = listItem.File.ServerRelativeUrl;
            var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
            var fileName = Path.Combine("c://", (string)listItem.File.Name);
    
            Console.WriteLine(fileRef);
            Console.WriteLine(fileInfo);
            Console.WriteLine(fileName);
    
            using (var fileStream = System.IO.File.Create(fileName))
            {
    
                fileInfo.Stream.CopyTo(fileStream);
            }      
        }   
    }

    Feel free to reply if there are still any questions. 

    Best regards

    Patrick Liang
    TechNet Community Support

    Wednesday, June 11, 2014 11:13 AM
    Moderator

All replies

  • Hello Manikant,

    The current forum is for VSTO specific questions.

    I'd recommend asking Office 365 related questions in the SharePoint 2013 - Development and Programming  forum instead.

    Tuesday, June 10, 2014 1:15 PM
  • Thanks for suggestion I moved it...
    Wednesday, June 11, 2014 5:49 AM
  • Hi,

    You can try the code below to download file from your site using Client Object Model:

    public static void DownloadFile()
    {
        using (ClientContext clientContext = new ClientContext("yoursite"))
        {
            string libName = "Documents";
            string pwd = "yourpasswordhere";
            string account = "youraccount";
    
            SecureString passWord = new SecureString();
            foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
            clientContext.Credentials = new SharePointOnlineCredentials(account, passWord);
                            
            List list = clientContext.Web.Lists.GetByTitle(libName);
            var listItem = list.GetItemById(2);
            clientContext.Load(listItem, i => i.File);
            clientContext.ExecuteQuery();
    
            var fileRef = listItem.File.ServerRelativeUrl;
            var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
            var fileName = Path.Combine("c://", (string)listItem.File.Name);
    
            Console.WriteLine(fileRef);
            Console.WriteLine(fileInfo);
            Console.WriteLine(fileName);
    
            using (var fileStream = System.IO.File.Create(fileName))
            {
    
                fileInfo.Stream.CopyTo(fileStream);
            }      
        }   
    }

    Feel free to reply if there are still any questions. 

    Best regards

    Patrick Liang
    TechNet Community Support

    Wednesday, June 11, 2014 11:13 AM
    Moderator