none
Redirect to a page from a sharepoint library RRS feed

  • Question

  • Hi All,

    I have a sharepoint library. While loading the library's allitems.aspx page i need to check the user is a administrator or not. if not then i need to redirect the user to certain page something.aspx instantly. How to do it.. I have done some R&D on internet but couldn't get to any solution.

    Friday, October 18, 2019 7:02 AM

All replies

  • Hi

    You can use JSOM to achieve this.

    1. Edit the allitems.aspx page, add a script editor webpart to the page.

    2. Edit the webpart content, add JSOM code to check the user role. If he is not admin then redirect page to some page.

    Below is a example code for you.

    <script src="https://code.jquery.com/jquery-3.2.1.min.js">  
    </script>
    <script>
    	$(document).ready(function() {
    		SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CheckUserAdmin);
    	});
    	
    	var curUser;
    	
    	function CheckUserAdmin(){
    		var ctx = new SP.ClientContext.get_current();
    		var curWeb = ctx.get_web();
    		curUser = curWeb.get_currentUser();
    		ctx.load(curUser);
    		ctx.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
    	}
    	
    	function onQuerySucceeded(){
    		var isSiteAdmin = curUser.get_isSiteAdmin();
    		if(isSiteAdmin)
    		{
    			alert('Site admin');
    		}
    		else
    		{
    			alert('Not site admin');
    		}
    	}
    	
    	function onQueryFailed(){
    		alert('Error occurred.');
    	}
    </script>

    For more information about JSOM, please refer to below link.

    Complete basic operations using JavaScript library code in SharePoint


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Learn Microsoft 365 from Microsoft DOCs now!
    Please Vote and Mark as Answer if it helps you.

    • Proposed as answer by LinyusMVP Friday, October 18, 2019 8:27 AM
    Friday, October 18, 2019 8:19 AM
  • Hi,

    Any update? Did you try to use Justin's solution?

    In SharePoint 2013, we can also use REST API to check the current user permission, then redirect to a page from a document library.

    Example, add the code below into script editor web part in the allitem.aspx page in library.

    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function () {
    	if(!IsAdminUser()){
    		window.location.href="/SitePages/something.aspx";
    	}
    });
    function IsAdminUser() {
    	var flag=false;
    	ExecuteOrDelayUntilScriptLoaded(function(){
    		//Permission for admin to show or hide the entries on memory board using ShowOnHomePage Field
    		var perm = new SP.BasePermissions();
    		perm.set(SP.PermissionKind.manageWeb);
    		$.ajax({
    			url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/doesuserhavepermissions(@v)?@v={'High':'" + perm.$4_1.toString() + "', 'Low':'" + perm.$5_1.toString() + "'}",
    			type: "GET",
    			headers: { "accept": "application/json;odata=verbose" },
    			async:false,
    			success: function (data) {
    				var d = data.d.DoesUserHavePermissions;
    				flag=d;
    			},
    			error: function (err) {
    				console.log(JSON.stringify(err));
    			}
    		});
    	}, "sp.js");	
    	return flag;
    }
    </script>

    More information:

    SharePoint 2013: How to Check User Permission with REST API

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Monday, October 21, 2019 2:39 AM
    Moderator
  • Hi,

    Would you please provide us with an update on the status of your issue?

    Best Regards,
    Dennis

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, October 28, 2019 8:25 AM
    Moderator