3

I am looking for a solution to navigate to MySite from "main" site without using a hard coded link. Also I want the same when going back to main site.

The solution is going from development to test and then to prod, so I need a way that it gets the URL and sets it as "href". The ribbon wont be shown for the end-user so I can't use this.

PS: I don't have time for using a C# solution. Prefer Javascript etc.

1
  • I suppose your location of mysite and root site is always the same. E.g. localhost-dev/mysite, localhost-prod/mysite so why just not take the server url and append the mysite url part?
    – luccio
    Apr 8, 2015 at 13:35

1 Answer 1

2

There are two ways to do this

  1. One using a global JS variable (either in Master Page or js file in layouts folder). Inside this file you can have the URL and use it to form the dynamic URL
  2. Using JS code

    //Get user's personal MySite url
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetMySiteUrl);
    
    function GetMySiteUrl() {
        //Get the current user's account information
        $.ajax({
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=UserUrl",
            method: "GET",
            headers: {
                "accept": "application/json;odata=verbose",
            },
            success: function (data) {
                alert(data.d.UserUrl;
            },
            error: function (err) {
            }
        });
    }
    
1
  • The JavaScript way works for going to MySite. But how would you go about for coming back? In what way would you call this function again from mysite and should the URL be hard coded here? Because hardcoding is what I am trying to avoid. Apr 9, 2015 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.