views:

316

answers:

2

Hi all:

I'm having difficulties getting XslCompiledTransform.Load method to take a server path. I googled around and found that I need to do something like:

xslt.Load(System.Web.HttpContext.Server.MapPath(xslPath), XsltSettings.Default, new XmlUrlResolver());

But it returned an error saying HttpContext is null.

I also tried:

xslt.Load(System.Web.HttpServerUtility.MapPath(xslPath), XsltSettings.Default, new XmlUrlResolver());

That also returned an error saying an object reference is required for the non-static field, method, or property 'System.Web.HttpServerUtility.MapPath(string)'

The xslPath has a path that points to a xsl file in Sharepoint Web. I just want XslCompiledTransform to load the xsl file with the server path. Is it possible? If so, what is the proper way or hackish way of doing it?

EDIT: I have access to a SPWeb object which contains the path to the xsl file. However when I check the ServerRelativeUrl, it just says "/MyTree/xsl.xsl". The problem here is I couldn't get the XslCompiledTransform.Load to load the file from SharePoint list.

Thanks.

A: 

During a the processing of a request, the current HttpContext is HttpContext.Current. In a Page/UserControl/WebPart this is also the property Context.

HttpContext.Context.Server.MapPath(xslPath)

If your method is not called during the processing of a request, HttpContext.Current will be null. In this case you could map the path manually.

public string MapPath(string path)
{
    if (HttpContext.Current != null)
        return HttpContext.Current.Server.MapPath(path);

    path = path.Replace("/", @"\");
    if (path.StartsWith(@"~\")) {
        path = path.Substring( 2 );
    } else if (path.StartsWith(@"\")) {
        path = path.Substring( 1 );
    }
    // a non-prefixed path is already relative to your web server root

    return Path.Combine( HttpRuntime.AppDomainAppPath, path );
}

The above is for mapping disk paths in ASP.NET in general.

If the file is contained in you SPWeb object, you should use SPWeb.GetFile

SpWeb web;

SPFile file = web.GetFile( path );

XmlReader r = XmlReader.Create( file.OpenBinaryStream() );
xslt.Load( r );
Lachlan Roche
@Lachlan Roche: what package is Path from? Also, the path of the xsl file is retrieved from Sharepoint so it doesn't start with ~/ or /. I think it just comes straight from a list.
BeraCim
@BeraCim System.IO.Path
Lachlan Roche
@Lachlan Roche: thanks for the reply. I putted in the ~ in xslpath. Unfortunately this time the Load method said it could not find a part of the path C:\Inetpub\wwwroot\wss\VirtualDirectories\80\~\{original xslpath directory string}. If I take out the ~, the root directory turned to c:\ instead. How do I make the Load method to point to the actual Sharepoint List instead of local system?
BeraCim
@Lachlan Roche: that bugger load method still couldnt find the file... its either the AppDomainPath (wwwroot\wss\) or c:\blahblahblah doesnt matter whether its forward or backward slash. I tried with the XmlReader, and that too refuses to take any SharePoint related path.
BeraCim
@BeraCim is the xslt actually on disk? or is it in the SharePoint virtual path provider?
Lachlan Roche
@Lachlan Roche: the xsl is in a Sharepoint list.
BeraCim
A: 

What I found in the end is that if I pass in a url that does not resemble a local path, the XslCompiledTransform class will automatically switch to a different mode to read the path as a URL.

If I pass in a string that contains only the name of the file, XslCompiledTransform will look for the file in my local hard disk. Likewise if I pass in something like /myFolder/myXsl.xsl.

However, if I pass in a sharepoint URL e.g. web.ParentWeb.Url + NameOfFile, then it'll go off to read it from the Sharepoint URL.

I'm not 100% sure why it does the automatic switch, but at least the above worked for me.

BeraCim