Get files from _layouts folder in SharePoint
Asked Answered
P

1

5

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.

Prophets answered 19/6, 2012 at 9:40 Comment(1)
possible duplicate of Get Directory Path to 12 Hive programmaticallySaccharoid
K
7

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.

Kamila answered 19/6, 2012 at 10:56 Comment(1)
I know this is old but people will find it on Google. This is now obsolete in SP2013. msdn.microsoft.com/en-us/library/…Caput

© 2022 - 2024 — McMap. All rights reserved.