none
Dynamic url in promoted links RRS feed

  • Question

  • Hi,

    We want to create a template having promoted links included as a part of the template. Hence whenever a site is created using this template will have promoted links to it.

    There are 2 promoted links that we create which points to 2 libraries in the same site viz., List A and List B. We are using jquery currently to achieve this and following is the code:

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script language="javascript">
    	$(document).ready(function () {
    });
    
    	$(function()
    	{
    		var webAbsURL = _spPageContextInfo.webAbsoluteUrl;
    		var linkTitle = "";
    		var url = "";
    		
    		$("a[id^=Tile_WPQ]").each(function()
    		{
    			$(this).click(function ()
    			{
    				linkTitle = $(this).find('li[title]').attr('id');
    										
    				switch (linkTitle)
    				{
    					case 'Tile_WPQ3_1_6':
    						STSNavigate(webAbsURL + "/Lists/ListA/Allitems.aspx?Source=" + webAbsURL + "/default.aspx");
    						break;
    					
    					case 'Tile_WPQ3_2_6':
    						STSNavigate(webAbsURL + "/Lists/ListB/Allitems.aspx?Source=" + webAbsURL + "/default.aspx");
    						break;
    					
    					default:
    						STSNavigate(webAbsURL);
    				}			
    			})
    		})
    	});
    </script>

    The above code is added in the home page using script editor web part. The site is a sub-site and not the top level site.

    While creating the promoted links, we have added "/" value in the link location as a temporary entry since the field is compulsory.

    It works perfectly fine in IE. Rarely I see that Firefox redirects the page to the "/" site instead of the respective library but rest of the time it redirects to the correct library. However Safari browser always redirects the page to the "/" site and never ever worked so far.

    Can someone please provide guidance on achieving the best solution possible so that it works fine on all the browsers.

    Thank you and

    Regards

    Tanzim


    Tanzim Akhtar

    Wednesday, June 15, 2016 2:42 PM

Answers

  • Hi,

    I modified your code as below, it works in my test environment.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script language="javascript">
    $(function(){
    	var webAbsURL = _spPageContextInfo.webAbsoluteUrl;
    	var linkTitle = "";
    	
    	$("a[id^='Tile_WPQ']").each(function(){
    		$(this).attr("onclick","");
    		$(this).attr("clickAction","");
    		$(this).click(function (){
    		
    			linkTitle = $(this).find('li[title]').attr('id');
    									
    			switch (linkTitle){
    			
    				case  'Tile_WPQ3_1_6':
    					STSNavigate(webAbsURL + "/Lists/ListA/Allitems.aspx?Source=" + webAbsURL + "/default.aspx");
    					break;
    				
    				case 'Tile_WPQ3_2_6':
    					STSNavigate(webAbsURL + "/Lists/ListB/Allitems.aspx?Source=" + webAbsURL + "/default.aspx");
    					break;
    				
    				default:
    					STSNavigate(webAbsURL);
    			}			
    		})
    	})
    });
    </script>

    Best Regards,

    Dennis


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by Tanzim Thursday, June 16, 2016 8:24 AM
    Thursday, June 16, 2016 2:34 AM
    Moderator

All replies

  • Hi,

    I modified your code as below, it works in my test environment.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script language="javascript">
    $(function(){
    	var webAbsURL = _spPageContextInfo.webAbsoluteUrl;
    	var linkTitle = "";
    	
    	$("a[id^='Tile_WPQ']").each(function(){
    		$(this).attr("onclick","");
    		$(this).attr("clickAction","");
    		$(this).click(function (){
    		
    			linkTitle = $(this).find('li[title]').attr('id');
    									
    			switch (linkTitle){
    			
    				case  'Tile_WPQ3_1_6':
    					STSNavigate(webAbsURL + "/Lists/ListA/Allitems.aspx?Source=" + webAbsURL + "/default.aspx");
    					break;
    				
    				case 'Tile_WPQ3_2_6':
    					STSNavigate(webAbsURL + "/Lists/ListB/Allitems.aspx?Source=" + webAbsURL + "/default.aspx");
    					break;
    				
    				default:
    					STSNavigate(webAbsURL);
    			}			
    		})
    	})
    });
    </script>

    Best Regards,

    Dennis


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by Tanzim Thursday, June 16, 2016 8:24 AM
    Thursday, June 16, 2016 2:34 AM
    Moderator
  • Alhamdulillaah, it works.

    Excellent Dennis. Thank you very much for the solution.

    I have no experience in JQuery and probably this is the first one I have written by copy paste from here and there but with understanding. However if I understand your code correctly then I see that you have made the default links blank and then added the required urls. Moreover I read that $(document).ready is necessary to load the sp scripts and then your custom one so that you won't face any issues but I see that your code worked perfectly.

    Can you please educate me on those two points for my knowledge if you think it is appropriate.

    Bet regards

    Tanzim


    Tanzim Akhtar

    Thursday, June 16, 2016 8:29 AM
  • Hi Tanzim,

    $(document).ready(function(){
      //code
    });

    same as:

    $(function() {
      //code
    });

    The key point as below:

    $(this).attr("onclick","");
    $(this).attr("clickAction","");
    

    We need remove the default value of those attributes, some browser have compatibility issues.

    Best Regards,

    Dennis


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Thursday, June 16, 2016 8:47 AM
    Moderator
  • Thank you Dennis once again for your valuable contribution.

    Best regards

    Tanzim


    Tanzim Akhtar

    Thursday, June 16, 2016 9:00 AM