Using ASP.Net user controls ASCX in MOSS/WSSv3


Chris Johnson has posted a great article on using ASP.Net ASCX user controls in MOSS/WSSv3. I have been recently creating custom controls in Visual Studio (i.e. Control Classes not ASCX). To use these I add the controls to the GAC and then add an entry to the web.config to make it a safe control. The benefit of the ASCX model is that you can develop the user control in a more visual manner.

The ASCX controls can be used in MOSS/WSSv3 via the following method:

1. Create a Directory called "usercontrols" in the root of your sharepoint web site on the file system E.g. C:\Inetpub\wwwroot\wss\VirtualDirectories\moss.litwareinc.com80\UserControls

2. Open IIS manager and in the root of your SharePoint site create a VirtualDirectory called "_controls" and point it to that newly created directory.

3. Put your user control in that newly created directory on the filesystem

4. Open the web.config file and add the following:

<SafeControl src="~/_controls/*" mce_src="~/_controls/*" IncludeSubFolders="True" Safe="True" AllowRemoteDesigner="True" /">

5. In your ASPX page add the following:

<%@ Register src="~/_controls/SomeControl.ascx" mce_src="~/_controls/SomeControl.ascx" TagName="somecontrol" TagPrefix="uc2" %">

And...

<uc2:somecontrol id="mycontrol1" runat="server"></uc2:somecontrol>

6. Run your ASPX page and your control should render correctly.

Comments (6)

  1. williamcornwill says:

    Habib Says:

    April 25, 2007 at 3:01 am

    Hi,

    Great post!!! I have done what you have listed in order to use custom controls and it works fine. But I am having an issue when I enable anonymous access and the anon user tries to access the site that uses the custom controls. I either get an “unexpected error occurred” error or the site prompts the user to login. Any idea why this might be happening? Do I need to setup special permission for the “UserControls” folder? Thanks.

  2. williamcornwill says:

    Code Jedi Says:

    April 25, 2007 at 8:56 am

    I have noticed this behaviour in some circumstances. I have several basic user controls such as AdRotator and DublinCoreMetaDataControl which work with no problem, but I also developed a more complex control that iterates all SPSites in the Root Site, this one prompts for user credentials when in anonymous user mode. I haven’t really looked into it yet.

  3. williamcornwill says:

    Dan Marth Says:

    June 6, 2007 at 2:22 am

    Do you have a dll file along with your ascx file? I tried the step above except I have a dll file with code behind for the page, I tried just placing it in the bin folder but it just keeps giving errors.

    Any ideas?

    Thanks,

    Dan

  4. williamcornwill says:

    siju Says:

    June 26, 2007 at 7:39 am

    What would I do if I have thridparty dlls being used by my custom controls? Thanks,

  5. williamcornwill says:

    Code Jedi Says:

    June 26, 2007 at 9:41 am

    Siju,

    Chris Johnson placed a comment on his blog post in relation to this:

    You can put your code behind DLL in the C:InetpubwwwrootwssVirtualDirectoriesmoss.litwareinc.com80BIN directory and SharePoint will find them.

    You will need to add an entry to the SafeControls list in web.config for your types in the DLL.

  6. williamcornwill says:

    Frucomerci Says:

    July 27, 2007 at 12:22 am

    Hello, good post, was helpful for me!! A bit short but enough! An attached dll would be great

Skip to main content