using System; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.WebPartPages; using System.DirectoryServices; using System.Net; using System.IO; using System.Configuration; using System.Xml; using System.Xml.Xsl; using System.Data; using System.Web; using System.Web.UI.WebControls; namespace Mullivan.SharePoint.WebParts { [Guid("78d71219-4044-4979-8aad-35f177b7b0b2")] public class WeatherWebPart : Microsoft.SharePoint.WebPartPages.WebPart { private const string DEFAULT_LOCALE = "USMO0787"; private const string DEFAULT_ZIP = "63131"; private const bool DEFAULT_AUTOLOCATE = true; private Table _tbl = null; private TableRow _trError = null; private TextBox _tboxSearch = null; private Button _btnSearch = null; public WeatherWebPart() : base() { } public bool AutoLocate { get { if (this.ViewState["AutoLocate"] != null) return (bool)this.ViewState["AutoLocate"]; else return DEFAULT_AUTOLOCATE; } set { this.ViewState["AutoLocate"] = value; } } public string Locale { get { if (this.ViewState["Local"] != null) return (string)this.ViewState["Local"]; else return DEFAULT_LOCALE; } set { this.ViewState["Local"] = value; } } public string Zip { get { if (this.ViewState["Zip"] != null) return (string)this.ViewState["Zip"]; else return DEFAULT_ZIP; } set { this.ViewState["Zip"] = value; } } protected override void CreateChildControls() { _tbl = new Table(); _tbl.Style[HtmlTextWriterStyle.MarginTop] = "10px"; _tbl.Style[HtmlTextWriterStyle.Width] = "width=100%"; TableRow tr = new TableRow(); _tbl.Rows.Add(tr); TableCell cell1 = new TableCell(); cell1.Text = "Enter Location "; tr.Cells.Add(cell1); TableCell cell2 = new TableCell(); tr.Cells.Add(cell2); TableCell cell3 = new TableCell(); tr.Cells.Add(cell3); _trError = new TableRow(); _trError.Visible = false; _tbl.Rows.Add(_trError); TableCell tcError = new TableCell(); tcError.ColumnSpan = 3; tcError.Style[HtmlTextWriterStyle.Color] = "red"; tcError.Style[HtmlTextWriterStyle.TextAlign] = "left"; tcError.Text = "Location could not be found."; _trError.Cells.Add(tcError); _tboxSearch = new TextBox(); _tboxSearch.ID = "tboxZip"; _tboxSearch.Width = 150; cell2.Controls.Add(_tboxSearch); _btnSearch = new Button(); _btnSearch.ID = "btnSearch"; _btnSearch.Text = "Search"; _btnSearch.Click += new EventHandler(_btnSearch_Click); cell3.Controls.Add(_btnSearch); this.Controls.Add(_tbl); base.CreateChildControls(); } protected override void OnLoad(EventArgs e) { EnsureChildControls(); if (!Page.IsPostBack && this.AutoLocate) SetUserLocation(); if(!Page.IsPostBack) this.Locale = GetLocaleByUserInfo(); base.OnLoad(e); _trError.Visible = false; } private void _btnSearch_Click(object sender, EventArgs e) { try { if(!string.IsNullOrEmpty(_tboxSearch.Text.Trim())) this.Locale = GetLocale(_tboxSearch.Text.Trim()); } catch { _trError.Visible = true; } } protected override void Render(HtmlTextWriter writer) { try { // creating a url which in responce sned rss feed in form of xml string reqUrl = string.Format("http://xoap.weather.com/weather/local/{0}?link=xoap&cc=*&dayf=7", this.Locale); // First we request our content from our provider source .. in this case .. The Weather Channel HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(reqUrl); wr.Timeout = 5000; //load the response into a response object WebResponse resp = wr.GetResponse(); // create a new stream that can be placed into an XmlTextReader using (Stream str = resp.GetResponseStream()) { using (StringReader srXsl = new StringReader(Properties.Resources.RSSWeather)) { using (XmlTextReader xrXsl = new XmlTextReader(srXsl)) { XslCompiledTransform cmplTrans = new XslCompiledTransform(); cmplTrans.Load(xrXsl); using (XmlTextReader xrXml = new XmlTextReader(str)) { XmlWriterSettings settings = new XmlWriterSettings(); settings.CheckCharacters = false; settings.ConformanceLevel = ConformanceLevel.Fragment; using(XmlWriter xwHtml = XmlWriter.Create(writer, settings)) { cmplTrans.Transform(xrXml, xwHtml); } } } } } base.Render(writer); } catch (Exception ex) { writer.Write(ex.Message); } } private bool SetUserLocation() { try { SPWeb web = SPContext.Current.Web; SPUser u = web.SiteUsers[web.CurrentUser.LoginName]; SPList userList = web.SiteUserInfoList; SPListItem uItem = userList.Items.GetItemById(u.ID); if (uItem != null) { string strZip = uItem.Properties["WorkZip"] as string; if (!string.IsNullOrEmpty(strZip)) this.Zip = strZip; return true; } } catch { this.Zip = DEFAULT_ZIP; } return false; } private string GetProperty(SearchResult searchResult, string PropertyName) { if(searchResult.Properties.Contains(PropertyName)) return searchResult.Properties[PropertyName][0].ToString() ; else return string.Empty; } private string GetLocaleByUserInfo() { try { string where = this.Zip; return GetLocale(where); } catch { return this.Locale; } } private string GetLocale(string where) { string strRequest = "http://xoap.weather.com/search/search?where=" + HttpUtility.UrlEncode(where); // First we request our content from our provider source .. in this case .. The Weather Channel HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(strRequest); wr.Timeout = 5000; //load the response into a response object WebResponse resp = wr.GetResponse(); // converting it into stream Stream str = resp.GetResponseStream(); // creating xml reader XmlTextReader XmlTextReader = new XmlTextReader(str); XmlTextReader.XmlResolver = null; XmlDocument doc = new XmlDocument(); // loading xml data in xml doc doc.Load(XmlTextReader); // searching location id return doc.DocumentElement.ParentNode.LastChild.FirstChild.Attributes[0].Value; } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)