Showing posts with label parameter. Show all posts
Showing posts with label parameter. Show all posts

Thursday, November 22, 2007

Passing url parameters to webpart in SharePoint

Sometimes you may need to create a webpart page in SharePoint that it can use the parameters passed by url. But the "Page Viewer" webpart does not allow you to use the parameters in your webpart page url. I even tried to use SharePoint Designer 2007 to modify the page directly, but still does not work.

So I think why not directly use a "Content Editor" webpart? Because a content editor webpart allows you to embed javascript code in it. We can use an "iframe" which url is generated by javascript with url parameters from the webpart page.

Then I create a webpart page, insert in a content editor webpart, and then insert the following code in it:

<script type="text/javascript">
function getQueryStringRegExp(name)
{
var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");
if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, " "));
return "";
}

document.write("<iframe src='http://www.yourdomain.com/index.php?test="
+ getQueryStringRegExp('test') + "' width=100% height=100%></iframe>");
</script>


The function getQueryStringRegExp is for retrieve parameter value from url, which is get from here:
http://www.codebit.cn/pub/html/javascript/tip/javascript_get_url_parameter/

Search

Google