[英]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.
我正在使用ASP.NET登录名和按钮来执行此操作。 My issue is that im not 100% sure on how to get the value from the LoginName into the javascript which needs this.
我的问题是,即时消息不是100%确定如何将LoginName中的值转换为需要此值的javascript。 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
我没有办法进入服务器代码,所以这一切必须由SharePoint设计器完成,我知道常见的方法是做这样的事情
window.location="http://mysite/default.aspx?u="+ LoginName1
But this seems it doesn't want to work. 但这似乎并不起作用。 Any answers?
有什么答案吗?
JavaScript Code JavaScript代码
function Redirect()
{
window.location="http://mysite/default.aspx";
}
ASP.NET Code ASP.NET代码
<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 + '"; 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 参考: 如何在asp.net mvc中的脚本文件中获取当前的登录用户名
Assuming you are using SP2010, you can use Sharepoint's Client Object Model to get the current user details. 假设您正在使用SP2010,则可以使用Sharepoint的客户端对象模型来获取当前的用户详细信息。
<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. 唯一的问题是,重定向的时间可能会花一点时间,因为代码需要等待sp.js加载才能获取当前用户的详细信息。 Alternatively, you can redirect without the username on the query string, and simply retrieve the username from your ASPX page using the above code.
或者,您可以在查询字符串中不带用户名的情况下进行重定向,并使用上述代码从ASPX页面中检索用户名。
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" %>
为此,请在.aspx页顶部添加以下行:
<%@ 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) (
Version=14.0.0.0
2010年SharePoint和Version=12.0.0.0
对于SharePoint 2007)
Now, just after the <form>
tag add this line: 现在,在
<form>
标记之后,添加以下行:
<form>
<SPSWC:ProfilePropertyLoader runat="server"/>
Finally add the below block before the </form>
tag: 最后,在
</form>
标记之前添加以下代码块:
<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. 因此,在您的页面中,您将看到一个“ userDetails”块,其中包含当前用户的登录名,名字,姓氏,工作电子邮件和首选名称。 With JavaScript you can get the username with :
使用JavaScript,您可以通过以下方式获取用户名:
document.getElementById('ctl00_userLogin').innerHTML
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.