2014-01-07 48 views

回答

24

這裏是爲我工作的代碼:我發現了一個更簡單的方法

<script src="/SiteAssets/jquery.SPServices-2013.02a.js" type="text/javascript"></script> 
<script src="/SiteAssets/jquery.js" type="text/javascript"></script> 

<script type="text/javascript"> 
    var userid= _spPageContextInfo.userId; 
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")"; 
    var requestHeaders = { "accept" : "application/json;odata=verbose" }; 
    $.ajax({ 
    url : requestUri, 
    contentType : "application/json;odata=verbose", 
    headers : requestHeaders, 
    success : onSuccess, 
    error : onError 
    }); 

    function onSuccess(data, request){ 
    var loginName = data.d.Title; 
    alert(loginName); 
    } 

    function onError(error) { 
    alert("error"); 
    } 
</script> 
+0

對於獲取Web用戶信息非常有用。 – GoldBishop

+1

如果您像我一樣使用SP2010,它將無法爲您工作,因爲'_spPageContextInfo.webAbsoluteUrl'在SP2010上不起作用。檢查[這裏](http://sympmarc.com/2013/03/26/using-_sppagecontextinfo-to-determine-the-current-sharepoint-context-in-script) – mohsenof

+0

這也適用於我,謝謝!我無法使用ClientContext對象,因爲它不會觸發任何錯誤,但JS進程被阻塞以創建ClientContext。 – Alex

4

您可以使用SharePoint JSOM來獲取當前用戶的帳戶信息。此代碼(在腳本編輯器Web部件中作爲片段添加時)只會在瀏覽器中彈出用戶的顯示和帳戶名稱 - 您需要在gotAccount中添加其他任何內容以獲取所需格式的名稱。

<script type="text/javascript" src="/_layouts/15/SP.js"></script> 
<script type="text/javascript" src="/_layouts/15/SP.UserProfiles.js"></script> 
<script type="text/javascript"> 

    var personProperties; 

    SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.UserProfiles.js'); 

    function getCurrentUser() { 
    var clientContext = new SP.ClientContext.get_current(); 
    personProperties = new SP.UserProfiles.PeopleManager(clientContext).getMyProperties(); 
    clientContext.load(personProperties); 
    clientContext.executeQueryAsync(gotAccount, requestFailed); 
    } 

    function gotAccount(sender, args) { 
    alert("Display Name: "+ personProperties.get_displayName() + 
     ", Account Name: " + personProperties.get_accountName()); 
    } 

    function requestFailed(sender, args) { 
    alert('Cannot get user account information: ' + args.get_message()); 
    } 

</script> 

有關詳細信息,請參閱MSDN中的SP.UserProfiles.PersonProperties文檔。

+0

也許它只是我,但我無法得到這與ClientWebApp的工作。開始一個乾淨的石板,並重新驗證。這種設計最適合哪種部署模式?關於自動,SharePoint或提供程序託管的Web應用程序。 – GoldBishop

+0

此代碼是爲腳本編輯器Web部件編寫的 - 您是否試圖在SharePoint的應用程序中實現相同的功能?如果是這樣,代碼會有點不同,因爲您不會擁有腳本編輯器Web部件所具有的相同上下文。 –

+0

其實,我能夠從WebApp中獲得用戶,就像腳本編輯器一樣。上下文應該始終如一,取決於你打開哪個門來獲取它。就像EF上下文框架一樣思考上下文。有與WebApp和跨域相關的其他問題。回到開發和實施WebPart,得到我需要做的事情,直到我們可以解決x域。 – GoldBishop

16

,它甚至不使用SP.UserProfiles.js。我不知道它是否適用於每個人的特殊情況,但絕對值得分享。

//assume we have a client context called context. 
var web = context.get_web(); 
var user = web.get_currentUser(); //must load this to access info. 
context.load(user); 
context.executeQueryAsync(function(){ 
    alert("User is: " + user.get_title()); //there is also id, email, so this is pretty useful. 
}, function(){alert(":(");}); 

無論如何,感謝您的回答,我想與UserProfiles混合一下,即使這對我的情況來說並不是必要的。

1

U可以使用JavaScript才達到,像這樣:

function loadConstants() { 

    this.clientContext = new SP.ClientContext.get_current(); 
    this.clientContext = new SP.ClientContext.get_current(); 
    this.oWeb = clientContext.get_web(); 
    currentUser = this.oWeb.get_currentUser(); 
    this.clientContext.load(currentUser); 
     completefunc:this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed)); 


} 

//U must set a timeout to recivie the exactly user u want: 

function onQuerySucceeded(sender, args) { 

    window.setTimeout("ttt();",1000); 
} 
function onQueryFailed(sender, args) { 

    console.log(args.get_message()); 
} 

//By using a proper timeout, u can get current user : 

function ttt(){ 

    var clientContext = new SP.ClientContext.get_current(); 
    var groupCollection = clientContext.get_web().get_siteGroups(); 
    visitorsGroup = groupCollection.getByName('OLAP Portal Members'); 

    t=this.currentUser .get_loginName().toLowerCase(); 

    console.log ('this.currentUser .get_loginName() : '+ t);  

} 
11

如何:

$.getJSON(_spPageContextInfo.webServerRelativeUrl + "/_api/web/currentuser") 
.done(function(data){ 
    console.log(data.Title); 
}) 
.fail(function() { console.log("Failed")}); 
+0

非常感謝。 – avrahamcool

8

如果你是在SharePoint頁面只需使用:

_spPageContextInfo.userId; 
+0

請求的是獲取用戶名。這只是返回用戶ID。 – Peter

+0

如果您使用腳本編輯器Web部件,則可以使用_spPageContextInfo.userDisplayName或_spPageContextInfo.userDisplayName來獲取用戶名或電子郵件地址。 – jdgregson

0

你如果您知道用戶的ID,可以使用以下功能:

function getUser(id){ 
var returnValue; 
jQuery.ajax({ 
url: "http://YourSite/_api/Web/GetUserById(" + id + ")", 
type: "GET", 
headers: { "Accept": "application/json;odata=verbose" }, 
success: function(data) { 
     var dataResults = data.d; 
     alert(dataResults.Title); 
    } 
}); 
} 

,或者你可以嘗試

var listURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/currentuser"; 
0

試試這個代碼..

function GetCurrentUsers() { 

    var context = new SP.ClientContext.get_current(); 
    this.website = context.get_web(); 
    var currentUser = website.get_currentUser(); 
    context.load(currentUser); 
    context.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed)); 
    function onQuerySucceeded() { 

     var currentUsers = currentUser.get_title(); 
     document.getElementById("txtIssued").innerHTML = currentUsers; 

    } 

    function onQueryFailed(sender, args) { 
     alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); 
    } 

}

+0

考慮添加一些解釋和代碼。 – Alexei

0

要獲取當前用戶信息:

jQuery.ajax({ 
    url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/currentuser", 
    type: "GET", 
    headers: { "Accept": "application/json;odata=verbose" } 
}).done(function(data){ 
    console.log(data); 
    console.log(data.d.Title); 
}).fail(function(){ 
    console.log(failed); 
}); 
相關問題