Yanıt How to get current site URL in a New/Edit Form

  • 08 Ağustos 2012 Çarşamba 10:27
     
     

    I'm working on customize New/Edit form on SharePoint 2010 by SPD 2010.

    I need to reference some Javascript code and css file, which are located in 'Documents' list on the same site. Now I use absolute URL to reference the files. But when I deploy to production, the site name will be changed also the server name. So I hope to build a relative reference, or get the current site URL in page code.

    Anyone knows how to make it?


    Alex Du

Tüm Yanıtlar

  • 08 Ağustos 2012 Çarşamba 12:54
     
     

    hi

    check this doc regarding relative URL

    http://www.ibdhost.com/help/path/


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

  • 08 Ağustos 2012 Çarşamba 13:33
     
      Kod İçerir

    $().SPServices.SPGetCurrentSite(); --> For this refer to the spservices library ofcourse.

    This is one way of getting the url or else you can use windows.location.href and split it.

    -AJ

  • 08 Ağustos 2012 Çarşamba 13:50
     
     

    Hi AJ,

    I think your method works, but may not for my case.

    It seems you invoke SharePoint client model to retreive the site URL, which is an addin of jQuery. But at first I need to reference jQuery library like <script scr="http://servername/sitename/documents/jQuery.js" /> in the New Form page code.

    But when I deploy to production, the servername and sitename part will be changed. So I hope to use a relative path to replace the src part, or has some method to know the site url forehand to generate the src part.

    So your method seems depends on successfully referenced jQuery library, which is my difficult part.


    Alex Du

  • 09 Ağustos 2012 Perşembe 09:33
    Moderatör
     
     Yanıt

    Hi ,

    I understand that you want to add javascript on New\Edit form to get current site URL. When you create an item to a list ,the url is like :http://servername:portnumber/sitename/Lists/listname/NewForm.aspx?RootFolder=.The ‘Lists’ in the URL is unique ,you can use this to get the current site URL. Here is the javascript code :

        <script type="text/javascript" language="javascript">

             window.onload = function () {

                 var currentUrl = window.location.href;

                 currentUrl = currentUrl.substring(0, currentUrl.lastIndexOf('Lists')+1);

                 alert(currentUrl);

             }

         </script>

    When it comes to a library ,you need to change Lists in the code to the library name.

    Thanks,

    Entan Ming


    Entan Ming

    TechNet Community Support

  • 09 Ağustos 2012 Perşembe 10:02
     
     Yanıt

    Hello,

    You can do like below to refer the files uploaded in the same site.

    <script type="text/javascript" src="../../List Name/jquery.js"></script>

    Also to access the current context using Java script, please go through the below link.

    http://msdn.microsoft.com/en-us/library/gg701783.aspx

    Thanks,
    Dhileep