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
Anonymous
March 27, 2007
How can you use code behind with User Controls used in this way? Where does the DLL go?Anonymous
March 27, 2007
PingBack from http://www.virtual-generations.com/2007/03/28/sharepoint-2007-link-dump-3/Anonymous
March 28, 2007
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.Anonymous
March 28, 2007
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.Anonymous
April 02, 2007
Perhaps using DelegateControl and a feature coul be also a good idea ?Anonymous
April 05, 2007
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?Anonymous
April 12, 2007
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 NigelAnonymous
May 06, 2007
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 advanceAnonymous
June 05, 2007
The comment has been removedAnonymous
June 06, 2007
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?Anonymous
June 18, 2007
attributes names are case-sensitive, you should use: <SafeControl Src="~/_controls/*" IncludeSubFolders="True" Safe="True" AllowRemoteDesigner="True" />Anonymous
July 19, 2007
Until yesterday it has been quite a while since I blogged. Basically here is the list of things I haveAnonymous
July 19, 2007
Until yesterday it has been quite a while since I blogged. Basically here is the list of things I haveAnonymous
August 16, 2007
The comment has been removedAnonymous
August 23, 2007
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.Anonymous
October 16, 2007
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?Anonymous
November 20, 2007
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.Anonymous
December 06, 2007
The comment has been removedAnonymous
December 26, 2007
I would just like to know how can we add sessions in user controlAnonymous
January 07, 2008
Body: Fly Buys is New Zealand’s leading loyalty programme and is managed by Loyalty New Zealand. FlyAnonymous
January 26, 2008
site http://claudiadonaghy.warp0.com/ domain page homeAnonymous
January 29, 2008
site http://cristinbickford.mystarship.com/ http domain websiteAnonymous
January 31, 2008
Chris Johnson has posted a great article on using ASP.Net ASCX user controls in MOSS/WSSv3. I have beenAnonymous
February 07, 2008
http www domain website http://rosariobearce.actorsite.com/ httpAnonymous
February 28, 2008
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&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!Anonymous
March 19, 2008
Great post, but if you put the mce_src, it doesn't work. NatoAnonymous
April 17, 2008
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 VeeraAnonymous
April 28, 2008
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...Anonymous
June 02, 2008
Hi, Am getting control not found error. Does any one has got the same and found any work arroundsAnonymous
September 16, 2008
The comment has been removedAnonymous
September 18, 2008
The comment has been removedAnonymous
February 04, 2009
one more way to use usercontrols in MOSS ASPX pages http://www.a2zdotnet.com/View.aspx?id=95