Last Monday we held a "Next Level" seminar at Microsoft in Alpharetta. Danny has mentioned several downloads (part 1 and part 2). I wanted to go ahead and provide the Virtual Earth web part we demonstrated.
This was a simple (yet hopefully interesting) example of how you can create a web part. It does not demonstrate all aspects of web parts. For example, many web parts use ASP.NET server side controls. Typically you would instantiate those controls in CreateChildControls() and populate them with data from web services, list items, or databases within OnPreRender() and let the base classes handle rendering those child controls. To keep this example simple, we kept it down to two methods: OnLoad() and RenderContents() and we are simply rendering Javascript and HTML.
There are plenty of improvements that can be made, but in the interest of keeping it simple (and getting sleep), we kept it as small as possible. For a list of ideas, see the source code.
Here's what you need to do to get this to work:
- Download the source from here.
- Unzip the source and put it on a development SharePoint server.
- Open the solution and fix the reference to Microsoft.SharePoint.dll (typically this is found in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI)
-
Update the <SafeControls> section of web.config to contain the following:
<SafeControl Assembly="ThreeWill.SharePoint.NextLevel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=06b98477ebadad9a" Namespace="ThreeWill.SharePoint.NextLevel.UI.WebControls.WebParts" TypeName="*" Safe="True" />
The web.config file is typically found in C:\Inetpub\wwwroot\wss\VirtualDirectories\80.
- Compile the project then run the provided "_install.bat". This batch file simply puts the web part in the GAC and runs IISRESET. Web controls can alternatively be deployed to the web site bin directory.
-
Add the web part to the web part gallery.
- Go to the web part gallery by going to the top level site, then choosing Site Actions -> Site Settings, and under Galleries click Web Parts
- On the Web Part Gallery page click New.
- On the Web Part Gallery : New Web Parts page scroll down until you see ThreeWill.SharePoint.NextLevel.UI.WebControls.WebParts.VirtualEarthWebPart. Click its checkbox and then click the Populate Gallery button.
- Navigate to the page where you want an instance of your web part, edit the page and add the "VirtualEarthWebPart" (under the Miscellaneous section).
-
Create a list to contain pin locations.
- Within the same site that has your web part create a "Locations" list (name it whatever you like).
- Update the list schema by adding "Latititude", "Longitude", and "Description" fields (the exact field names matter).
- Add rows to the list. One way to find latitude and longitude values is to use http://terraserver.microsoft.com/.
- Edit your web part properties. Go to the miscellaneous section and at least set the list name to be the name of your list. The zoom level can be anywhere from 1 to 19.
Hopefully that does it for you. Granted, this could be packaged into a nice feature that did all of the above work for you. You should be able to add multiple instances of the web part on the same page and they could point to the same locations list or different lists.
I believe there are more sophisticated Virtual Earth web parts out there. The purpose of was just to show something fun and simple. We were able to draw on recent project experience where we created a more complicated Virtual Earth web part.
One final note… Microsoft Virtual Earth does have terms of use that you need to adhere to.
Enjoy!