Tips for Devs #1: Enumerate Farm Sites and Pages
23. June 2009 08:14

I've decided to do a little series on SharePoint Tips for Developers, which will focus on common tasks that aren't immediately obvious, hopefully saving people some time in their day to day work.

Our first tip is about enumerating the objects in your farm.  Specifically, taking an SPFarm object, finding all the web applications in the farm, all the sites, and all the pages within them.  Some portion of that enumeration is likely to come up quite often in your work.

First, Getting all Web Applications in a farm is probably one of the lesser known tasks:

SPWebService webService = farm.Services.GetValue<SPWebService>("");
foreach (SPWebApplication webApp in webService.WebApplications)
{
//Do Something with Web Application here
}

In the above example, the variable farm is an SPFarm object, which you can get in a variety of ways, depending where your code is running.  In a timer job you might just do "this.Farm" -- in a web part you can get the farm from the site object in SPContext.Current.


Now we can go through all the sites and webs.  Probably the simplest way to do this is to loop through the Sites collection of your web app, and call a recursive helper function on each web, which will enumerate through all child webs regardless of depth.

        foreach (SPSite site in webApp.Sites)
{
//Pass web to helper function to enumerate
GetWeb(site.RootWeb);

}
        private void GetWeb(SPWeb web)
{
//TODO: Do something with the web object

//Process the sub webs recursively
foreach (SPWeb subweb in web.Webs)
{
GetWeb(subweb);
}
}

Once you have the web, there are a variety of ways you might find the pages you are interested in.  For example, if it is a publishing site you might want to find all the Pages libraries and get only the published pages -- You could do this with the web object you have available in GetWeb(...):

        foreach (SPList list in web.Lists)
{
if(list.Title == "Pages")
{
foreach (SPListItem item in list.Items)
{
if (item.File.Exists &&
item.File.Url.EndsWith(".aspx") &&
item.Versions[0].Level == SPFileLevel.Published)
{
//Do something clever with the page which is item.File.Url
}
}
}

Note that above we check if the item is published by looking at the level attribute of the version.

Tags: , , , Comments (1) | Permalink
Virtual Earth Map in SharePoint Search Results
16. March 2009 09:15

I often integrate Virtual Earth / Live Maps into MOSS because It's a relatively easy way to add some interesting and useful interaction without any custom code.

I wanted to share with you the 'default' XSL template I typically start from for generating a Virtual Earth Map in my MOSS search results.  From here, I make whatever enhancements or customizations I want, which I'll describe more in a moment.

The prerequisites here are that you have managed columns with Latitude and Longitude that are coming back in your search results (which means you've updated the columns XML to include them on the settings of your search core results webpart)

Here's an example of the managed columns I've got, and will be using -- It's simply an addition of Lat and Lon, and utilization of the existing Title column.  On the right is my updated XML that goes in the columns property of the core results part:

image image

This data could come from a BDC connection to an external data source (e.g. SQL), or you could have a content type or list that has the Lat/Lon information in it.

(If you have a big list or table of data with addresses, and want to generate the coordinates for them, I recommend using this tool: http://www.batchgeocode.com/, You can export your list to excel, save as a delimited format, geocode, and re-import)

So once you have your columns available, this is the base XSL you can put in your core results part to generate a map, and put Push Pins with a popup for the Title at all the correct locations:

 

maptransform.xsl (2.34 kb)

 

The XSL does the following:

  1. Create a div for the map
  2. Includes the Virtual Earth javascript
  3. Write javascript for creating the map, creating a shape array, and pushing the CreateMap function into the body load for sharepoint (_spBodyOnLoadFunctionNames)
  4. Use the XSL to generate a bunch of calls to "Add Shape" based on the search results, to add in all the points.  You could expand this to add any HTML into the pushpin popup, such as content from the result item.

 

You will need to update the Map.LoadMap call to set the zoom level and coordinates to center on (currently its in Redmond, Washington, at a high zoom).

You can build on this using the ability to drag areas of the map to refine search results, or use the built in clustering ability to cluster large groups at different zoom levels.

Good luck!

Tags: , , , , Comments (2) | Permalink
"Tweet This" Link with TinyURL in ASP.Net
14. March 2009 10:59

In migrating to my new blog platform, one thing that I am really excited about is now being able to put custom code to power features I was not able to have in Google Blogger.

The first thing I did was add some code-behind to my user controls to generate good "Tweet This" links so that users could Tweet my posts on Twitter.

 

Most samples you see for this simple stick your page URL in the query string.  This doesn't often work, as the URL will be too long, as the user won't be able to tweet the message.

 

I create two functions behind my page, TweetThisLink and TinyUrl

 

    public string TweetThisLink(string Url, string Title)
{
return "http://twitter.com/home?status=@StefanGordon Reading "
              + TinyUrl(Url) + " " + Title;
}

public string TinyUrl(string Url)
{
try
{
if (!Url.ToLower().StartsWith("http") && !Url.ToLower().StartsWith("ftp"))
{
Url = "http://" + Url;
}
WebRequest request =
                  WebRequest.Create("http://tinyurl.com/api-create.php?url=" + Url);
WebResponse res = request.GetResponse();
string text;
using (StreamReader reader = new StreamReader(res.GetResponseStream()))
{
text = reader.ReadToEnd();
}
return text;
}
catch (Exception)
{
return Url;
}
}

From my ASPX page, I can now insert a nice "Tweet This" link as follows:

<a href="<%=TweetThisLink(Post.PermaLink.ToString(), Server.HtmlEncode(Post.Title)) %>">
Some image or text </a>
Tags: , , Comments (1) | Permalink
Windows Server 2008 MOSS Development Machine with Hyper-V - Part I
14. July 2008 15:48

This is the first, in a multi-part series on using Windows Server 2008 as your development O/S, and utilizing the recently released Hyper-V functionality to ease SharePoint 2007 development.  Here I'll cover getting the new Hyper-V running, and in the next part, I'll cover SharePoint installation, and how you can (hopefully) benefit from Server 2008.

There are numerous reasons to run Windows Server 2008 on your development PC.  Here were the selling points for me:

  • Take advantage of many of the new features of Vista, while still being able to install and run server applications.
  • Performance improvements over Vista due to the extremely modular installation.
  • Stability.
  • Hyper-V performance benefits over Virtual PC

First, Make sure you have supported hardware:

You must be on an x64 based machine, with a CPU that supports hardware virtualization.  Most of the Intel Core 2 Duo's seem to, as well as AMD's AMD-V line.

I'm utilizing a Lenovo Thinkpad T60p.

Most machines appear to ship with the CPU hardware virtualization support disabled in the BIOS.  On my Thinkpad I found the settings under Config->CPU.  Also, after you change this setting you must POWER OFF your machine and turn it back on.  Simply resetting the machine does not appear to let the CPU change modes.

How to Setup Hyper-V

Throughout this process, you'll be rebooting so often that you'll feel like you are running Windows 95.  I'll try to streamline things from my experience to save you some time.

From this point forward, assumes you have installed Windows Server 2008 x64 on your hardware, but not yet configured it.

Go download the RTM update to bring your Hyper-V to the final release.  (2008 shipped with a pre-release version)

http://www.microsoft.com/downloads/details.aspx?FamilyId=F3AB3D4B-63C8-4424-A738-BADED34D24ED&displaylang=en

Once the update is installed, you'll need to restart, and Windows will perform some reconfigurations at both shutdown and startup.

Once you are back, Server Manager should open, if not go ahead and launch it.

In Server Manager,  at the top level, under the Roles Summary you currently may not have much of anything.

 image

Go ahead and click “Add Roles” and choose Hyper-V.

image

Follow through the wizard.  The only important thing you'll need to choose is some information around network adapters.

The role is now added, but isn't enabled or available.  Go ahead and restart.

 

 

 

 

image

After some major reconfigurations, you'll be back, and you'll see Hyper-V in your Roles Summary.

image

If you've ensured your CPU virtualization support was enabled in the BIOS, you can now expand the Hyper-V manager from the Server Manager tree, and right click to “Connect To Server”

image 

Go ahead and connect to localhost.

image

Now when you click your server name, you'll get an actions menu on the right, and you can click “New” to create a new machine.

image

There is a very nice wizard for setting up your virtual machine.  (Alternatively, you could have attached a preexisting one).

Take note of the installation options section, where you can choose an ISO to install the OS from.  I installed Server 2008 Standard on my first virtual machine, as it is virtualization ‘aware', and performs very well.  I will later create one for Server 2003 as well, to test on multiple environments.

Make sure you get enough memory allocated!

 

image

Once you've created it you can go ahead and start it:

image

And There she is!

image

image

In the next part, I will discuss installing SharePoint on our Server 2008 virtualized machine, as well as the resulting development workflow.

Tags: , , , Comments (0) | Permalink