q36988 发表于 2015-9-26 11:27:17

sharepoint使用自定义RSS webpart获取weather天气、stock股票信息

Web Part Code
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Xml;
using System.Text;
using UtilityLibrary;
namespace Concept.RSSFeed
{
   
    public class RSSFeed : System.Web.UI.WebControls.WebParts.WebPart
    {
      #region Private Variables
      private bool displayXml = false;
      private string xsltFile = "";
      private string xmlFeed = String.Empty;
      #endregion
      #region Public Properties
      /// <summary>
      /// allows user to specify an xslt file.
      /// </summary>
      
      public string XSLTFile
      {
            get { return xsltFile; }
            set { xsltFile = value.Trim(); }
      }

      /// <summary>
      /// allows user to specify the url of the rss feed
      /// </summary>
      
      public string XMLFeed
      {
            get { return xmlFeed; }
            set { xmlFeed = value.Trim(); }
      }
      /// <summary>
      /// allows user to specify the url of the rss feed
      /// </summary>
      
      public bool DisplayXML
      {
            get { return displayXml; }
            set { displayXml = value; }
      }
      #endregion
      public RSSFeed()
      {
      }
      protected override void CreateChildControls()
      {
            base.CreateChildControls();
            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
      }
      protected override void Render(HtmlTextWriter writer)
      {
            try
            {
                #region Check Properties
                if (String.IsNullOrEmpty(XSLTFile))
                  throw new ConceptException("XSLT File is blank");
                if (String.IsNullOrEmpty(XMLFeed))
                  throw new ConceptException("XML Feed is blank");
                #endregion

                XmlDocument weatherFeed = new XmlDocument();
                string arrxml = "";
                if (XMLFeed.Contains("$"))
                {
                  string[] feedarr = XMLFeed.Split('$');
                  for (int i = 0; i < feedarr.Length; i++)
                  {
                        XmlDocument weatherarr = WebClientUtil.GetRSSFeed(feedarr.ToString());
                        arrxml += weatherarr.DocumentElement.InnerXml;
                  }
                  StringBuilder sb = new StringBuilder();
                  sb.Append("<root>").Append(arrxml).Append("</root>");
                  weatherFeed.LoadXml(sb.ToString());
                }
                else
                {
                  weatherFeed = WebClientUtil.GetRSSFeed(XMLFeed);
                }

                // get xml from feed               

                if (DisplayXML)
                {
                  UtilityLibrary.WebPartUtil.SurroundInTextarea(writer, weatherFeed);
                }

                // transform to HTML
                Uri currentPage = this.Page.Request.Url;
                int pathIndex = currentPage.AbsoluteUri.IndexOf(currentPage.AbsolutePath);
                string uriString = string.Empty;
                if (pathIndex > 0)
                {
                  uriString = currentPage.AbsoluteUri.Substring(0, pathIndex);
                }
                currentPage = new Uri(String.Format("{0}/Style Library/XSL Style Sheets/{1}", uriString, XSLTFile));
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                  XMLUtil.XSLUtil.SPSTransformXML(weatherFeed, currentPage, null, writer);
                });
                base.RenderContents(writer);
            }
            catch (ConceptException ex)
            {
                WebPartUtil.HandleWebPartApplicationException(writer, ex, this.ID);
            }
            catch (Exception ex)
            {
                WebPartUtil.HandleWebPartError(writer, ex, this.ID);
            }
      }
    }
}
  
  天气rss
  http://xml.weather.yahoo.com/forecastrss?p=CAXX0504&u=c$http://xml.weather.yahoo.com/forecastrss?p=CAXX0301&u=c$http://xml.weather.yahoo.com/forecastrss?p=CAXX0518&u=c$http://xml.weather.yahoo.com/forecastrss?p=CAXX0183&u=c$http://xml.weather.yahoo.com/forecastrss?p=CAXX0054&u=c
  
  天气xslt

Weather XML
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:user="anything here"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
    <link rel="stylesheet" type="text/css" href="/_layouts/Corus/Corus.Weather/Corus_Weather_Feeds.css"/>
    <script language="JavaScript" type="text/JavaScript" src="/_layouts/Corus/Corus.Weather/weather.js"></script>
    <div class="weather_feeds">
      <span class="span_info">Weather for:</span><span id="lable_current_location"></span>
      <select id="select_loation" style="display:none;" onchange="selectweatherloaction();">
      <option value="1">Toronto</option>
      <option value="2">Montreal</option>
      <option value="3">Vancouver</option>
      <option value="4">Halifax</option>
      <option value="5">Calgary</option>
      </select><span id="span_select_loation" onclick="span_click();" title="Change Location">▼</span>
      <xsl:for-each select="/root/channel">
      <table>
          <xsl:attribute name="id">weather_table<xsl:value-ofselect="position()"/>
          </xsl:attribute>
          <xsl:attribute name="style">
            display:none;
          </xsl:attribute>
          <tr>
            <td rowspan="2" class="td_img">
            <xsl:element name="img">
                <xsl:attribute name="src">
                  http://l.yimg.com/a/i/us/we/52/<xsl:value-of select="item/yweather:condition/@code" />.gif
                </xsl:attribute>
            </xsl:element>
            </td>
            <td class="td_today">
            <span class="span_today">
                <xsl:value-of select="item/yweather:condition/@temp" />
                <span style="color:#333;">°</span>
            </span>
            <span class="span_today_text">
                <xsl:value-of select="item/yweather:condition/@text" />
            </span>
            </td>
          </tr>
          <tr>
            <td class="td_after">
            <span><xsl:value-of select="item/yweather:forecast/@day" />:</span><xsl:value-of select="item/yweather:forecast/@low" />°~<xsl:value-of select="item/yweather:forecast/@high" />°   <xsl:value-of select="item/yweather:forecast/@text" />
            <br />
            <span><xsl:value-of select="item/yweather:forecast/@day" />:</span><xsl:value-of select="item/yweather:forecast/@low" />°~<xsl:value-of select="item/yweather:forecast/@high" />°   <xsl:value-of select="item/yweather:forecast/@text" />
            </td>
          </tr>
      </table>
      </xsl:for-each>
      <script language="JavaScript" type="text/JavaScript">
      var tablecount=<xsl:value-of select="count(/root/channel)" />;
      var select_loation=document.getElementById("select_loation");
      var lable_current_location=document.getElementById("lable_current_location");
      if(GetCookie("locationweather")==null)
      {
      SetCookie("locationweather", select_loation.options.value);
      lable_current_location.innerHTML=select_loation.options.text;
      //select_loation.style.display="";
      }
      else
      {
      lable_current_location.innerHTML=select_loation.options.text;
      select_loation.options.selected=true;
      select_loation.style.display="none";
      }
      function span_click()
      {
      if(select_loation.style.display=="none"){
      select_loation.style.display="";
      lable_current_location.style.display="none";
      }
      else{select_loation.style.display="none";
      lable_current_location.style.display="";
      }
      }
      function selectweatherloaction()
      {
      var Days = 30;
      var exp= new Date();
      exp.setTime(exp.getTime() + Days*24*60*60*1000);
      document.cookie="locationweather="+escape(select_loation.options.value)+";expires=" + exp.toGMTString()+ ";path=/";
      lable_current_location.innerHTML=select_loation.options.text;
      select_loation.style.display="none";
      lable_current_location.style.display="";
      <![CDATA[
          for(var i=1;i<=tablecount;i++)
          {
          var table_current="weather_table"+i;
          if(i==GetCookie("locationweather")){
          document.getElementById(table_current).style.display="";}
          else{document.getElementById(table_current).style.display="none";}
          }
            ]]>
      }
      var table_current="weather_table"+GetCookie("locationweather");
      document.getElementById(table_current).style.display="";
      
      </script>
    </div>
</xsl:template>
</xsl:stylesheet>
  
  股票rss
  http://xpi1.xpi.com/corus/quote.aspx?Ticker=T.CJR.B$http://xpi1.xpi.com/corus/quote.aspx?Ticker=CJR%20Y
  
  股票xslt

Code
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
    <link rel="stylesheet" type="text/css" href="/_layouts/Corus/Corus_Stock_Feeds.css"/>
    <div class="stock_feeds">
      <span class="stock_feeds_title corus_webpart_border_bottom_color">Stock Information</span>
      <table class="stock_feeds_table">
      <xsl:if test="count(/root/record) > 0">
          <xsl:for-each select="/root/record">
            <tr>
            <td class="td_align_left">
                <xsl:value-of select="recordheader/ticker" />
            </td>
            <td class="td_align_right">
                <xsl:value-of select="recordheader/exchange" />
            </td>
            <td class="td_align_left" style="padding-left:5px;">
                $<xsl:value-of select="marketdata/last" />
                <xsl:if test="marketdata/change/points&gt;0">
                  <img src="/_layouts/images/Branding/Corus/Insite/price_up.gif" />
                </xsl:if>
                <xsl:if test="marketdata/change/points&lt;0">
                  <img src="/_layouts/images/Branding/Corus/Insite/price_down.gif" />
                </xsl:if>
            </td>
            <td class="td_align_right">
                <xsl:value-of select="marketdata/change/percent" />
            </td>
            </tr>
          </xsl:for-each>
      </xsl:if>
      </table>
      <a href="http://www.corusent.com/home/Corporate/InvestorRelations/StockInfo/tabid/1676/Default.aspx?ticker=CJR.B.CA,CJR">
      <span class="span_link"></span>
      </a>
    </div>
</xsl:template>
</xsl:stylesheet>
  
  webpart
页: [1]
查看完整版本: sharepoint使用自定义RSS webpart获取weather天气、stock股票信息