Welcome to MSDN Blogs Sign in | Join | Help

Chris Johnson

All about Chris Johnson, SharePoint Products & Technologies & Other Stuff.

News

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.

Posted: Wednesday, March 28, 2007 1:02 PM by chjohn
Filed under: , ,

Comments

Tony said:

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

# March 28, 2007 4:24 AM

William Cornwill said:

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.

# March 28, 2007 8:04 AM

chjohn said:

Hi Tony,

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.

Thanks,

-Chris.

# March 28, 2007 5:59 PM

Renaud Comte said:

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

# April 2, 2007 8:35 AM

Ali said:

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?

# April 5, 2007 2:17 PM

Nigel said:

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

# April 12, 2007 1:33 PM

vdonkey said:

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

# May 7, 2007 1:34 AM

Will Pritchard said:

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

# June 5, 2007 4:21 PM

brwalias said:

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?

# June 6, 2007 5:38 PM

isel said:

attributes names are case-sensitive, you should use:

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

# June 18, 2007 10:33 AM

Chris Johnson said:

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

# July 19, 2007 2:02 PM

Noticias externas said:

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

# July 19, 2007 2:07 PM

yuhang sun said:

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

# August 16, 2007 9:26 PM

Bhavin Gajjar said:

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.

# August 23, 2007 9:59 AM

Ian Kelly said:

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?

# October 16, 2007 1:27 PM

smeyer said:

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.

# November 20, 2007 1:01 PM

Noé García said:

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 Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\wss_minimaltrust.config" />

to:

     <trustLevel name="WSS_Medium" policyFile="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\config\wss_mediumtrust.config" />

Regards,

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

# December 7, 2007 4:35 AM

Tom King said:

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

# December 27, 2007 12:28 AM

Mirrored Blogs said:

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

# January 7, 2008 5:54 PM

site said:

site http://claudiadonaghy.warp0.com/ domain page home

# January 27, 2008 7:59 AM

site said:

site http://cristinbickford.mystarship.com/ http domain website

# January 29, 2008 9:36 PM

CodeJedi.NET said:

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

# January 31, 2008 8:00 PM

http said:

http www domain website http://rosariobearce.actorsite.com/ http

# February 8, 2008 1:12 AM

bps said:

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" width="0" height="0" alt="Page view tracker" />

</body>

</html>

Can someone explain me why?

Thanks in advance!

# February 28, 2008 12:17 PM

nato castro said:

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

Nato

# March 19, 2008 3:32 PM

veera said:

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

# April 17, 2008 8:47 AM

Densio_phil said:

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...

# April 29, 2008 6:43 AM

Chinna said:

Hi,

Am getting control not found error.

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

# June 2, 2008 4:27 PM

Oliver said:

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

# September 17, 2008 12:24 AM

Oliver said:

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 />

# September 19, 2008 2:07 AM

Rahemath said:

one more way to use usercontrols in MOSS ASPX pages

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

# February 5, 2009 4:21 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker