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.

Advertisements

10 Responses to Using ASP.Net user controls ASCX in MOSS/WSSv3

  1. Habib says:

    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. Code Jedi says:

    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. Dan Marth says:

    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. siju says:

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

  5. Code Jedi says:

    Siju,

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

    You can put your code behind DLL in the C:\Inetpub\wwwroot\wss\VirtualDirectories\moss.litwareinc.com80\BIN 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. Frucomerci says:

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

  7. Kapildev says:

    Great post working fine.
    I am configuring search and when doing add mappinh i am not getting the mapped property of my application. I have done full crawlled so many times but not getting it. any one knows it?
    Thanks…

  8. jaloplo says:

    Hi,
    And, How to use a resource string in your ascx file???
    I can’t reach how to do this.

  9. Imran Yousuf says:

    Great post, I was looking for this info…

  10. Mark says:

    Was there a reason why you chose to put your user control within the virtual directory off the site instead of using the CONTROLTEMPLATES folder in the 12 Hive? are there any benifits to doing it this way?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: