Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have made a few HTML files which I intend to use as either email templates or schemas.

When I run this code:

var t = "Not found!";
        SPSecurity.RunWithElevatedPrivileges(() =>
         {
             using (var site = new SPSite(spSite.ID))
             {
                 var web = site.RootWeb;
                 var file = web.GetFile("/_layouts/email/" + template + ".html");
                 t = Encoding.Default.GetString(file.OpenBinary());
             }
         });
        return t;

my method returns this error: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

Is it not possible to get files from /_layouts/ when not logged in?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

While I'm not 100% certain, I suspect that your problem is that you are using a SharePoint-specific method to access a file that is not actually in SharePoint. Everything in /_layouts/ is actually in the underlying file system of the server.

What might work better on a lot of fronts is to move your templates into Sharepoint, either in their own document library or under /Style Library/ somewhere. Not only would that make your method above work, but it would also ensure that any updates to the templates would be reflected across the farm, without having to copy the html files manually.

Just a thought.

share|improve this answer
add comment

Use var spTemplatePath = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\email\" + template + ".html"); to get the path on the the filesystem, and then just open a filestream.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.