Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get data to display weather using HTML web resource

(0) ShareShare
ReportReport
Posted on by

Hi experts, 

I created an HTML web resource to display the weather for the entered city. My HTML has a text box and a button where the user can enter the city. 

However, I want my code to pick up the value of the city from address1_city attribute and display the information dynamically. How can I change my code to make it work? 

<!DOCTYPE html>
<html>
<head>
	<title>Weather API</title>
	<script 
		src="code.jquery.com/jquery-3.2.1.min.js"
		integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
		crossorigin="anonymous"></script>
	</head>
	<body>
		<input id="city"></input>
		<button id="getWeatherForecast">Get Weather</button>
		<div id="showWeatherForecast"></div>

		<script type="text/javascript">
			$(document).ready(function() {
				$("#getWeatherForecast").click(function(){
					var city = $("#city").val();
					var key = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';

					$.ajax({
						url: 'api.openweathermap.org/.../weather',
						dataType: 'json',
						type: 'GET',
						data: {q: city, appid: key, units: 'metric'},

						success: function(data){ 
							var wf = '';
							$.each(data.weather, function(index, val){
								wf += '<p><b>' + data.name + "<b><img src=openweathermap.org/.../w"+ val.icon + ".png></p>" +
								data.main.temp + '&deg;C' + ' | ' + val.main + ", " +
								val.description
							});
							$("#showWeatherForecast").html(wf);
						}
					})
				});
			});
		</script>
	</body>
	</html>


Thanks,

Jon

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: Get data to display weather using HTML web resource

    Hi all,

    I've got it to work now. Just had to tweak the image icon URL a little bit.

    Thank you so much again for helping me out! Really appreciate it.

    Best,

    Jon

  • Suggested answer
    Shaminderpal Singh Profile Picture
    1,565 on at
    RE: Get data to display weather using HTML web resource

    Hi Jon,

    Since the image is not hosted inside crm that's the reason it is not coming.

    Save the image to your local,add it as a web resource file

    and replace below highlighted url with your url

    <img src="openweathermap.org/.../w"+ val.icon + ".png>

    -Shaminder

  • Community Member Profile Picture
    on at
    RE: Get data to display weather using HTML web resource

    Thank you so much Shaminder & Goutam for helping me with this task.

    Just another thing I was wondering is would you know why the icon isnt displaying the image? In my simple html page it shows the icon:

    Screen-Shot-2018_2D00_04_2D00_30-at-6.16.19-PM.png

    Thanks,

    Jon

  • Verified answer
    Shaminderpal Singh Profile Picture
    1,565 on at
    RE: Get data to display weather using HTML web resource

    Hi Jon,

    Remove this line from code //Add a webresource with jquery code and give its reference here

    It was an indicator for you to replace the webresource name :)

    <script src="..//WebResources/{Specify the schema of jquery file which you created}" >

    use the one which is named as jquery1.9.min.js

    -Shaminder

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Get data to display weather using HTML web resource

    Hi Jon ,

    try to use Jquery_1.9.1.min

  • Community Member Profile Picture
    on at
    RE: Get data to display weather using HTML web resource

    Hi Shaminder, 

    I added the jqeury reference from the SDK as shown below but I'm still getting message as following:

    4201.Capture1.PNG

    4201.Capture1.PNG

    Did I use the correct jquery file? Or do I am have to do it some other way? I'm sorry I'm a beginner at the technical side of CRM.

    Thanks,

    Jon

  • Verified answer
    Shaminderpal Singh Profile Picture
    1,565 on at
    RE: Get data to display weather using HTML web resource

    Hi Jon

    Use the below one:

    <!DOCTYPE html>

    <html>

    <head>

       <title>Weather API</title>

       <script src="..//WebResources/new_jquery" ></script>//Add a webresource with jquery code and give its reference here

    </head>

    <body>

      <div id="showWeatherForecast"></div>

       <script type="text/javascript">

    $(function() {

                   var city = window.parent.Xrm.Page.getAttribute('address1_city').getValue();

                   console.log('city', city)

                   var key = '5c9c57526e87675e29e6dc4028ccb393';

    $.ajax({

                           url: 'https://api.openweathermap.org/data/2.5/weather',

    dataType: 'json',

    type: 'GET',

    data: {q: city, appid: key, units: 'metric'},

    success: function(data){

    var wf = '';

    $.each(data.weather, function(index, val){

    wf += '<p><b>' + data.name + "<b><img src=openweathermap.org/.../w"+ val.icon + ".png></p>" +

    data.main.temp + '°C' + ' | ' + val.main + ", " +

    val.description

    });

    $("#showWeatherForecast").html(wf);

                           },

                           error: function error(data) {

                               console.log(data);

                           }

    })

    });

       </script>

    </body>

    </html>

    I have tested it also,go the result as below.

    0574.addr.PNG

    Note-Make sure to add jquery reference by creating a new web resource

    -Shaminder

  • gdas Profile Picture
    50,091 Moderator on at
    RE: Get data to display weather using HTML web resource

    Hi Jon ,

    Are you getting any error ? please debug the code and let me know are you getting the city value or not . And in addition also share error if you are getting any.

  • Community Member Profile Picture
    on at
    RE: Get data to display weather using HTML web resource

    Hi Goutam,

    Thank you for your reply.

    I made the change and removed the input text box tag and button tag. But the web resource is blank when I load the page.

    <!DOCTYPE html>
    <html>
    <head>
    	<title>Weather API</title>
    	<script 
    		src="code.jquery.com/jquery-3.2.1.min.js"
    		integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
    		crossorigin="anonymous"></script>
    	</head>
    	<body>
    		
    		<div id="showWeatherForecast"></div>
    
    		<script type="text/javascript">
    			$(document).ready(function() {
    				$("#getWeatherForecast").click(function(){
    					var city = window.parent.Xrm.Page.getAttribute("address1_city").getValue();
    					var key = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
    
    					$.ajax({
    						url: 'api.openweathermap.org/.../weather',
    						dataType: 'json',
    						type: 'GET',
    						data: {q: city, appid: key, units: 'metric'},
    
    						success: function(data){ 
    							var wf = '';
    							$.each(data.weather, function(index, val){
    								wf += '<p><b>' + data.name + "<b><img src=openweathermap.org/.../w"+ val.icon + ".png></p>" +
    								data.main.temp + '&deg;C' + ' | ' + val.main + ", " +
    								val.description
    							});
    							$("#showWeatherForecast").html(wf);
    						}
    					})
    				});
    			});
    		</script>
    	</body>
    	</html>


  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Get data to display weather using HTML web resource

    Hi Jon ,

    Could you please try with this  - 

        var city = window.parent.Xrm.Page.getAttribute("address1_city ").getValue();

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans