Creating a custom Weather Web Part in SharePoint 2010


Update :- The below solution doens’t work any more

http://www.tropicdesigns.net/blog/weather-com-suddenly-cancels-their-free-xml-data-feed-for-developers/

 

Recently we had a requirement to create a web part that will show the weather information based on the address of the user, which we were pulling from Microsoft Dynamics CRM’s database.

These following two links were of immense help

http://svintinner.blogspot.com/2007/02/how-to-use-weathercom-to-replace-msnbc.html

http://weatherwebpart.codeplex.com/

This is how the web part looks

Below is the sample source code for the web part

public class MyWeatherWebPart : WebPart
{
String UserLocation = "Ahmedabad"; // user city
String UserCountry = "India"; // user country
String RequestforURL = string.Empty; // URL which use to get xml data ( rss feed response ) from weather.com
System.Web.UI.WebControls.Xml xmlResults = new System.Web.UI.WebControls.Xml(); // this object contain final data we need to publish(write on sharepoint site)

protected override void Render(HtmlTextWriter writer)
{
try
{
String reqUrl = "<a href="http://xoap.weather.com/weather/local/">http://xoap.weather.com/weather/local/</a>" + GetLocaleID() + "?cc=*&dayf=5&link=xoap&prod=xoap&par=xxxxxxxx&key=xxxxxxxxxxxxxxx";
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(reqUrl);
//load the response into a response object
WebResponse resp = wr.GetResponse();
// create a new stream that can be placed into an XmlTextReader
Stream str = resp.GetResponseStream();
XmlTextReader reader = new XmlTextReader(str);
reader.XmlResolver = null;
// create a new Xml document and locading feed data in to it
XmlDocument doc = new XmlDocument();
doc.Load(reader);

// as xml style sheet is use for publishing data so we loading it into envornment
HttpContext context = HttpContext.Current; // getting refrence of current environment
// getting reference of path where sml style sheet stays
string path = context.Request.MapPath("/_layouts/RSSWeatherXSL.xsl");

// this object need to convert xml data into xml style sheet view
XslTransform trans = new XslTransform();

trans.Load(path); // loading xsl file
xmlResults.Document = doc; // loading xml data
xmlResults.Transform = trans; // transforming xml to xsl
xmlResults.DataBind(); // binding data
xmlResults.Visible = true; // setting property
xmlResults.RenderControl(writer); // rendering/publishing data as html

}
catch (Exception ex)
{
writer.Write(ex.Message);
}

}
/// <summary>
///   GetLocaleID() THIS METHOD USED TO GET LOCATION ID WHICH we NEED TO GET WEATHER RSS FEED
/// THIS METHOD USE USER CITY AND COUNTRY INFORMATION
/// </summary>
/// <returns></returns>
public string GetLocaleID()
{
try
{
RequestforURL = "<a href="http://xoap.weather.com/search/search?where">http://xoap.weather.com/search/search?where</a>=" + UserLocation.ToString() + "," + UserCountry.ToString();
// First we request our content from our provider source .. in this case .. The Weather Channel
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(RequestforURL);
//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;
}
catch(Exception ex)
{
throw ex; // any exception then default value i.e toronto, canada
}
}
}

Bye.

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

One thought on “Creating a custom Weather Web Part in SharePoint 2010”

Leave a Reply to OBAMA IS A NIGGER Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: