0

Im trying to redirect a user to a new page in which there username will be displayed within a text box. I am using ASP.NET Loginname and Buttons to do this. My issue is that im not 100% sure on how to get the value from the LoginName into the javascript which needs this. I have no way to get into the server code so this all has to be done by SharePoint designer I know the common way is to do something like this

window.location="http://mysite/default.aspx?u="+ LoginName1

But this seems it doesn't want to work. Any answers?

JavaScript Code

function Redirect()
{   
    window.location="http://mysite/default.aspx";
}

ASP.NET Code

<asp:Button runat="server" Text="To Sysomos" id="Button1" OnClientClick="if (!Redirect()) { return false;};"></asp:Button>
       <asp:LoginName runat="server" id="LoginName1" ></asp:LoginName>

3 Answers 3

0

try something like this

<script>
var usrName = "@HttpContext.Current.User.Identity.Name";
</script>

window.location="http://mysite/default.aspx?u='+ usrName + '";

Reference : How to get the current login user name in my Script file inside my asp.net mvc

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't seem to work as all it is doing is picking up "@HttpContext.Current.User.Identity.Name"
0

Assuming you are using SP2010, you can use Sharepoint's Client Object Model to get the current user details.

<script type="text/javascript">   
(function () {   
   var spUser;

   // Ensure SP objects have been loaded before executing our code    
   ExecuteOrDelayUntilScriptLoaded(getSpUser,"sp.js");    

   function getSpUser() {
       var clientContext = new SP.ClientContext.get_current();
       var spWeb = clientContext.get_web();
       spUser = spWeb.get_currentUser();
       clientContext.load(spUser);
       clientContext.executeQueryAsync(getSpUserSuccess, getSpUserException);
   }

   function getSpUserSuccess(sender, args) {
       var curUserId = spUser.get_loginName();
       // redirectToSomeAspxPage();
   }

   function getSpUserException(sender, args) {
       // Do any necessary error handling.
   } 
})();
</script>

Only issue with this is that the time to redirect may take (slightly) longer as the code needs to wait for sp.js to load before you can get the current user's details. Alternatively, you can redirect without the username on the query string, and simply retrieve the username from your ASPX page using the above code.

Comments

0

You can find the username and other things for the current user. To do it, start by adding this line at the top of your .aspx page: <%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

(Version=14.0.0.0 for Sharepoint 2010 and Version=12.0.0.0 for Sharepoint 2007)

Now, just after the <form> tag add this line:

<form>
  <SPSWC:ProfilePropertyLoader runat="server"/>

Finally add the below block before the </form> tag:

<div id="userDetails" style="display:none">
  <asp:LoginName runat="server" id="userLogin">
  <SPSWC:ProfilePropertyValue PropertyName="FirstName" ApplyFormatting="false" id="userFirstName" runat="server"/>
  <SPSWC:ProfilePropertyValue PropertyName="LastName" ApplyFormatting="false" id="userLastName" runat="server"/>
  <SPSWC:ProfilePropertyValue PropertyName="WorkEmail" ApplyFormatting="false" id="userWorkEmail" runat="server"/>
  <SPSWC:ProfilePropertyValue PropertyName="PreferredName" ApplyFormatting="false" id="userPreferredName" runat="server"/>
</div>

So in your page you'll see a "userDetails" block with the current user login, firstname, lastname, work email and preferred name. With JavaScript you can get the username with :

document.getElementById('ctl00_userLogin').innerHTML

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.