2

I would like to find a way how to redirect each user to first accessible subsite in SharePoint 2013.

I have an example of sites' structure:

  • MAIN SITE (homepage with links to sites)
    • Site1
      -Subsite1 -Subsite2 -Subsite3 -Subsite4
    • Site2
      -Subsite5 -Subsite6 -Subsite7
    • Site3
      -Subsite8 -Subsite9 -Subsite10 -Subsite11 -Subsite12

I need to automatically redirect SP user from Site1 to Subsite1 or (if user has no permissions to subsite1) Subsite2 (or) Subsite3 (or) Subsite4.

This solution should be implemented on each site (1,2,3).

Can somebody help me?

EDIT:

I have now a trouble with executing code (function runCode()):

var webCollection = null;

function runCode() {
    var clientContext = new SP.ClientContext.get_current();
    if (clientContext != undefined && clientContext != null) {
        var web = clientContext.get_web();
        this.webCollection = web.getSubwebsForCurrentUser(null);
        clientContext.load(this.webCollection);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
}

function onQuerySucceeded() {
    var webName = "";
    var webEnumerator = this.webCollection.getEnumerator();
    webEnumerator.moveNext();
        var web = webEnumerator.get_current();
        webName += web.get_title(); 
    window.location.href = "http://sharepoint/sites/m1/s/" + webName;
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
1

You could check SharePoint REST API to make ajax request and perform your expected behavior.

https://msdn.microsoft.com/en-us/library/office/dn499819.aspx#bk_WebGetSubwebsFilteredForCurrentUser

You could also take time to learn how to use JSOM API to do the same think (but too heavy to implement if you have not a friendly framework :)

https://msdn.microsoft.com/en-us/library/office/jj246242.aspx

Hope it helps :)

  • Thank you for your answer. Unfortunately, the first solution you presented works only in SharePoint Online but I use SharePoint Server and the second solution was found previously by me and it doesn't work. I tried to implement it in both: default.aspx by SharePoint Designer and Script Editor Web Part. I need other solutions. – xeroxix Apr 25 '16 at 14:54
  • The second solution shows only a button but without result after click - I see only a text: ''Child sites of the current site:'... – xeroxix Apr 25 '16 at 14:59
  • Could you share your code used in the second solution? Maybe something is missing. – Stact Apr 25 '16 at 14:59
  • I just copied and pasted code from MSDN. I don't think it's a problem with missing code. – xeroxix Apr 25 '16 at 15:02
  • Sometimes, scripts are not loaded correctly. Check with this: SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { // Your code }); – Stact Apr 26 '16 at 8:02
1

It works fine for me:

var clientContext = SP.ClientContext.get_current();
var contextWeb = clientContext.get_web();
var webCollection = contextWeb.getSubwebsForCurrentUser(null);
clientContext.load(webCollection);

clientContext.executeQueryAsync(
    function () {
        // console.log('success :)');
        var webEnumerator = webCollection.getEnumerator();
        var subsites = [];
        while (webEnumerator.moveNext()) {
            var web = webEnumerator.get_current();
            var title = web.get_title();
            var url = web.get_url()
            var logoUrl = web.get_siteLogoUrl();
            var description = web.get_description();

            subsites.push({url: url, title: title, description: description, logoUrl: logoUrl});
        }
        // console.log(subsites)
        // do stuff to reorder
        var firstSiteUrl = subsites[0].url;
        // do stuff to redirect
        // console.log(firstSiteUrl);
    },
    function () {
        // log failure
        // console.log('error :(');
    }
);
  • Thanks for your contribution but I just adapted previous code to work fine. Your guidelines were very useful to me and I achieved an appropriate solution to my case. – xeroxix Apr 26 '16 at 13:45
  • I improved my working solution by replacing get_title() to get_url() and it works really better - especially in a situation when subsite title is different than the site URL ending, it never redirects any user. Now it's universal by getting url instead of subsite title. Thank you again! – xeroxix Apr 26 '16 at 14:21
0

Finally I get a fully worked solution on each site in SharePoint.

It needs to be pasted in default.aspx under the tag:

<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

Here is the js code:

<script type = "text/javascript" >

var webCollection = null;

function runCode() {
    var clientContext = new SP.ClientContext.get_current();
    if (clientContext != undefined && clientContext != null) {
        var web = clientContext.get_web();
        this.webCollection = web.getSubwebsForCurrentUser(null);
        clientContext.load(this.webCollection);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
}

function onQuerySucceeded() {
    var webName = "";
    var webEnumerator = this.webCollection.getEnumerator();
    webEnumerator.moveNext();
    var web = webEnumerator.get_current();
    webName += web.get_url(); 
    window.location.href = webName;
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
window.setTimeout(function(){runCode()},0);


</script>   

IMPORTANT!

I must notice that first subsite is the first created subsite.

If somebody moves the hyperlinks in top navigation bar, it doesn't matter for above redirection method - it always moves to the first created (by admin or developer) subsite!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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