1

I have a little program that queries a web service, transforms the results using XSLT and displays them in a jQuery control. When I move the code into a web part it and deploy it, however I'm getting the following error:

The remote server returned an error: (401) Unauthorized.

This is the code that's loading my XSLT file, which is where I suspect the problem is:

XPathDocument xPathDoc;
XsltArgumentList args;

protected override void OnPreRender(EventArgs e)
{
  xPathDoc = QueryRunner.GetQueryResults();
  args = QueryRunner.GetColumnsToShow();
}

public static XPathDocument GetQueryResults()
{
  OpacAccess.OpacAccess proxy = new OpacAccess.OpacAccess();

  string result = string.Empty;

  try
  {
     result = proxy.query("CompanyName", "username", "password", 
         ReadingList=\"London\"", 1000);
  }
  catch (Exception ex)
  {
  }

  // Store the xml of the result
  XPathDocument xPathDoc = new XPathDocument(new StringReader(result));

  return xPathDoc;
}   

protected override void RenderContents(HtmlTextWriter output)
{
  XslTransform xslt = new XslTransform();

  // Load the xslt file and perform the transformation
  xslt.Load("http://rginfonet/Style%20Library/XSL%20Style%20Sheets/Liberty.xslt");
  xslt.Transform(xPathDoc, args, output, null);
  output.Flush();
}

Here's my stack trace if it's at all helpful to anyone:

[WebException: The remote server returned an error: (401) Unauthorized.] System.Net.HttpWebRequest.GetResponse() +5375213 System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials) +69 System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials) +3929371 System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +54 System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver) +74 System.Threading.CompressedStack.runTryCode(Object userData) +70 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) +0 System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state) +108 System.Xml.XmlTextReaderImpl.OpenUrl() +186 System.Xml.XmlTextReaderImpl.Read() +208 System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space) +217 System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space) +20 System.Xml.Xsl.XsltOld.Compiler.LoadDocument(XmlTextReaderImpl reader) +85 System.Xml.Xsl.XslTransform.Load(String url, XmlResolver resolver) +100 System.Xml.Xsl.XslTransform.Load(String url) +25 LibertyControl.ResultsGrid.RenderContents(HtmlTextWriter output) +74 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 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

[WebPartException: The remote server returned an error: (401) Unauthorized.] Microsoft.SharePoint.WebPartPages.SPChrome.RenderPartContents(HtmlTextWriter output, WebPart part) +140 Microsoft.SharePoint.WebPartPages.SPChrome.RenderWebPart(HtmlTextWriter output, WebPart part) +63 Microsoft.SharePoint.WebPartPages.WebPartZone.RenderZoneCell(HtmlTextWriter output, Boolean bMoreParts, WebPart part) +144 Microsoft.SharePoint.WebPartPages.WebPartZone.RenderWebParts(HtmlTextWriter output, ArrayList webParts) +495 Microsoft.SharePoint.WebPartPages.WebPartZone.Render(HtmlTextWriter output) +902 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.Control.Render(HtmlTextWriter writer) +10 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.HtmlControls.HtmlContainerControl.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.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +163 System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32 System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +51 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27 System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99 System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134 System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19 System.Web.UI.HtmlControls.HtmlContainerControl.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.Control.Render(HtmlTextWriter writer) +10 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.Page.Render(HtmlTextWriter writer) +29 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.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

Please let me know if you can lend some insight to what's causing this. Thanks!

6
  • 1
    how do you query the web service ? from the code behind ? What are the authentication mechanism of the web service ?
    – Steve B
    Commented Jun 14, 2011 at 14:16
  • Yes, I'm querying from the code-behind. I'm using a web reference to query the service. I will include the code for the GetQueryResults method in my original question so you can see. I'm not doing any fancy authentication.
    – Music Magi
    Commented Jun 14, 2011 at 14:20
  • "I'm not doing any fancy authentication", but are doing authentication ?
    – Steve B
    Commented Jun 14, 2011 at 14:29
  • and also, how did you create the service reference ? WCF or legacy web reference ?
    – Steve B
    Commented Jun 14, 2011 at 14:31
  • Did you unit test it without any sharepoint? Like just from command line?
    – vasya10
    Commented Jun 14, 2011 at 14:52

1 Answer 1

2

According the stack trace, I think the web service call is not the problem. This line is probably causing the exception :

xslt.Load("http://rginfonet/Style%20Library/XSL%20Style%20Sheets/Liberty.xslt");

In fact, you are facing a douple-hop scenario. Your user connects to SP, where the webparts also try to connect to SP (even if it's local).

Try to change the xslt.Load method to not an Uri, but a stream :

protected override void RenderContents(HtmlTextWriter output)
{
  XslTransform xslt = new XslTransform();
  var web = SPContext.Current.Web;
  var styleFile = web.GetFile("Style%20Library/XSL%20Style%20Sheets/Liberty.xslt");
  using(var stream = styleFile.OpenBinaryStream())
  {
    using(var xr = XmlReader.CreateDefault(stream))
    {
      // Load the xslt file and perform the transformation
      xslt.Load(xr);
      xslt.Transform(xPathDoc, args, output, null);
      }
  }
  output.Flush();
}
1
  • I had to tweak the code a bit, but I got this to work. Thanks! Now, I just need to get my jQuery working.
    – Music Magi
    Commented Jun 14, 2011 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.