Friday, September 12, 2008

Use User Control in WebPart (manually deploy to Inetpub)

Talked with 黎明 (msn group's friend) last night about his new creative about moss webpart experiences. That is User Control usage in Web Part. I adviced him to write it down to something like blog. He told me he did never write that. For a technical deposit. I draft that here.
1. Create User Control.

Create a C# Web Application by VS 2008

Add new User Control as you like show and compile it. Snapshot show you how it looks like.



2. Add existing user control to a web part and deploy web part.


  1. Create a new web part by wss extension of vs 1.2

  2. Create a new folder "wpresources" in project. And add existing ascx file in this folder.



  3. Change web part CS file as below snappit.


  4. namespace UCPart{
    [Guid("c60b2566-0113-490a-9581-8ecbac96a81a")]


    public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
    {


    public WebPart1()
    {
    }


    UserControl userControl;


    protected override void CreateChildControls()
    {
    base.CreateChildControls();
    userControl =
    (UserControl)Page.LoadControl(@"/wpresources/WebUCOne.ascx");
    this.Controls.Add(userControl);
    }


    protected override void
    Render(HtmlTextWriter writer)
    {
    //base.Render(writer);
    userControl.RenderControl(writer);
    } }}



  5. Change AssemblyInfo.cs file to add "using System.Security;" to the using part and add below snippet to the last row.
    [assembly: AllowPartiallyTrustedCallers]

  6. Change C:\Inetpub\wwwroot\wss\VirtualDirectories\portal.mysharepoint80\Web.Config file. Add SafeControl and change Trust Level as below




  7. Change the output path of project properties to

    ..\..\..\..\..\Inetpub\wwwroot\wss\VirtualDirectories\portal.mysharepoint80\bin\

  8. Build the project and copy WebUCOne.ascx file to folder

    C:\Inetpub\wwwroot\wss\VirtualDirectories\portal.mysharepoint80\wpresources

  9. Run "iisreset -noforce" on command line

  10. Open [portal] -[site action]-[Gallery]-[Web Part Gallery]-[New], find the one you created and click [Populate Gallery].

  11. Add this web part to any page to show.


No comments: