Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get files from _layouts folder in SharePoint

I'm currently working on a project where I'm adding docx-files to the Layout folder in Visual Studio and then use those ducment files to create Content Types.

The problem is that I can't get the document files programmatically. Using the web browser I can get the files but not using web.GetFolder();. The code is running in a Feature Receiver when a feature is activated.

SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb;

SPFolder docTempFolder = web.GetFolder("_LAYOUTS/Projekt/DocumentTemplates");

This code give me a collection with zero files.

What am I doing wrong?

Thanks for helping.

like image 603
carruw Avatar asked Jun 19 '12 09:06

carruw


1 Answers

SPFolder is for getting objects from SharePoint document libraries/lists. You can't access files in your file system (the 14 hive) by trying to cast them as an SPFolder. Also you can't use SPWeb.GetFolder as the files are nowhere near your web. They are on your harddisk.

You could get the 14 hive by using GetGenericSetupPath, so something like this would work:

var path = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\Projekt\DocumentTemplates");

This path you could now Access with regular stream readers, System.IO.File etc.


EDIT: For SharePoint 2013 and above the mentioned method is obsolete as in newer SharePoint versions references to the _layouts folder always contain the SharePoint version in the path (e.g. /_layouts/15 for SharePoint 2013 /_layouts/14 for SharePoint 2010.

The new method is SPUtility.GetVersionedGenericSetupPath. The method does not exist in SharePoint 2010.

like image 59
Dennis G Avatar answered Sep 22 '22 17:09

Dennis G