设为首页 收藏本站
查看: 760|回复: 0

[经验分享] sharepoint使用自定义RSS webpart获取weather天气、stock股票信息

[复制链接]

尚未签到

发表于 2015-9-26 11:27:17 | 显示全部楼层 |阅读模式
DSC0000.gif DSC0001.gif 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
{
    [Guid("40d01bcc-311c-464c-bc33-5e061beb1306")]
    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>
        [WebBrowsable(true), Personalizable(true), WebDisplayName("XSL Filename"), WebDescription("Specify a custom xsl file.")]
        public string XSLTFile
        {
            get { return xsltFile; }
            set { xsltFile = value.Trim(); }
        }

        /// <summary>
        /// allows user to specify the url of the rss feed
        /// </summary>
        [WebBrowsable(true), Personalizable(true), WebDisplayName("Feed URL"), WebDescription("Specify a custom rss url.if have many url,use '$' split.")]
        public string XMLFeed
        {
            get { return xmlFeed; }
            set { xmlFeed = value.Trim(); }
        }
        /// <summary>
        /// allows user to specify the url of the rss feed
        /// </summary>
        [WebBrowsable(true), Personalizable(true), WebDisplayName("Display XML"), WebDescription("Display the resulting XML Stream.")]
        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-of  select="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[1]/@day" />:</span>  <xsl:value-of select="item/yweather:forecast[1]/@low" />°~<xsl:value-of select="item/yweather:forecast[1]/@high" />°   <xsl:value-of select="item/yweather:forecast[1]/@text" />
              <br />
              <span><xsl:value-of select="item/yweather:forecast[2]/@day" />:</span>  <xsl:value-of select="item/yweather:forecast[2]/@low" />°~<xsl:value-of select="item/yweather:forecast[2]/@high" />°   <xsl:value-of select="item/yweather:forecast[2]/@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[select_loation.selectedIndex].value);
        lable_current_location.innerHTML=select_loation.options[select_loation.selectedIndex].text;
        //select_loation.style.display="";
        }
        else
        {
        lable_current_location.innerHTML=select_loation.options[GetCookie("locationweather")-1].text;
        select_loation.options[GetCookie("locationweather")-1].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[select_loation.selectedIndex].value)+";expires=" + exp.toGMTString()+ ";path=/";
        lable_current_location.innerHTML=select_loation.options[select_loation.selectedIndex].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
DSC0002.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-119037-1-1.html 上篇帖子: Sharepoint 开发心得No.4: [转载]Feature.xml文件介绍 下篇帖子: sharepoint中eventhandler的ItemUpdated
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表