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>