SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My code behind code from the aspx webpage uses a transform.xsl to do some custom xslt transformation.

Scenario 1: Try to get the file from the _layouts folder.

SPWeb web = SPContext.Current.Web;
SPFile file = web.GetFile(@"/layouts/MyFeature/transform.xsl");

Note : The file is accesible on this url http://mysite:8080/_layouts/MyFeature/transform.xsl when using the browser.

Scenario 2: Try to get the file from the current (http://mysite:8080/MyFeature/MyPage.aspx) url.

SPWeb web = SPContext.Current.Web;
SPFile file = web.GetFile(@"transform.xsl");


SPFile file = web.GetFile(@"MyFeature/transform.xsl");


SPFile file = web.GetFile(@"/MyFeature/transform.xsl");

All scenarios fail, how to solve this?

The 'transform.xsl' is now located at 'MyFeature/transform.xsl' and I've followed the tip from Anders Rask, and my code looks like this now:

SPWeb web = SPContext.Current.Web;
XslCompiledTransform transformer = new XslCompiledTransform();

XsltSettings settings = new XsltSettings(true, true);
string xslLocation = web.Url + "/FormWrapper/transform.xsl";
transformer.Load(xslLocation, settings, GetResolverWithDefaultCredentials());

// Do XSLT transform...
share|improve this question
up vote 3 down vote accepted

I'm pretty sure SPWeb.GetFile() returns your site pages, like default.aspx. Also your XSLT file isnt an SPFile if its located in the _LAYOUTS folder.

Instead try something similar to the below code (note boilerplate code so error handling etc should be added)

// fetch xsl processor
XslCompiledTransform processor = GetXslt(@"/layouts/MyFeature/transform.xsl");
StringWriter writer = new StringWriter(CultureInfo.CurrentCulture);

// Transform XSLT code here

private static XslCompiledTransform GetXslt(string xsltUrl)
    XslCompiledTransform processor = new XslCompiledTransform(false);
    bool enableDocumentFunctions = true;
    bool enableScript = true;
    XsltSettings settings = new XsltSettings(enableDocumentFunctions, enableScript);

    // add error handling here
    processor.Load(xsltUrl, settings, GetResolverWithDefaultCredentials());
    return processor;

// use resolver to avoid getting security exceptions
private static XmlUrlResolver GetResolverWithDefaultCredentials()
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
    return resolver;
share|improve this answer

You could write a Module Feature that "uploads" your transform.xsl into a document library (Style Library for example) which allows you to access the file as an SPFile.

share|improve this answer
In the current implementation I manually uploaded the xsl file to the Documents library. Your solution sounds good, but the solution from Anders Rask fits more in our design. – Stef Feb 24 '10 at 17:35

Your Answer


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.