Search code examples
asp.netjavascriptsharepointlogin-name

SharePoint - get logged-in user with ASP, manipulate with javascript?


I have an .aspx page built in SharePoint that I want to get the user name of the currently logged-in user (through ASP) and then manipulate it (using javascript). I'm getting an "object expected" error. Here's some of the code:

function checkGroupPermissions() { var loginNameVar = document.getElementById("LoginName1").innerHTML;

--snip--

}

</script> <div id="loginNameDiv" style="display:none"> <asp:LoginName runat="server" id="LoginName1"></asp:LoginName> </div>

I appreciate any and all assistance...please and thanks :)


Solution

  • You cannot refer to an asp.net server control on the client side javascript by directly referring to its Id property. You have to use the controls ClientID property for that. Since your javascript is within your Sharepoint .aspx webpage, you can refer to it as:

    document.getElementById('<%=LoginName1.ClientID %>');
    

    See this for more details:

    MSDN article on Client Script in .Net Pages