We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

In SharePoint, one can use PageViewer WebPart within a webpart page to display another web page within it. All that one is required to do is, provide the URL of the page as the ContentLinkparameter of the WebPart.

 However, users might often have requirements of resetting the ContentLink parameter at runtime i.e. they may need to pass a query string parameter dynamically to the referred page. PageViewer does not provide any direct approach to implement this but this doesn't imply that we cannot work around it! We can implement the described functionality by using Reflection.

This hack is illustrated in the example below using C#.NET:

 

 

Namespaces which need to be included-

using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
 
Code block
 
using (SPSite oSPsite = new SPSite("url"))
{
      oSPsite.AllowUnsafeUpdates = true;
 
      using (SPWeb oSPWeb = oSPsite.OpenWeb())
      {     oSPWeb.AllowUnsafeUpdates = true;
           
     SPWebPartCollection colWebParts = oSPWeb.GetWebPartCollection(
                                               Storage.Shared);                  
            "absolute url of the webpart page",
 
           
            foreach (WebPart webPart in colWebParts)
            {
                  if (webPart.GetType().ToString() == 
                              "Microsoft.Sharepoint.WebPartPages.PageViewerWebPart")
                  {
   PropertyInfo[] wptProperties = webPart.GetType().GetProperties(
                                  BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                       
                      foreach (PropertyInfo property in wptProperties)
     {
                       if (property.Name == "ContentLink")
                              {
                      //set new content link property and save changes
              property.SetValue(webPart, "new content link url", null);
                                    colWebParts.SaveChanges(webPart.StorageKey);
                                    break;
                              }
                        }
                        break;
                  }
            }
            oSPWeb.AllowUnsafeUpdates = false;
      }
      oSPsite.AllowUnsafeUpdates = false;
}
 
 


Related Tags:

SharePoint

Author: Kaushik Mohanty

top

Related Pages

  • SharePoint Programming Expertise
  • SharePoint Application Projects

SharePoint

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login