Thursday, December 11, 2008

Site Navigator Released

Ensynch has just released a new SharePoint WebPart that will display a tree view of all of the sites in your SharePoint environment. Props to Joe Zamora and Jerry Camel for all of their development efforts, designing and creating this WebPart.


There are two versions of the Site Navigator, the Lite Version and the Professional Version. The Lite Version, is available at no cost and will allow you to select a starting point for the tree and additionally will allow you to "trim the tree" based on user permissions. The Professional version, which is available for a small fee, adds some additional functionality to set the automatic expansion depth, show site collections only, and adds an option to select which web applications to show instead of setting a URL starting point.


If you are interested in either the Lite or Pro version, simply drop a line to SharePointSolutions@Ensynch.com and we will get you hooked up. And in case you just want to know how we did it, here are some of the details, with a few minor changes to simplify the illustration...

After you create a Class Library project, make sure you add a reference to the Microsoft.SharePoint namespace and have all of the following using commands:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Administration;
Also, you will need to add two classes to your .cs file. One of these classes will be used to build and render the WebPart Tree and the other will be implementing the EditorPart class to build the property modification pane. There are a couple of methods that must be implemented in both classes:
public class SPSiteCollectionNav : System.Web.UI.WebControls.WebParts.WebPart, IWebEditable
{
     
     public SPSiteCollectionNav()
     {
     
     }
     
     //
     //IWebEditable Members
     //
     EditorPartCollection IWebEditable.CreateEditorParts()
     {
     
           List editors = new List();
           return new EditorPartCollection(editors);
     
     }
     
     object IWebEditable.WebBrowsableObject
     {
     
           get { return this; }
     
     }
     
     //
     //WebPart override methods
     //
     protected override void CreateChildControls()
     {
     
           base.CreateChildControls();
           this.ChildControlsCreated = true;
     
     }
     
     protected override void Render(HtmlTextWriter writer)
     {
     
           RenderChildren(writer);
     
     }
     
}

public class TreeViewEditor : EditorPart, ICallbackEventHandler
{
     
     public TreeViewEditor()
     {
     
     }
     
     //
     //EditorPart override methods
     //
     protected override void CreateChildControls()
     {
     
     }
     
     public override bool ApplyChanges()
     {
     
           EnsureChildControls();
     
           return true;
     
     }
     
     public override void SyncChanges()
     {
     
           EnsureChildControls();
     
     }
     
     //
     //ICallbackEventHandler Members
     //
     private string _argument = string.Empty;
     
     public void RaiseCallbackEvent(string eventArgument)
     {
     
           //store the argument in a local variable so we can use it in the "GetCallbackResult" method
           //later on if necessary.
           _argument = eventArgument;
     
     }
     
     public string GetCallbackResult()
     {
     
           return string.Empty;
     
     }
     
}
We are going to discuss building the SPSiteCollectionNav class first. Now that we have implemented all of the necessary functions, we can begin customizing our code. We are going to use the CreateChildControls() method to initiate the creation our tree and add it to the WebPart Controls Collection. We are also going to add a new property that we can set with our EditorPart to dynamically change properties of our tree display. Here is enough to get you started.
private TreeView tree;

public SPSiteCollectionNav()
{
     tree = new TreeView();
}

//lets add a property to turn on and off a create date tag
//appended to the tree node text, for EditorPart demonstration
private bool showCreateDate;

[WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
public bool ShowCreateDate
{
     get { return showCreateDate; }
     set { showCreateDate = value; }
}

EditorPartCollection IWebEditable.CreateEditorParts()
{
     List editors = new List();
     
     //add our EditorPart to the editors collection
     TreeViewEditor editor = new TreeViewEditor();
     editors.Add(editor);
     
     return new EditorPartCollection(editors);
}

protected override void CreateChildControls()
{
     
     base.CreateChildControls();
     Controls.Clear();
     
     //Elevate our privs so that we can get access to the Server Farm
     SPSecurity.CodeToRunElevated myCodeToRun = new SPSecurity.CodeToRunElevated(PopulateTreeControl);
     SPSecurity.RunWithElevatedPrivileges(myCodeToRun);
     
     //Create the tree and add it to the page
     Controls.Add(tree);
     
     this.ChildControlsCreated = true;
     
}

private void PopulateTreeControl()
{
     
     //clear out the tree so we can start fresh
     tree.Nodes.Clear();
     
     //get the servers in the local farm
     SPFarm farm = SPFarm.Local;
     SPWebService service = farm.Services.GetValue("");
     
     //enumerate the servers and add them to the tree
     foreach (SPWebApplication webApp in service.WebApplications)
     {
           TreeNode webAppNode = new TreeNode();
           webAppNode.Text = webApp.Name;
     
           if (showCreateDate)
           {
                 webAppNode.Text += " Created On: " + webApp.Created.ToString();
           }
     
           webAppNode.NavigateUrl = null;
           tree.Nodes.Add(webAppNode);
     
           //TODO: add code to enumerate the sites under
           //the web app and add them to the tree
     }
     
     
}
Next, we will work on adding some customizable properties to our WebPart. This is done with the EditorPart.
private CheckBox showCreateDateCheckBox;

public TreeViewEditor()
{

     //instantiate our create date checkbox
     showCreateDateCheckBox = new CheckBox();

}

protected override void CreateChildControls()
{
     
     //add the create date checkbox to our properties pane
     showCreateDateCheckBox.Text = "  Show Create Date with Site Title";
     Controls.Add(showCreateDateCheckBox);
     Controls.Add(new LiteralControl("<div style='width:100%' class='UserDottedLine'></div>"));

}
     
public override bool ApplyChanges()
{
     
     EnsureChildControls();
     
     SPSiteCollectionNav part = WebPartToEdit as SPSiteCollectionNav;
     
     if (part != null)
     {
           //save the checkbox value back to the WebPart
           part.ShowCreateDate = showCreateDateCheckBox.Checked;
     }
     else
     {
           return false;
     }
     
     return true;
     
}
     
public override void SyncChanges()
{
     
     EnsureChildControls();
     
     SPSiteCollectionNav part = WebPartToEdit as SPSiteCollectionNav;
     
     if (part != null)
     {
           //sync any changes to the web part back to its properties
           showCreateDateCheckBox.Checked = part.ShowCreateDate;
     }
     
}
And there you have it. The Create Date checkbox will appear in the properties pane of the WebPart and can be used to turn on and off the display of the Site Create Date in the Site Node text of our Navigation WebPart.

No comments: