Unable to get property 'SPGetCurrentUser' of undefined or null reference

Experts,
 We have sharepoint 2013 and i am trying to add some javascript add to layout page. We are trying to read current user and compare with sharepoit group. I have added the script but some how it's error. Looks like SP.JS is not loading. Can you please review my code and give some suggestions.

Error : Unable to get property 'SPGetCurrentUser' of undefined or null reference

<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%> <%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.AccessDeniedPage" MasterPageFile="~/_layouts/15/errorv15.master"       %> <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="wssuc" TagName="AccessRequestsDialog" src="~/_controltemplates/15/AccessRequestsDialog.ascx" %> 
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
 <SharePoint:EncodedLiteral runat="server" text="<%$Resources:wss,accessDenied_pagetitle15%>" EncodeMethod='HtmlEncode' Id='LiteralBrowserPageTitle'/> 
</asp:Content> <asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server"> 
<asp:Literal id="LiteralPageTitle" runat="server"/> </asp:Content> <asp:Content contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server"> 
<meta name="Robots" content="NOINDEX " /> <meta name="SharePointError" content="1" />
 </asp:Content> <asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server"> <WebPartPages:AllowFraming runat="server" /> 
<div id="ms-accessDenied-reqDialog"> <wssuc:AccessRequestsDialog Id="AccessRequestsDialog" ShowTitle="True" runat="server"/>
 </div> <div id="AccessDeniedAdditionalDetails" runat="server" /> 
    <div style="float:left;width:100%;">
        <p style="text-align: left;width:100%;font-size:11pt;">
            Please <a title="Login Page" href="javascript:createDynamicRedirectURL();"> "click Me"</a> to  sign in as a different user 
        </p>
    </div>    
	

	
	<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script src="https://code.jquery.com/jquery-1.11.2.min.js" type="text/javascript" ></script>

	
  <script type="text/javascript">

	function insureSPServices(callbackFunction)
    {
	  
      if ($().SPServices == null)
       {  
	   
	   jQuery.getScript("http://server/_layouts/15/sp.runtime.js", callbackFunction); 
	
	   
       }
      else
   	   { 
      callbackFunction.call(null, "Already Loaded"); 
 	   }
	}
	
	function isGroupMember(groupName, callback)
   	{ 
	$().SPServices
	    ({ 
               	operation: "GetGroupCollectionFromUser",
 				userLoginName: $().SPServices.SPGetCurrentUser(),
				async: true, 
				completefunc: function (xData, Status)
				{ 
				callback($(xData.responseXML).find("Group[Name='" + groupName + "']").length == 1);
				} 
				
		});
	};

	function InitializePage(data, textStatus)
	{  
 	isGroupMember("ADRTest Members", function (result)
	  {   
	    console.log("hi");
	    if (result) { 
	  // Code for when current user is in the group
	  alert("User in Group");
	                }  
					Else
					{
					alert("User not in Group");
					}
	  });
	}
	$(document).ready(function () {  
	insureSPServices(InitializePage);
	});
	</script>

     <script language="javascript" type="text/javascript">
         function createDynamicRedirectURL() {
             var curPath = document.location.pathname;
             var curPathArray = curPath.split("_layouts");
             var redirectPage = curPathArray[0];
             var redirectURL = "AccessDenied.aspx?loginasanotheruser=true&Source=" + redirectPage;
             //alert(redirectURL);
             window.location.href = redirectURL;
         }
     </script>

     <script language="javascript" type="text/javascript">
         function createDynamicRedirectURL() {
             var curPath = document.location.pathname;
             var curPathArray = curPath.split("_layouts");
             var redirectPage = curPathArray[0];
             var redirectURL = "AccessDenied.aspx?loginasanotheruser=true&Source=" + redirectPage;
             //alert(redirectURL);
             window.location.href = redirectURL;
         }
     </script>

</asp:Content> 
<asp:Content ContentPlaceHolderId="PlaceHolderGoBackLink" runat="server"> </asp:Content>

Open in new window

sharepoint0520Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Piotr StrycharzSoftware/web developerCommented:
I suppose that you did not include jquery.SPServices.js in your page. You are checking for $().SPServices in your 'insureSPServices' function (should read 'ensure..'?), but you are loading SP.Runtime.js in case of null. Sp.runtime.js is a sharepoint built-in script. In fact you are loading it already in line 18 (BTW, almost certainly this script is already loaded by SharePoint as it contains some important SP functions).

You need jquery.SPServices.js  which is NOT a SharePoint script. See instruction.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sharepoint0520Author Commented:
I think you are right. I did not use SPService.js. I head that SPService.js may not load all the time and sometimes it does not work for chrome. Is there any alternate option to read user in Application page (layout pages) like Access Denied page where user redirect if they don't have access.
Piotr StrycharzSoftware/web developerCommented:
You may use REST function:

http://server/_api/web/currentuser?$select=LoginName

Open in new window


This is quite simple and uses built-in functionality - does not require external scripts.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.