Step 1.
New a Web application, add a web user control(there will have ascx file and ascx.cs file). Strong named Signing. Install the Web application dll to GAC (C:\Windows\Assembly).
Step 2.
Place the ascx file and ascx.cs file to the path:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS
Step 3.
Copy the Web application dll to C:\Inetpub\wwwroot\wss\VirtualDirectories\port\bin
Modify the file:C:\Inetpub\wwwroot\wss\VirtualDirectories\port\web.config
Between < SafeControls> < /SafeControls> node, add:
< SafeControl Assembly="MyWebApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=******" Namespace="MyWebApplication" TypeName="Class" Safe="True" />
Step 4.
Create a sharepoint webpart project
New a Control, and load the control from our ascx file, the code is pretty much like:
[Guid("**********")]
public class ThisisADemodWebpart : System.Web.UI.WebControls.WebParts.WebPart
{
Control _myControl;
string err;
public ThisisADemodWebpart()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
try
{
_myControl.RenderControl(writer);
}
catch (Exception e)
{
writer.Write(e.Message + ":" + err);
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
this.Controls.Clear();
_myControl = this.Page.LoadControl("\\_layouts\\myWebUserControl.ascx");
this.Controls.Add(_myControl);
}
catch (Exception e)
{
err = e.Message;
}
}
}
One thing to notice, that in the web user control, if there is Sql Database access, we may meet the security access/permission exception below:
" at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.PermissionSet.Demand() at System.Data.Common.DbConnectionOptions.DemandPermission() at System.Data.SqlClient.SqlConnection.PermissionDemand() at ...."
We need to modify the file:
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG\wss_minimaltrust.config
Add:
< SecurityClass Name="SqlClientPermission"
Description="System.Data.SqlClient.SqlClientPermission, System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
to the SecurityClasses section and
< class="highlight1">IPermission class="SqlClientPermission" version="1"
Unrestricted="true" />
Notice: this post is for sharepoint2007 or earlier version. The reason using this way is that sharepoint webpart solution template is not able to add web user control. This post is the same as add use an ascx control from another web site. In sharepoint 2010, there is visual webpart project and we don't need to bother to do like this post any more.
1 comment:
Hi,Simplicity is the key to a good Web Design Cochin for your web page because a web page that is simple is clean, easy to navigate through, and convenient for the human eye.thanks........
Post a Comment