Thursday, February 3, 2011

Map folder in SharePoint Root

I am sure many of you needed from time to time to get a local path to a file that was deployed to the SharePoint Root (AKA Hive in 2007 version, AKA “12” folder AKA “14” folder).

For whatever reason you needed to do so, you probably notices that the ASP.NET API (Server.MapPath) or any other ASP.NET method of discovering the local path for a file or folder did not work out as excepted.

Without going into a long explanation on why that did not work, There is a very little documented method that I have used a lot in the past and I have learned not many developer know it.

The SPUtility class contains a static method named “GetGenericSetupPath” that takes one string parameter.

That string parameter can be any folder under the SharePoint Root folder, and the result of the method is the local file system path of that folder.

For example, to get the local path for “Template” folder, use this command:
SPUtility.GetGenericSetupPath("Template");

But to get the “Layouts” folder, you have to use this command:
SPUtility.GetGenericSetupPath("Template\\LAYOUTS");

Now, saying that – you have to keep in mind that although you do have read permissions to these folders while inside a SharePoint APP, other permissions (like create file, or create folders) may be blocked.

Cheers,

Shai.

2 comments:

Unknown said...

Very useful post. Thanks you.
However, I think the method is actually "GetGenericSetupPath".

Shai Petel said...

Yeah, you are correct!
fixing it now... thanks!