I am trying to load an XSLT file in a Sharepoint MOSS 2007 custom webpart, but I am getting a 401 error while trying to access an XSLT file. I tried providing default credentials but it is empty upon inspection during debugging.
Does anyone know how I can load the XSLT file from a custom webpart? Thanks in advance.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultNetworkCredentials;
XsltSettings settings = new XsltSettings(true, true);
XslCompiledTransform oXSLTranform = new XslCompiledTransform();
string siteUrl = SPContext.Current.Site.Url;
if (siteUrl.EndsWith("/"))
siteUrl = siteUrl.Remove(siteUrl.LastIndexOf("/"));
siteUrl += "/Style Library/XSL Style Sheets/THM1News.xslt";
oXSLTranform.Load(siteUrl ,settings, resolver);
This code returns a 401 error:
The remote server returned an error: (401) Unauthorized.
[WebException: The remote server returned an error: (401) Unauthorized.] System.Net.HttpWebRequest.GetResponse() +5313085 System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials) +69 System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +3929007 System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +54 System.Xml.Xsl.Xslt.XsltLoader.CreateReader(Uri uri, XmlResolver xmlResolver) +26 System.Xml.Xsl.Xslt.XsltLoader.Load(Compiler compiler, Object stylesheet, XmlResolver xmlResolver) +315 System.Xml.Xsl.Xslt.Compiler.Compile(Object stylesheet, XmlResolver xmlResolver, QilExpression& qil) +41 System.Xml.Xsl.XslCompiledTransform.CompileXsltToQil(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) +59 System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) +66 System.Xml.Xsl.XslCompiledTransform.Load(String stylesheetUri, XsltSettings settings, XmlResolver stylesheetResolver) +38 NewsGallery.AjaxNewsWebPart.AjaxNewsControl.RenderContents(HtmlTextWriter output) +403 System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +32 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) +10 System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +32 Microsoft.SharePoint.WebPartPages.WebPart.RenderWebPart(HtmlTextWriter output) +36 Microsoft.SharePoint.WebPartPages.WebPart.RenderWebPartInternal(HtmlTextWriter writer) +139 Microsoft.SharePoint.WebPartPages.WebPart.Render(HtmlTextWriter writer) +93 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25 Microsoft.SharePoint.WebPartPages.SPChrome.RenderPartContents(HtmlTextWriter output, WebPart part) +66