QueryStringPageViewer WebPart for Sharepoint

    /// <summary>
    /// Mads Haugbø Nissen 2003(c)
    /// Objectware AS
    /// Norway
    /// 
    /// http://weblogs.asp.net/mnissen
    /// 
    /// The QueryStringPageViewer provides the same functionality as the regular PageViewerWebPart
    /// but allows providing a selection of querystring parameters from the page that hosts this webpart
    /// to the page viewed in the QueryStringPageViewer.
    /// </summary>
    [DefaultProperty("ContentLink"),
    ToolboxData("<{0}:QueryStringPageViewer runat=server></{0}:QueryStringPageViewer>"),
    XmlRoot(Namespace="http://www.objectware.no/Lawyer/Sharepoint/Webparts/QueryStringPageViewer")]
    public class QueryStringPageViewer : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private string _contentLink = "";
        [Browsable(true)]
        [Category("Content")]
        [DefaultValue("")]
        [WebPartStorage(Storage.Shared)]
        [FriendlyName("The url of the content to show.")]
        [Description("Supply Querystring params to be passed like this: Page.aspx?Param1&Param2&Param3")]
        public string ContentLink
        {
            get
            {
                return this._contentLink;
            }
            set
            {
                this._contentLink = value;
            }
        }
        
        protected string BaseUrl
        {
            get
            {
                int endIndex = this.ContentLink.IndexOf("?");
                if(endIndex != -1)
                    return this.ContentLink.Substring(0, endIndex + 1);
                else
                    return "";
            }
        }
        /// <summary>
        /// Render this Web Part to the output parameter specified.
        /// </summary>
        /// <param name="output"> The HTML writer to write out to </param>
        protected override void RenderWebPart(HtmlTextWriter output)
        {
            string url = this.UrlWithQueryStringGet();
            if(url.Length > 0)
            {
                string name = "QueryStringPageViewer_" + this.UniqueID;
                output.AddAttribute(HtmlTextWriterAttribute.Id, name, false);
                output.AddAttribute(HtmlTextWriterAttribute.Name, name, false);
                output.AddAttribute(HtmlTextWriterAttribute.Width, "100%", false);
                output.AddAttribute(HtmlTextWriterAttribute.Height, "100%", false);
                output.AddAttribute(HtmlTextWriterAttribute.Height, "100%", false);
                output.AddAttribute(HtmlTextWriterAttribute.Src, url, false);
                output.AddAttribute("ddf_src", url, false);
                output.AddAttribute("frameBorder", "0", false);
                output.RenderBeginTag(HtmlTextWriterTag.Iframe);
                output.RenderBeginTag(HtmlTextWriterTag.Div);
                output.Write("IFrames not supported by this browser");
                output.RenderEndTag();
                output.RenderEndTag();            
            }
            else
            {
                Control c = new LiteralControl(string.Format("To link to content, <a href=\"javascript:MSOTlPn_ShowToolPaneWrapper('{0}','{1}','{2}');\">open the tool pane</a> and then type a URL in the ContentLink text box with QueryString parameters formatted like this: 'Page.aspx?Param1&Param2'.", 1, 129, this.ID)); 
                c.RenderControl(output);
            }
        }
        
        private ArrayList QueryStringKeysGet()
        {
            int startIndex = this.ContentLink.IndexOf("?");
            ArrayList queryStrings = new ArrayList();
            if(startIndex > -1 && this.ContentLink.Length > startIndex + 1)
            {
                string queryStringRaw = this.ContentLink.Substring(startIndex + 1);
                if(queryStringRaw.IndexOf("&") != -1)
                {    
                    string[] tokens = queryStringRaw.Split(new char[]{'&'});
                    for(int i = 0; i < tokens.Length; i++)
                        queryStrings.Add(tokens[i].ToLower());
                }
                else
                {
                    queryStrings.Add(queryStringRaw.ToLower());
                }
            }
            return queryStrings;
        }
        protected virtual string UrlWithQueryStringGet()
        {
            ArrayList queryStringsToPass = this.QueryStringKeysGet();
            string url = this.BaseUrl;
            
            for(int i=0; i < Page.Request.QueryString.Count; i++)
            {
                string queryStringKey = Page.Request.QueryString.Keys[i];
                string queryStringValue =  Page.Request.QueryString[i];
                if(queryStringsToPass.IndexOf(queryStringKey.ToLower()) != -1)
                {
                    url += queryStringKey + "=" + queryStringValue + "&";
                }
            }
            
            url = url.TrimEnd(new char[]{'&'});
            return url;
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
    <Title>QueryStringPageViewer</Title>
    <Description>Passes querystring parameters to the page in the viewer.</Description>
    <Assembly>Objectware.Lawyer.Sharepoint</Assembly>
    <TypeName>Objectware.Lawyer.Sharepoint.WebParts.QueryStringPageViewer</TypeName>
    <!-- Specify initial values for any additional base class or custom properties here. -->
    <ContentLink xmlns="http://www.objectware.no/Lawyer/Sharepoint/Webparts/QueryStringPageViewer"></ContentLink>
</WebPart>
Filed under:

Comments

# ChrisPragash said:

Hello,

Thanks for this code snippet. I need some help with this thoug; I have a URL something like http://www.website.com?txtUserName=Username&txtPassword=password. I know this works on a normal browser, but using the QueryString PageViewer web part, I'm unable to browse this link, it always comes right back to the Login Page, and even if I try to log in using the right username password and submit the page, it never logs me in, keeps coming back to the Login page. I have tried mutiple web sites and each have the same behavior. Any help here would be greatly appreciated.

Thanks,
Chris

Wednesday, July 28, 2004 3:25 PM
# ChrisPragash said:

Hello,

Thanks for this code snippet. I need some help with this thoug; I have a URL something like http://www.website.com?txtUserName=Username&txtPassword=password. I know this works on a normal browser, but using the QueryString PageViewer web part, I'm unable to browse this link, it always comes right back to the Login Page, and even if I try to log in using the right username password and submit the page, it never logs me in, keeps coming back to the Login page. I have tried mutiple web sites and each have the same behavior. Any help here would be greatly appreciated.

Thanks,
Chris

Wednesday, July 28, 2004 3:25 PM
# TrackBack said:
Tuesday, March 08, 2005 4:43 AM
# Kiran said:

Hi,

When I used this code snippet I am getting errors for all of the assemblies. If i comment those references and run it then i cant see the text box to add the url in the webpart. And where should i use the dwp file?

Wednesday, September 12, 2007 4:36 AM
# Vijay said:

I have one data entry form with validation controls inside the Page viewer webpart. When i press "submit" button page remains in the same position instead of going to page Top.

Any idea?

Tuesday, October 30, 2007 10:00 AM
# Nathan Peters said:

Thank you for this post. I plan on creating this as a WSS 3.0 web part, and I will post a link to a compiled DLL/source code when completed.

Friday, November 30, 2007 8:02 PM
# Lee Shen said:

I could make the cs code work only by adding following overidden method GetToolParts.

public override ToolPart[] GetToolParts()

{

 ToolPart[] toolparts = new ToolPart[2];

 CustomPropertyToolPart custom = new  CustomPropertyToolPart();

 WebPartToolPart wptp = new WebPartToolPart();

 wptp.Expand(WebPartToolPart.Categories.Appearance);

  wptp.Hide(WebPartToolPart.Properties.FrameState);

  wptp.Hide(WebPartToolPart.Properties.FrameType);

  toolparts[0] = wptp;

  toolparts[1] = custom;

     return toolparts;

}

Wednesday, March 19, 2008 3:11 PM
# Wahm com the online magazine for work at home moms. said:

Site build it work at home moms wahm. Work from home moms. Moms work at home. Work at home moms message boards. Moms work at home center.

Thursday, June 12, 2008 12:13 AM
# Ephedrine to buy. said:

Ephedrine products. Ephedrine liquid gels. Reducing ephedrine into meth. How to make meth from ephedrine hcl. Ephedrine..

Wednesday, July 30, 2008 2:10 AM
# transparent bikini said:

Help me to find transparent bikini

Wednesday, June 10, 2009 2:09 PM
# transparent bikini said:

Help me to find transparent bikini

Wednesday, June 10, 2009 2:09 PM
# gas powered scooters said:

You will see Buy gas powered scooters

www.world66.com/.../gas_powered_scoote

Wednesday, June 10, 2009 8:46 PM
# name said:

So where it to find,

Wednesday, July 29, 2009 5:00 AM
# name said:

So where it to find?,

Wednesday, July 29, 2009 7:11 PM
# name said:

I like your work!,

Thursday, July 30, 2009 8:38 AM

Leave a Comment

(required) 
(required) 
(optional)
(required)