Using ASP.Net user controls in your WSSV3/MOSS ASPX pages…


Following on from my post of application dev on SharePoint (here), I have had some people ask how to embed ASP.Net User Controls in the pages you can add to your site using Option 4 (see post for more details on what this option is).


Well … i did some digging/research and have this set of steps to achieve this.  Here goes:


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 (32)

  1. Tony says:

    How can you use code behind with User Controls used in this way?  Where does the DLL go?

  2. Thanks Chris this is excellent and no need for SmartPart.   I have recently been creating class based custom controls but the ASCX visual creation method might be handy sometimes.

  3. chjohn says:

    Hi Tony,

    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.

    Thanks,

    -Chris.

  4. Renaud Comte says:

    Perhaps using DelegateControl and a feature coul be also a good idea ?

  5. Ali says:

    Chris,

    this is cool. i’m not sure why, in general, this is so difficult to do with Sharepoint. One additional question i had was if the application that is being imported to sharepoint accesses a webservice, will porting the web.config settings to sharepoint be sufficient? will there be additional work that needs to be done?

  6. Nigel says:

    I am having problems when the usercontrol uses session variables.

    I have set enablesessionstate="true" on the pages directive in web.config and made sure I have a sessionstatemodule in the <httpModules> section but still get the error message "sessionstate can only be used when enableSessionState is set to true" when an seessionstate variable is accessed.

    Any ideas why greatfully recevied

    Thanks

    Nigel

  7. vdonkey says:

    Hi, I have a question.

    I used control in aspx, <%@ Register TagPrefix="uc1" TagName="UpFile" Src="../ucontrol/UpFile.ascx" %>

    but when different clients happened to access the same page, it seemed they are using the same control object somewhere in the server’s memory. when one client changed a variable in the control, it will effect the other one’s display. how to solve it?

    thanks in advance

  8. Everything seems to get off the ground just fine for me with the exception of adding the SafeControl to web.config.  Once this line is added to the SafeControls and IIS is reset I receive the following error:

    This page has encountered a critical error. Contact your system administrator if this problem persists.

    I have double and triple checked the process and haven’t found any deviations from your post.  I am using ascx/.cs files in my _controls Virtual Directory.  The only difference from the example is these are not precompiled and are calling on a service .asmx and App_Code/*.cs classes.  Could this be the issue?

    Thanks in advance,

    Will

  9. brwalias says:

    By adding:

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

    to the web.config it pumps out an error on my site:

    This page has encountered a critical error. Contact your system administrator if this problem persists.

    Any ideas?

  10. isel says:

    attributes names are case-sensitive, you should use:

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

  11. Until yesterday it has been quite a while since I blogged. Basically here is the list of things I have

  12. Until yesterday it has been quite a while since I blogged. Basically here is the list of things I have

  13. yuhang sun says:

    What about deploying a custom user control to _controltemplates directory? It is an out-of-box directory for holding asp.net user controls.

  14. Bhavin Gajjar says:

    Hi,

    I followed article and at last got error …

    The mce_src attribute on the register directive is not allowed in this page.

    Troubleshoot issues with Windows SharePoint Services.

    What to do with this error?

    Thanks in Adv. if  one can help me.

  15. Ian Kelly says:

    I have tried this but with the same problems that several have had.

    1.) mce_src attribute causes errors in my entire site

    2.) Where does one put the <uc2></uc2> control instance within the page where it will show up correctly?

    3.) When I add the control anywhere inside a default.aspx or any other page/page layout I get a render error saying "Can’t find ~/_controls….."

    Is anyone else having these same problems?

  16. smeyer says:

    I had the same issue with the mce_src attribute

    mce_src="~/_controls/*" in the safe controls in web config caused a critical error on all sites in the collection. Not sure why but would like to understand why if possible.

  17. Noé García says:

    In our sites, we had the critical error on all sites, but I have solved following the instructions from Chris Johnson’s blog. First you should add the next line to your web.config.

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

    and it’s necessary change the next line:

         <!–<trustLevel name="WSS_Minimal" policyFile="C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12configwss_minimaltrust.config" />

    to:

         <trustLevel name="WSS_Medium" policyFile="C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12configwss_mediumtrust.config" />

    Regards,

    Noé García (from Asturias (Spain-Europe)).

  18. Tom King says:

    I would just like to know how can we add sessions in user control

  19. Body: Fly Buys is New Zealand’s leading loyalty programme and is managed by Loyalty New Zealand. Fly

  20. CodeJedi.NET says:

    Chris Johnson has posted a great article on using ASP.Net ASCX user controls in MOSS/WSSv3. I have been

  21. bps says:

    Only an obsefrvation, i’ve found on my web site a visit reffered from this site, I don’t know how, but it’s like this, anyway I’ve found this code on this website:

    <img src="http://c.microsoft.com/trans_pixel.aspx?TYPE=PV&amp;r=http%3a%2f%2fnoslan.com%2fwp-admin%2fadmin.php%3fpage%3dwassup&quot; width="0" height="0" alt="Page view tracker" />

    </body>

    </html>

    Can someone explain me why?

    Thanks in advance!

  22. nato castro says:

    Great post, but if you put the mce_src, it doesn’t work.

    Nato

  23. veera says:

    Hi ,

    I have intranet site and internet site  on same webapplication and   using same content database .

    i can able to apply user controls to the pages in internet site    , user controls are displaying properly . when i try the same thing in intranet site . iam unable to use the user controls ?

    any ideas? Please let me know

    Veera

  24. Densio_phil says:

    Hi there, I have a question..

    How can I integrate a asp.net user control to a xslt functionality.

    To be more specific.

    I have a sharepoint page that uses xslt to return search results. In my xslt I want to place a user control that was made by VB.net. How can i place this user control(link) and point it to the vb code???

    Thanks in advance…

  25. Chinna says:

    Hi,

    Am getting control not found error.

    Does any one has got the same and found any work arrounds

  26. Oliver says:

    Hi,

    what can be difference between users controls in aspx pages and users controls in smartpart? I tried both but smart part is nicer because you can put a user control anywhere as long as the target page is web part enabled. I also let you guys how to use own class files etc.

    0. create a project and add class file for common use such as your common functions etc. if you have own custom control then create a cs file for it as well.

      DatePicker.cs (with name space Microsoft.DatePicker)

      Common.cs

    1. create web user control with name space.

    name space Microsoft.myOwnUserControl1Namespace

    {

     public partial class contols_ManageMicrosoft : System.Web.UI.UserControl

       {…………

     aspx page should read

    ‘Inherits="Microsoft.myOwnUserControl1Namespace.contols_ManageMicrosoft"

    if you have another user control,

    name space Microsoft.myOwnUserControl2Namespace

    {

     public partial class contols_ManageMicrosoftToilet : System.Web.UI.UserControl

       {…………

     aspx page should read

    ‘Inherits="Microsoft.myOwnUserControl2Namespace.contols_ManageMicrosoft"

    2. publish the project with updatable and srong name signed. (how to create a key .snk file please check sn.exe help)

    3. using ILMerge.exe to combine all dlls at the pre compiled bin folder into one dll (assembly). This will keep the key (strong name signing) as well.

    4. Move the new dll (myAssembly.dll) to GAC and find all necessary info via its property (right click and select property in the GAC)

    5. register into web.config in sharepoint

      Make sure each name space should be added to the config file.

         <SafeControl Assembly="myAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a0b30de848420999" Namespace="Microsoft.myOwnUserControl1Namespace" TypeName="*" Safe="True" />

         <SafeControl Assembly="myAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a0b30de848420999" Namespace="Microsoft.myOwnUserControl1Namespace" TypeName="*" Safe="True" />

         <SafeControl Assembly="myAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=a0b30de848420999" Namespace="Microsoft.DatePicker" TypeName="*" Safe="True" />

    6. With Smart part it should work straight.

    With custom aspx page, you need to add a line

    <%@ Register src="~/ManageMicrosoft.ascx" tagname="ManageMicrosoft" tagprefix="uc1" %>

    7. if system.data…. error message occurs, then you need to check the below link for fixing the problem.

    http://www.eggheadcafe.com/software/aspnet/30460745/request-for-the-permissio.aspx

    8. Good luck

  27. Oliver says:

    but if you want, you can make one assembly with one name space(name space=Microsoft, assembly=Microsoft(.dll)). Before you put the ascx file to sharepoint directory, you need to change the first line of your web user controls.

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ManageMicrosoftToilet.ascx.cs" Inherits="Microsoft.contols_ManageMicrosoftToilet, Microsoft" %> <!– the behind code will be found in assembly Microsoft and its name space is Microsoft. –>

    With these information you can do whatever with your sharepoint.You can put your user controls to sharepoint aspx page or smart part in the sharepoint.

    to be bear in mind, if you have another user controls inside your user control, the reference tag should include assemly="Microsoft" beside namespace="Microsoft" tag="cc1" and you can create a user control inside your page.

    <cc1:DatePicker id="myPicker" runat="server />

  28. Rahemath says:

    one more way to use usercontrols in MOSS ASPX pages

    http://www.a2zdotnet.com/View.aspx?id=95

Skip to main content