Webpart code to dynamically load a page viewer webpart to display a requested page

    1:  using System;
    2:  using System.Runtime.InteropServices;
    3:  using System.Web.UI;
    4:  using System.Web.UI.WebControls;
    5:  using System.Web.UI.WebControls.WebParts;
    6:  using System.Xml.Serialization;
    7:   
    8:  using Microsoft.SharePoint;
    9:  using Microsoft.SharePoint.WebControls;
   10:  using Microsoft.SharePoint.WebPartPages;
   11:  using System.Xml;
   12:  using System.ComponentModel;
   13:   
   14:  namespace WebPart1
   15:  {
   16:      [Guid("8517b24e-202c-4319-8960-303996a3fdb6")]
   17:      public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
   18:      {
   19:          public WebPart1()
   20:          {
   21:              this.ExportMode = WebPartExportMode.All;
   22:          }
   23:   
   24:          protected override void CreateChildControls()
   25:          {
   26:              base.CreateChildControls();
   27:   
   28:              // TODO: add custom rendering code here.
   29:              // Label label = new Label();
   30:              // label.Text = "Hello World";
   31:              // this.Controls.Add(label);
   32:          }
   33:   
   34:          protected override void Render(HtmlTextWriter writer)
   35:          {
   36:              SPWeb web = SPContext.GetContext(this.Context).Web;
   37:              web.AllowUnsafeUpdates = true;
   38:              SPUser user = web.CurrentUser;
   39:              SPWebPartCollection collection = web.GetWebPartCollection("default.aspx", Storage.Personal);
   40:              writer.Write(user.LoginName);
   41:              PageViewerWebPart lobjWebPArt = new PageViewerWebPart();
   42:              //Change to the JSP URL or your web Application
   43:              lobjWebPArt.ContentLink = "https://www.microsoft.com?SID=" + user.LoginName.ToString() + "&VIEW=domain";
   44:              collection.Add(lobjWebPArt);web.Dispose();
   45:          }
   46:      }
   47:  }

:: Please note that I would have just uploaded my initial code and you might want to consider proper optimization of the code and disposal of objects properly. I might not have updated the latest code here.

Comments

  • Anonymous
    February 02, 2012
    hi, Iam trying this, but what i found is that the content displayed trhorugh the webpart is not saving the session variables, so im having problems accesing the variables within the application that is showed throught the webpart. any idea? thanks!

  • Anonymous
    February 04, 2012
    before using sessions, you might need to enable session state in your sharepoint server first. please check on it.