Wednesday, August 30, 2017

office/file from SharePoint Online



Sample DOwnload a file which is inside SharePoint Online

Building the Sample

Prior to work with the sample you must have an SharePoint online Site. 

A File uploaded anywhere on SharePoint.

Run the Sample and ENter the info

1. SiteUrl: Url of SharePoint Site.

2. User Name : Name of the authenticated user who has permission on sharePoint.

3. Password: Password of the User specified above.

4. File Url Url of file to be downloaded



Click Download.

file will be downloaded in the exe folder.


This sample includes the SharePoint Client dll Inside SharepointClient2013 folder.


It uses this dll to make a requst to the SharePoint passing thje Credential object which is created by username and password.


1. It initialized the ClientContext object of SharePoint Client dll.

by assigning the SharePointOnlineCredential object 


It read the userName and password.

Convert password into a secure string than initilizes the SharePointOnlineCredential object.

This object is than assigned to ClientContext.Credentials object.


Now to download a file from URL you must initialize the Web Object and than pass the ServerRelativeURl of file.


To get the File ServerRelativeURL sample first get the Web ServerRelativeUrl.

to get the Properties you must load it in client context

    Web web = clientContext.Web;               

clientContext.Load(web, website => website.ServerRelativeUrl);   


after loading youmust execute it as query.          


Now once you have the ServerRelativeURL use regex to get File Relative URL

 Regex regex = new Regex(SiteUrl, RegexOptions.IgnoreCase);                string strSiteRelavtiveURL = regex.Replace(FileUrl, string.Empty);                string strServerRelativeURL = CombineUrl(web.ServerRelativeUrl, strSiteRelavtiveURL);

Build the sample

Press the F5 key to build and deploy the app.


 /// <summary>           /// Get file information from Sharepoint.           /// </summary>           /// <param name="documentUrl">Document Url.</param>           /// <returns>Returns file information object.</returns>           public Stream GetFile()           {               using (ClientContext clientContext = GetContextObject())               {                      Web web = clientContext.Web;                   clientContext.Load(web, website => website.ServerRelativeUrl);                   clientContext.ExecuteQuery();                   Regex regex = new Regex(SiteUrl, RegexOptions.IgnoreCase);                   string strSiteRelavtiveURL = regex.Replace(FileUrl, string.Empty);                   string strServerRelativeURL = CombineUrl(web.ServerRelativeUrl, strSiteRelavtiveURL);                      Microsoft.SharePoint.Client.File oFile = web.GetFileByServerRelativeUrl(strServerRelativeURL);                   clientContext.Load(oFile);                   ClientResult<Stream> stream = oFile.OpenBinaryStream();                   clientContext.ExecuteQuery();                   return this.ReadFully(stream.Value);               }           }      private ClientContext GetContextObject()           {               ClientContext context = new ClientContext(SiteUrl);               context.Credentials = new SharePointOnlineCredentials(UserName, Password);               return context;           }      private static SecureString GetPasswordFromConsoleInput(string password)           {               //Get the user's password as a SecureString               SecureString securePassword = new SecureString();               char[] arrPassword = password.ToCharArray();               foreach (char c in arrPassword)               {                   securePassword.AppendChar(c);               }                  return securePassword;           }

Source Code Files

More Information

For more information on X, see ...?

No comments:

Post a Comment