SharePoint and getElementById()

I’ve just found out that using JavaScript’s getElementById() function doesn’t quite work as expected when dealing with controls on SharePoint pages. This is because SharePoint uses its own identifiers, so TextBox1 becomes something like ctl00$ctl00$g_3f6d90e4_335b_467c_a53f_6ae00bca6b63$ctl00$TextBox1.

Fortunately there’s a simple solution – instead of the following (which will cause an “Object required” JavaScript error):

document.getElementById("TextBox1");

you need to use this, which will insert the correct full ID for the element and thus work correctly:

document.getElementById("< %=TextBox1.ClientID%>");

It gets a bit more complicated when using nested controls, which is explained in this post by Eric Shupps.

FreeTextBox: How to fix “The type or namespace name ‘Toolbar’ could not be found”

I’m working on a Visual Studio 2005 ASP.NET project which makes use of FreeTextBox to provide rich text editing capability. My code sets up the toolbar programmatically but, despite following the installation instructions to the letter (including setting up the FTB TagPrefix), my code was failing to compile with The type or namespace name ‘Toolbar’ could not be found on the following line of code:

Toolbar ftbTools = new Toolbar();

I initially fixed this by adding the FreeTextBoxControls prefix:

FreeTextBoxControls.Toolbar ftbTools =
                             new FreeTextBoxControls.Toolbar();

But then my code fell over with a similar error on the very next line:

ftbTools.Items.Add(new ParagraphMenu());

Not wanting to have to add the FreeTextBoxControls prefix to this and my other 18 Add statements, I instead added the following using statement to my code file:

using FreeTextBoxControls;

Job done!

How to check whether a SharePoint user is in a particular group

Here’s a quick function I wrote to check whether a user is a member of a particular SharePoint group:

private bool IsMemberOf(string groupName)
{
    SPUser user = SPContext.Current.Web.CurrentUser;

    try
    {
        if (user.Groups[groupName] != null)
            return true;
        else
            return false;
    }
    catch
    {
        return false;
    }
}

The try-catch is required as – somewhat counter-intuitively – SharePoint seems to throw a “Group not found” error if the user is not a member of the group.

How to set SharePoint page title programmatically

I’ve spent some time today trying to figure out how to set the title of a SharePoint page from my own code. As blogger Michael Becker rightly points out, you can’t simply set Page.Title.

The correct solution, as provided by Michael, is illustrated in this example C# code:

// Get a reference to the appropriate Content Placeholder
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) 
                       Page.Master.FindControl("PlaceHolderPageTitle");

// Clear out anything that SharePoint might have put in it already
contentPlaceHolder.Controls.Clear();

// Put your content in
LiteralControl literalControl = new LiteralControl();
literalControl.Text = "Your text goes here";
contentPlaceHolder.Controls.Add(literalControl);

Happily this even works when you “cheat” by hosting an ASP.NET user control within a SmartPart, as opposed to creating a bona fide Web Part.

Standalone installer for Google Chrome 4

Google offer a standalone installer for the Windows build of Google Chrome, as opposed to the standard download which is actually just a small stub application that connects to Google’s servers to download and install the actual browser.

The offline installer is handy if you have a number of machines on which to install or update Chrome, but unfortunately Google haven’t updated it recently, so you end up with version 3.0.195.38 rather than the latest all-singing, all-dancing, extension-supporting version 4.0.249.89.

You can of course update to 4.0.249.89 from the About screen, but this defeats the purpose of using the standalone installer in the first place, and you may be unlucky enough to be on a corporate network which breaks the in-browser upgrade functionality.

By using Fiddler2 to monitor the activity of the stub installer, I was able to establish that it connects to the following google.com URL to download the latest build:

http://cache.pack.google.com/edgedl/chrome/install/249.89/chrome_installer.exe

This is your standalone/offline installer, which can be used to install new instances of Chrome or upgrade existing ones (in which case the installer will upgrade your browser silently).

Google release new builds fairly frequently, so keep an eye on the Google Chrome Releases blog and substitute 249.89 with the build number of the latest stable (or development, if you’re feeling brave) build, e.g. http://cache.pack.google.com/edgedl/chrome/install/322.2/chrome_installer.exe for version 5.0.322.2.

Ninite: download and install your favourite apps

Ninite is a free (for personal use) service which allows you to download and install several popular pieces of Windows software in bulk from a single installer – great when building a new PC or reinstalling an OS on an existing machine.

When you visit the site, you are presented with a list of well-known free and trial applications. Just select the ones you’re interested in, click Download and you’re presented with a small (~200K) stub installer which, when launched, downloads and installs each chosen application without user intervention.

I’ve just tried it on a new machine I’m setting up, and although it takes a while to download and install everything (I had selected quite a few apps), this part of the process is completely automated so you’re free to do something else while you wait.

Deploy ASP.NET web user controls on SharePoint using SmartPart

Today I’ve been playing with Return of SmartPart 1.3,  a shim which allows you to create SharePoint 2007 web parts using Web User Controls (ASCX files) created by Visual Studio/Visual Web Developer.

This is probably the least painful way for C#/VB.NET developers to delve into web part building without having to worry too much about the intricacies of the SharePoint platform.

Complete with setup wizard, comprehensive user guide and sample user controls, it’s a snap to get running, although you will need access to your SharePoint server both to install the SmartPart and any custom user controls. It also optionally supports AJAX controls, after installing Microsoft’s ASP.NET AJAX extensions.

There’s no need to use strong naming or even compile your controls – just drop the .ascx (and .ascx.cs/.vb, if necessary) files into your usercontrols directory and you’re good to go.