Build dynamic Url using the Current SharePoint Site Url Using Javascript, Content Editor WebPart and SPServices.

Build dynamic Url using the Current SharePoint Site Url Using Javascript, Content Editor WebPart and SPServices.

Step 1. Kindly follow my intital post to understand how java script can be used in Content Editor Web Part.

Step 2:This is an extension of my previous post.

Step 3:Cosider you need to have an hyperlink in the SharePoint page that uses the Current SharePoint Site URL, then it can be accomplished this way. 

In this code the link generated will be be to take user to a page within Pages directory of the SharePoint Site. If you append the Source attribute to the link then SharePoint knows how to bring them back.

This link = current Site URL + new page location + new page name + Source address + Querystring

<html>
<head>
<SCRIPT type=text/javascript src="/mysites/myDocLibrary/JsFiles/jquery-1.4.2.min.js"></SCRIPT>
<SCRIPT type=text/javascript src="/mysites/myDocLibrary/JsFiles/JsFiles/jquery.SPServices-0.5.6.min.js"></SCRIPT>
<SCRIPT type=text/javascript>


linkPage = function()
 {
     var thisSite = $().SPServices.SPGetCurrentSite();
     var linkToNewPage = thisSite + "/Pages/NewPage.aspx";
     var sourceURL = document.URL;
     var linkToNewPageFinal = linkToNewPage +"?Source="+ sourceURL +    "&MyQueryString=KeyWords";
      location.href = inkToNewPageFinal;
}

</SCRIPT>
</head>

<Body>

<a href ="javascript:linkPage()">Link to New Page</a>

</Body>
</html>

Step 4: To remove the identity that the code was generated from Content Editor Web Part, within the Web Part Setting, Under Appearance, For Chrome Type select "None". Click OK. Click Apply. Publish the Page.

Step 5: Clicking of "Link to New Page" will take to http://sample.com/sites/basicJs/Pages/NewPage.aspx?Source=http://sample.com/sites/basicJs/Default.aspx&MyQueryString=KeyWords

Step 6:Refet to my next post to extract the values of the QueryString.

Leave your comments below.

No comments: