Lev Waisberg
Lev Waisberg

Reputation: 11

How to store temp files in SharePoint 2010 hosted code?

I have a hosted web service in SharePoint 2010 that does uploads and downloads to sharepoint. Because the files can be large (100MB+), i would much rather use temp files as the streams the code goes through instead of memory-streams to avoid 100mb memory allocations each time it does download/upload.

The problem is that i could find a location in the server to store temp files. System.IO.Path.GetTempFileName() throws an error because the authenticated user doesn't have permissions to %TEMP% folder in the server. "%systemroot%\temp" allows writing files but not deleting them.

Any idea if i can get a location from sharepoint that is accessible for any authenticated user to store the files?

few notes:

  1. the files are temporary and need to be deleted right away so no need to consider clustering issues.

  2. I don't want a solution that requires doing any active action in the servers as this plugin might be deployed on farms with a lot of servers and i'd hate to ask the customer to go through each server.

Thanks.

Upvotes: 1

Views: 2892

Answers (2)

Joe
Joe

Reputation: 39

It might not be the best place but I have used the _layouts directory in the hive before (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS) for doing something similar to this before.

You can get this location with Microsoft.SharePoint.Utilities.SPUtility.GetGenericSetupPath() and you should be able to read/write in the directory. You may need to run as elevated permissions.

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

You need to access files under SharePoint's "system account". And yes, System.IO.Path.GetTempFileName() is correct location.

Starting point - SPSecurity.RunWithElevatedPrivileges.

Notes

  • If you can open files as "temporary + delete on close" (check appropriate flags in FileStream class).
  • Be extremely careful not to perform access to other SharePoint resource (SPFile/SPItem...) while running code inside RunWithElevatedPrivileges delegate.
  • You may only need to open file under RunWithElevatedPrivileges, read/write may work outside - please verify yourself. I'd keep all file access inside delegates running with RunWithElevatedPrivileges.

Upvotes: 2

Related Questions