【问题标题】:Query String in Javascript in Sharepoint ASPX pageSharepoint ASPX 页面中 Javascript 中的查询字符串
【发布时间】:2013-07-31 20:24:36
【问题描述】:

我试图将用户重定向到一个新页面,其中用户名将显示在一个文本框中。我正在使用 ASP.NET 登录名和按钮来执行此操作。我的问题是,我不是 100% 确定如何将 LoginName 中的值获取到需要这个的 javascript 中。我无法进入服务器代码,所以这一切都必须由 SharePoint 设计器完成 我知道常见的方法是做这样的事情

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

但这似乎不想工作。有答案吗?

JavaScript 代码

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

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>

【问题讨论】:

    标签: javascript asp.net sharepoint


    【解决方案1】:

    试试这样的

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

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

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

    【讨论】:

    • 这似乎不起作用,因为它所做的只是获取“@HttpContext.Current.User.Identity.Name”
    【解决方案2】:

    假设您使用的是 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>
    

    唯一的问题是重定向的时间可能需要(稍微)更长的时间,因为代码需要等待 sp.js 加载才能获取当前用户的详细信息。或者,您可以在查询字符串中不带用户名的情况下重定向,只需使用上述代码从您的 ASPX 页面检索用户名。

    【讨论】:

      【解决方案3】:

      您可以找到当前用户的用户名和其他内容。为此,首先在 .aspx 页面顶部添加以下行:&lt;%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt;

      Version=14.0.0.0 用于 Sharepoint 2010,Version=12.0.0.0 用于 Sharepoint 2007)

      现在,在 &lt;form&gt; 标记之后添加这一行:

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

      最后在&lt;/form&gt;标签前添加如下块:

      <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>
      

      因此,在您的页面中,您将看到一个“userDetails”块,其中包含当前用户登录名、名字、姓氏、工作电子邮件和首选名称。使用 JavaScript,您可以通过以下方式获取用户名:

      document.getElementById('ctl00_userLogin').innerHTML
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-27
        • 1970-01-01
        • 2011-01-31
        相关资源
        最近更新 更多