Using Features to enable Drop-Down Menus in Team Sites

I recently was trying to enable the top nav bar to be a drop down menu of all of the sub sites. I found this article http://sharingpoint.blogspot.com/2007/02/wss-v3-drop-down-menus-in-team-sites.html which told me everything I needed to know. Instead of modifying the default.master and the TopNavBar.ascx (for the application.master) to change the SPNavigationProvider to SPSiteMapProvider, I decided I wanted each teamsite to have the option and I’m reluctant from changing SharePoint system files (see http://www.graphicalwonder.com/?p=532), so I put this part into a feature. Here’s my code below:

The first file is Feature.xml

<Feature  Id="22b94164-5348-11dc-8314-0800200c9a66"
          Title="All Sites in Site Collection Navigation"
          Description="Enable all sites in the site collection top navigation bar."
          Version="12.0.0.0"
          Scope="Web"
          Hidden="false"
    DefaultResourceFile="core"
    xmlns="http://schemas.microsoft.com/sharepoint/">
    <ElementManifests>
        <ElementManifest Location="NavigationSiteSettings.xml"/>
    </ElementManifests>
</Feature>

The second file is NavigationSiteSettings.xml

<Elements
    xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- Top Nav -->
 <Control 
        Sequence="25"
        Id="TopNavigationDataSource"
  ControlClass="System.Web.UI.WebControls.SiteMapDataSource"
  ControlAssembly="System.Web, version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
        <Property Name="ID">topSiteMap</Property>        
        <Property Name="SiteMapProvider">SPSiteMapProvider</Property>
        <Property Name="ShowStartingNode">true</Property>
    </Control>
    <HideCustomAction
        Id="TopNav"
        HideActionId="TopNav"
        GroupId="Customization"
        Location="Microsoft.SharePoint.SiteSettings" />
</Elements>

I still needed to modify the default.master and TopNavBar.ascx (you don’t need to edit the TopNavBar.ascx if you are implementing my Using your SharePoint site Master Page on all application pages) so that more than one level is shown. I haven’t figured out a way around that part yet. Does anyone else know a way to override the SharePoint.AspMenu control?

————————————————————————————————————
Update (09/21/07): I have since found out that apparently the SPSiteMapProvider relies on the user having permissions to view the top-level site in the site collection. If the user does not have access to this top-level site, then they will be prompted for authentication and won’t be able to access the site. Using Reflector I was able to find the code where they are finding the top-level site without checking for permissions first.

I ended up having to write my own SiteMapProvider that did the correct security checking and trimming while still allowing access to the site.

On a side note, while I was looking into this issue, I also noticed that the Global Breadcrumbs (which also uses the SPSiteMapProvider) doesn’t do security trimming. So I wrote a SiteMapProvider for the Global Breadcrumbs as well that did security trimming. There doesn’t seem to be a delegate control for the Global Breadcrumbs like there is for the top nav so I couldn’t write a feature to override the default. So I decided to just change my default.master and in combination with Using your SharePoint site Master Page on all application pages will use my custom SiteMapProvider for the Global Breadcrumbs.

————————————————————————————————————
Update (05/19/08): Some people have asked for the source code of my custom site map providers. You can download my latest files below:

AllSitesInSiteCollectionNavTrimmed.cs

AllSitesBreadcrumbsTrimmed.cs

FYI, this may not be the most efficient code and I have noticed some caching issues with it sometimes. Other than that it seems to work great.

Hosting Multiple unique SharePoint Sites on a single server

Recently we upgraded to a new, faster server for our websites. Ever since WSS v3 was released to the web I’ve been running SharePoint on my home server. But now with the new server hosted in Texas, I decided to move our SharePoint site to this server.

There are a few differences between my home server and our new web server. My home server is also my domain controller but I didn’t want to setup a domain on the new web server. The home server really only needs to service our needs, but our web server needs to be able to service the needs of myself and others. Because of these two differences, I needed a way to run multiple sharepoint sites that were totally independent of each other including users. This is normally done in SharePoint using Active Directory Account Create Mode, but like I said, I didn’t want to run a domain. A new feature with WSS V3 is the ability to have forms based authentication with ASP.NET’s membership providers. This was what I needed to do.

I first found this blog entry http://weblog.vb-tech.com/nick/archive/2006/06/14/1617.aspx?harrison on how to setup forms based authentication using a SQL Server membership provider. I was able follow his directions and set this up but there were a few issues. One, these directions assume that you will only be having one membership provider per sharepoint server, which was not the case for me. I needed to have a different membership provider for each sharepoint site on this one server. Second, I had no good way of adding, editing, and deleting users.

To fix the first issue involved doing a little copy and paste action out of the machine.config file into my web.config for the SharePoint site. I needed to change the name of the membership provider and tell this SharePoint site to use my newly named membership provider as the default provider. The reason I have to have a unique name for each SharePoint site is because I need to add every membership provider for all of my sites to the central administrations web.config. My SharePoint site’s web.config membership code looks like the following:

<membership defaultProvider="TheLineberrys_Users">
	<providers>
		<add name="TheLineberrys_Users" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="TheLineberrys" requiresUniqueEmail="true" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
	</providers>
</membership>
<profile enabled="true" defaultProvider="TheLineberrys_Profiles">
	<providers>
		<add name="TheLineberrys_Profiles" connectionStringName="LocalSqlServer" applicationName="TheLineberrys" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
	</providers>
</profile>
<roleManager enabled="true" defaultProvider="TheLineberrys_Roles">
	<providers>
		<add name="TheLineberrys_Roles" connectionStringName="LocalSqlServer" applicationName="TheLineberrys" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
	</providers>
</roleManager>

Notice that my applicationName is the unique name of my website. This allowed me to have one membership database for all of the SharePoint sites but also be able to have completely seperate users, profiles, and roles.

To fix the second issue I found this blog http://weblogs.asp.net/scottgu/archive/2005/10/18/427754.aspx with sample code. This was good enough to start out with but didn’t provide all of the features I needed. I added this code as a virtual directory under my sharepoint website called profile. I next found http://www.qualitydata.com/products/aspnet-membership/default.aspx which provided a membership manager control that seemed to fit my needs perfectly. I had a few issues getting it working in my profile virtual directory because of SharePoint’s trust level. Eventually I found out that I needed to change the web.config of this virtual directory to full trust to fix my issues. The problem with the Membership Manager is it didn’t allow end users to manage their account, it was more of an administrator tool. So I decided to use a combination of the Profile Sample from Scott Guthrie and the Membership Manager. The Membership Manager will be my profile tools for administrators only, and from the Profile Sample I will use the changepassword and recoverpassword pages for the end users to do password management, and I will also use the createnewwizard as sort of a setup script when I first create a new SharePoint site.

After getting all of this working I needed to make it look pretty. I created my own SharePoint stripped master page based on C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\simple.master. Next I setup the colors for the Membership Manager to use SharePoint css classes and also made a profile landing page that based on your permissions and whether or not you are logged in shows the appropriate links.

But this wasn’t quite good enough. The end users would have to remember to go to /profile/ to change their password. This was unacceptable.

I decided to modify some of the SharePoint files. The first file I modified was C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\CONTROLTEMPLATES\Welcome.ascx which is the file that controls the menu items in the welcome menu at the top right of the page when you are logged in. I added the following right above the logout item:

<SharePoint:MenuItemTemplate runat="server" id="ID_ChangePassword"
Text="Change Password"
Description="Change my password used to login"
MenuGroupId="200"
Sequence="250"
UseShortId="true"
ClientOnClickNavigateUrl="/profile/changepassword.aspx"
/>

This now added a Change Password link on the welcome menu of every page for every SharePoint site on the server.

I also changed
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\login.aspx which is the file people see when they are trying to login. I added the following right above the last </asp:Content>:

<p>
<a href="/profile/recoverpassword.aspx">Forgot your password?</a>

So now I have a change password link on every page when someone is logged in and a forgot your password link on the login page.

Another issue I ran into using forms authentication is the search seemed to not be working. I then found this page http://wsssearch.com/formauthentication.html that explains how you have to add another web application that is mapped to your forms authenticated web application so the search crawler can use windows authentication to crawl the website. After following the directions on that page my search was up and going.

Viewing SharePoint Webparts in Frontpage

I’ve recently been developing a lot of web parts for SharePoint at work. According to the Microsoft best practices for web part development they suggest you implement the IDesignTimeHtmlProvider interface. This means you need to implement a function called GetDesignTimeHtml(). This allows you to define the html output to display when editing a sharepoint page in frontpage editor. A lot of web parts I have downloaded on the web don’t seem to implement this because they show up in frontpage saying “The Preview for this Web Part is not available”. I also had a hard time implementing this since the GetDesignTimeHtml expects a string of html and when using webparts you are rendering user controls lots of times. I finally found an easy way to render almost any webpart in frontpage.

 

public virtual string GetDesignTimeHtml()
{
	StringWriter sw = new StringWriter();
	HtmlTextWriter tw = new HtmlTextWriter(sw);
	try
	{
		CreateChildControls();
		RenderWebPart(tw);
	}
	catch(Exception e)
	{
		sw.Write(e.Message + e.StackTrace);
	}
	return sw.ToString();
}

 

Basically in this function you will need to call any other functions/events that normally get called in the webpart execution (such as CreateChildControls). Then using the htmltextwriter and string writer you can output the html that would be rendered. I included the try catch so I can see the errors that are displayed when trying to render a webpart in frontpage. When in production you should probably remove the try catch so the end user sees a more friendly error message.

Something I did notice was frontpage seems to run the page in a different context than the page is normally rendered. This means that when I was trying to access Page.User.Identity.Name I got an error in frontpage. So I had to use the SPControl.GetContextWeb(Context).CurrentUser.LoginName instead. I also noticed that because frontpage loads it in a different context that when loading an Itemplate from an external ascx file, I got the error saying “The virtual path” and then whatever the path is to my ascx file “maps to another application, which is not allowed”.