title: Open secure Sharepoint file programmatically using c#
body: <p>I need help opening am online Sharepoint file programmatically. These files need to be secured on Sharepoint (so that users can't wander into them), but I also need to access it somehow using my webpart (which will check for security). Normally, you can just use a hyperlink that links to the Sharepoint file, but we don't want people sharing these hyperlinks, so that won't work.</p>
  
  <p>I want the user to click a link, and have them see the "Open, Save, Close" dialog menu for that file. </p>
  
  <p>I have tried this </p>
  
  <pre><code>            String fileName = @"file.txt";
              String filePath = @"~online filePath/"; 
  
              System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
              response.ClearHeaders();
              response.ClearContent();
              response.Clear();
              response.ContentType = "text/plain"; 
              //or whatever file type, I can code that later
              response.AddHeader("Content-Disposition", "attachment; Filename=" + fileName + ";");
              response.TransmitFile(filePath + fileName);
              //response.Flush();
              response.End();
  </code></pre>
  
  <p>Unfortunately, while this works fine for local files (C://...), when I try this on my web fike, I only receive a text document that has the source code for the current page. Not sure why.</p>
  
  <p>As for dealing with the permissions, I plan to use elevated priviledges to have the program gain access to the files. </p>
  
  <p>I would appreciate any help, Thanks.</p>