简体   繁体   中英

Query String in Javascript in Sharepoint ASPX page

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>

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM