el_Bacco


Hey,

I am trying to programme a rss-reader web part.

I am the code from Ted Pattison's book - Inside (Microsoft) Windows SharePoint Services (~page 118). He combines the RSS Viewer with a xslt-file. But as very often in this book, he misses to explain how he is doing it.

Hopefully somebody can help here.

What I did.

1) Put the code into a cs file (RssViewWebPart.cs)
2) Put the xslt-code into a xslt file (called it RSS.xslt)
3) Under RSS.xslt properties (Advanced) --> Built Action: I took Embedded Resource
4) Built the solution
5) Droped it into the bin-folder, set trust level to full....
web part is not working...

Error:
The "RssViewWebPart" Web Part appears to be causing a problem. Object reference not set to an instance of an object.


Maybe this can help (Teds words...)
...is a simple xslt file for an RSS WEb Part application that will transform the output by using the xslcompiledtransform class. this file is compiled in as an embedded resource in this example, with the full name LitwareWebParts.Resources.RSS.xslt.....

Any help is highly appreciated :-)


Re: SharePoint - Development and Programming xslt file - embedded resource - web part is not working...

daniel.larson

Howdy!

Quick correction:

The example is from Ted Pattison and Daniel Larson's book...

Hey- that's my code! Did you compare your code against the code sample You'll need the fully qualified name of the compiled resource to read it using the resource reader. You can grab our code samples from http://tedpattison.com/insidewss/.

Let me know if that helps out-- feel free to post your code and I'll look at it.

Oh-- and THANKS for reading our book!




Re: SharePoint - Development and Programming xslt file - embedded resource - web part is not working...

MKeeper

Hi Dan ... great to see you on the forums!

I've read the book too, it's a great read (in fact my office now has 3 copies doing the rounds).

Helped me with my MCTS no end Smile






Re: SharePoint - Development and Programming xslt file - embedded resource - web part is not working...

el_Bacco

Hey Daniel,

Wow, I am impressed. Thanks for answering. And sorry for not mentioning you as author - it won't happen again

After some tests it is still not working as it should.

So first to your code:
1) I installed your files - it is working... at least somehow. After a while it lists some SharePoint feeds. But I can't access them via "Show RSS" - nothing happens if I click the button.

2) I was trying to receive feeds from other pages too. But nothing happened...
E.g. the url: http://www.spiegel.de/schlagzeilen/rss/0,5291,676,00.xml - accept it with OK. Still everything as before. Shall this be so (does the reader support external pages )


Let's come to my code... Please keep in mind - that all I want is to read some rss feeds. That is already enough.

3)
Same code as your in the files:
RSS.xslt
RssTitles.xslt
--> created both in a Resources folder and set built Action to Embedded Resource.

RssViewEditorPart.cs
WebPartResources.cs are copies as well.

Code Snippet
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Reflection;

namespace LitwareWebParts
{
public class RssViewWebPart : WebPart, IWebEditable
{
private Stream xmlResponseStream = null;
private WebRequest xmlReq;

public enum RenderMode
{
Full,
Titles
}

private string xmlUrl;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(false),
WebDisplayName("Feed Url"), WebDescription("Set your RSS feed's XML URL here!"),
System.ComponentModel.Category("Configuration")]
public string XmlUrl
{
get { return xmlUrl; }
set { xmlUrl = value; }
}

private RenderMode headlineMode = RenderMode.Full;
[Personalizable(PersonalizationScope.User), WebBrowsable(false),
WebDisplayName("Headline Mode"), WebDescription("Do you want to only display headlines "),
System.ComponentModel.Category("Configuration")]
public RenderMode HeadlineMode
{
get { return headlineMode; }
set { headlineMode = value; }
}

public override EditorPartCollection CreateEditorParts()
{
List<EditorPart> editorParts = new List<EditorPart>(1);
EditorPart part = new RssViewEditorPart();
part.ID = this.ID + "_rssViewEditor";
editorParts.Add(part);
EditorPartCollection baseParts = base.CreateEditorParts();
return new EditorPartCollection(baseParts, editorParts);
}


// Book page 116
// xmlUrl and RenderMOde properties omitted for clarity
// Handle any prerender tasks including async operation initiation
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (string.IsNullOrEmpty(this.xmlUrl))
return;
//Check to see if we're in design mode and if so skip request
if (this.WebPartManager.DisplayMode.AllowPageDesign)
{
this.Controls.Add(new LiteralControl("No display while in design mode. "));
return;
}

try
{
Uri xmlUri = new Uri(this.xmlUrl);
xmlReq = WebRequest.CreateDefault(xmlUri);
xmlReq.Credentials = CredentialCache.DefaultCredentials;
xmlReq.Timeout = 10000; // 10 Secs timeout
this.Page.RegisterAsyncTask(
new PageAsyncTask(new BeginEventHandler(BeginXmlRequest),
new EndEventHandler(EndXmlRequest), new EndEventHandler(XmlRequestTimeout),
null, true)
);
}
catch (System.Security.SecurityException)
{
this.Controls.Add(
new LiteralControl("Permission denied - set trust level to WSS_Medium."));
}
}

IAsyncResult BeginXmlRequest(object src, EventArgs args, AsyncCallback callback, object state)
{
return this.xmlReq.BeginGetResponse(callback, state);
}

void XmlRequestTimeout(IAsyncResult ar)
{
Label timeoutLabel = new Label();
timeoutLabel.Text = string.Format("The request time out while waiting for {0}.", this.XmlUrl);
this.Controls.Add(timeoutLabel);
}

void EndXmlRequest(IAsyncResult ar)
{
WebResponse response = this.xmlReq.EndGetResponse (ar);
this.xmlResponseStream = response.GetResponseStream();
}

protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
if (string.IsNullOrEmpty(this.xmlUrl) || this.xmlResponseStream == null)
return;
XslCompiledTransform transform = new XslCompiledTransform();
string xslt;
if (this.HeadlineMode == RenderMode.Full)
xslt = @"Resources.RSS.xslt";
else
xslt = @"Resources.RssTitles.xslt";
string resourceName = @"LitwareWebParts" + xslt;
using (Stream res = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
using (XmlTextReader stylesheet = new XmlTextReader(res))
{
transform.Load(stylesheet);
}
}

try {
using (XmlReader reader = new XmlTextReader(this.xmlResponseStream)){
XmlTextWriter results = new XmlTextWriter(writer.InnerWriter);
transform.Transform(reader, results);
reader.Close();
}
}
catch (Exception ex ){
writer.Write(ex.Message);
if (this.xmlResponseStream != null ){
this.xmlResponseStream.Close();
this.xmlResponseStream.Dispose();
}
}
}
}
}



I added the
public enum RenderMode
{
Full,
Titles
}
at the top.

The rest should be the same as in your RssViewWebPart4.

I saw that your prog creates a LitwareWebParts.resources.dll - mine does not. Maybe that is helpful.

There is still an error after entering the url (www.spiegel....)
"The "RssViewWebPart" Web Part appears to be causing a problem. Object reference not set to an instance of an object."


Puh, thats all for now. Don'tknow what else I could do.

Thanks for any help!!!







Re: SharePoint - Development and Programming xslt file - embedded resource - web part is not working...

daniel.larson

Thanks for posting the code-- that clears it up.

So-- it appears there's a typo in the code listing! Thanks for finding it. The code listing should use the fully qualified name of the resource, which is missing a dot in the book. It's hard to catch all that in editing!!!

Here is the correct code:
if (this.HeadlineMode == RenderMode.Full)
xslt = @"Resources.RSS.xslt";
else
xslt = @"Resources.RssTitles.xslt";
string resourceName = @"LitwareWebParts." + xslt; // Notice the extra dot here...


Also, you should be able to step through the code (attach to the W3WP.exe process) to debug this code-- you'll see that you're getting the error at this line:
using (Stream res = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))

So... try that out, and let me know your success!




Re: SharePoint - Development and Programming xslt file - embedded resource - web part is not working...

el_Bacco



Yeaaaaaaaaaaaaaaaaaaaah!!!!!!!!!!!!!

Finally it worked. Honestly I doubted it... but the result is perfect.

Good feeling. Thanks Daniel!