Question

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.

Was it helpful?

Solution

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) {
            }
        });
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top