2013-07-30 46 views
0

我試圖將用戶重定向到一個新的頁面,其中用戶名將顯示在文本框中。我正在使用ASP.NET登錄名和按鈕來執行此操作。我的問題是,即時通訊不是100%確定如何從LoginName獲取值到需要這個的JavaScript。我沒有辦法進入服務器代碼,所以這一切都必須通過SharePoint設計 做我知道常見的方式是做這樣的事情在Sharepoint ASPX的Javascript查詢字符串頁面

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> 

回答

0

假設你正在使用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頁面檢索用戶名。

0

您可以找到當前用戶的用戶名和其他內容。要做到這一點,通過在你的.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 2010年SharePoint和Version=12.0.0.0 2007年的Sharepoint)

現在,只需在<form>標記後添加此行:

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

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

因此,在您的頁面中,您將看到一個包含當前用戶登錄,名字,姓氏,工作電子郵件和首選名稱的「userDetails」塊。用JavaScript你可以得到用戶名:

document.getElementById('ctl00_userLogin').innerHTML