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

sharepoint0520
sharepoint0520 used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software/web developer
Commented:
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.

Author

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 developer

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial