Pages

Tuesday, September 3, 2013

Passing url parameters to webpart in SharePoint

To pass the URL parameters to a webpart page in SharePoint, we can't use the "Page Viewer" webpart because it does not allow us to use the parameters in the URL.

So we use teh "Content Editor" webpart since it allows us to embed javascript code in it. We can then use an "iframe" with a generated URL from a JavaScript with URL parameters from the webpart page's URL.

First, create a webpart page, then insert a content editor webpart in it with the following code:
<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.sitename.com/default.aspx?param1="+ getQueryStringRegExp('param1') + "' width=100% height=100%></iframe>");
</script>

The function getQueryStringRegExp is used to retrieve the parameter value from the page url.

Referece: