Friday, 27 April 2012

Weather Webpart


STEP 1:

Create EmptySharePoint project

STEP 2:

Create the simple webpart like weatherwebpart(Not Visualwebpart)



STEP 3:

Create one class file like (condition.cs) and paste the coding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeatherWebPart.WeatherWebPart
{
    class Conditions
    {
        string city = "No Data";
        string dayOfWeek = DateTime.Now.DayOfWeek.ToString();
        string condition = "No Data";
        string tempC = "No Data";
        string tempC1 = "No Data";
        string humidity = "No Data";
        string wind = "No Data";
        string high = "No Data";
        string low = "No Data";
        string icon = "No Data";

        public string TempC1
        {
            get { return tempC1; }
            set { tempC1 = value; }
        }
        public string City
        {
            get { return city; }
            set { city = value; }
        }
        public string DayOfWeek
        {
            get { return dayOfWeek; }
            set { dayOfWeek = value; }
        }

        public string Condition
        {
            get { return condition; }
            set { condition = value; }
        }

        public string TempC
        {
            get { return tempC; }
            set { tempC = value; }
        }
        public string Humidity
        {
            get { return humidity; }
            set { humidity = value; }
        }
        public string Wind
        {
            get { return wind; }
            set { wind = value; }
        }
        public string High
        {
            get { return high; }
            set { high = value; }
        }
        public string Low
        {
            get { return low; }
            set { low = value; }
        }
        public string Icon
        {
            get { return icon; }
            set { icon = value; }
        }
    }
}



STEP 4:

Select weatherwebpart.cs --> paste the coding

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Xml;
using System.Collections;
using System.Collections.Generic;

namespace WeatherWebPart.WeatherWebPart
{
    [ToolboxItemAttribute(false)]
    public class WeatherWebPart : WebPart
    {
        private String _location = null;
        [WebBrowsable(),
        Personalizable(),
        System.ComponentModel.Category("Configuration"),
        WebDisplayName("Location:")]
        public String Location
        {
            get { return _location; }
            set { _location = value; }
        }
        private XmlDocument xmlDoc = new XmlDocument();
        private Conditions currentCondition;
        private List<Conditions> forecastCondition;

        protected override void Render(HtmlTextWriter writer)
        {
            if (Location == null)
            {
                writer.Write("Please specify a location in the web part editor.");
                return;
            }

            xmlDoc.Load(string.Format("http://www.google.com/ig/api?weather={0}", Location));
            currentCondition = getCurrentConditions();
            forecastCondition = getForecastedConditions();

            if (currentCondition == null)
            {
                writer.Write("There was an error making an API call.");
                return;
            }

            writer.Write("<div class=\"lft-weatherheader\">{0}</div>", Location);

            writer.Write("<table class=\"lft-weathertable\">");

            if (currentCondition.Icon.Equals("")) { writer.Write("<tr><td><img src=\"/_layouts/images/BLANK.GIF\" />"); } else { writer.Write("<tr><td><img src="+currentCondition.Icon+" />"); }
       
         

            writer.Write("</td><td>Now ");
            writer.Write("{0}&deg;F</td></tr>", currentCondition.TempC);
            writer.Write("</td><td>Now ");
            writer.Write("{0}&deg;C</td></tr>", currentCondition.TempC1);
            writer.Write("</td><td>Now ");
            writer.Write("{0}</td></tr>", currentCondition.Wind);
            writer.Write("</td><td>Now ");
            writer.Write("{0}</td></tr>", currentCondition.Humidity);


            foreach (Conditions fc in forecastCondition)
            {

                writer.Write("<tr><td><img src=\"http://www.google.com{0}\" /></div>", fc.Icon);
             
             

               // writer.Write("</td><td>{0} ", fc.DayOfWeek);
               // writer.Write("{0}&deg;C", fc.High);
                //writer.Write(" | ");
                //writer.Write("{0}&deg;C</td></tr>", fc.Low);

            }
            writer.Write("</table>");

        }

        private Conditions getCurrentConditions()
        {
            Conditions currentConditions = new Conditions();

            if (xmlDoc.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
            {
                currentConditions = null;

            }
            else
            {
                currentConditions.City = xmlDoc.SelectSingleNode("/xml_api_reply/weather/forecast_information/city").Attributes["data"].InnerText;
                currentConditions.Condition = xmlDoc.SelectSingleNode("/xml_api_reply/weather/current_conditions/condition").Attributes["data"].InnerText;
                currentConditions.TempC1 =Convert.ToString(xmlDoc.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_c").Attributes["data"].InnerText);
                currentConditions.TempC =Convert.ToString(convertcelsiustofahrenheit(Convert.ToDouble(xmlDoc.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_c").Attributes["data"].InnerText)));
                currentConditions.Humidity = xmlDoc.SelectSingleNode("/xml_api_reply/weather/current_conditions/humidity").Attributes["data"].InnerText;
                currentConditions.Wind = xmlDoc.SelectSingleNode("/xml_api_reply/weather/current_conditions/wind_condition").Attributes["data"].InnerText;
                currentConditions.Icon = @"C:\Users\Administrator\Desktop\mostly_sunny.png"; //xmlDoc.SelectSingleNode("/xml_api_reply/weather/current_conditions/icon").Attributes["data"].InnerText;
            }

            return currentConditions;
        }

        private List<Conditions> getForecastedConditions()
        {
            List<Conditions> days = new List<Conditions>();

            if (xmlDoc.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
            {
                days = null;

            }
            else
            {
                XmlNodeList xnList = xmlDoc.SelectNodes("/xml_api_reply/weather/forecast_conditions");
                foreach (XmlNode xn in xnList)
                {
                    Conditions day = new Conditions();
                    //day.DayOfWeek = xn["day_of_week"].Attributes["data"].InnerText;
                   // day.Low = ConvertFahrenheitToCelsius(Double.Parse(xn["low"].Attributes["data"].InnerText)).ToString();
                    //day.High = ConvertFahrenheitToCelsius(Double.Parse(xn["high"].Attributes["data"].InnerText)).ToString();
                    //day.Icon = xn["icon"].Attributes["data"].InnerText;
                    //days.Add(day);
                }
            }

            return days;
        }

        public static int ConvertFahrenheitToCelsius(double f)
        {
            return Convert.ToInt32((5.0 / 9.0) * (f - 32));
         
        }

        public static int convertcelsiustofahrenheit(double d)
        {
            return Convert.ToInt32((d*9/5+32));
        }
    }
}

SharePoint online - Get List-item attachments and Display to div

Step 1 : Create a List ex: TestList and attach few images Step 2 : Copy and Pastet the below coding in App.js var  Items =  null ; ...