如何在脚本编辑器Web部件中使用JavaScript获取当前用户名?
答案 0 :(得分:33)
以下代码对我有用:
<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>
答案 1 :(得分:19)
我找到了一种更简单的方法,它甚至不使用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混在一起,尽管我的情况并非真的有必要。
答案 2 :(得分:12)
这个怎么样:
$.getJSON(_spPageContextInfo.webServerRelativeUrl + "/_api/web/currentuser")
.done(function(data){
console.log(data.Title);
})
.fail(function() { console.log("Failed")});
答案 3 :(得分:12)
如果您在SharePoint页面中,请使用:
_spPageContextInfo.userId;
答案 4 :(得分: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文档。
答案 5 :(得分:3)
获取当前用户信息:
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 );
});
答案 6 :(得分:1)
你可以使用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);
}
答案 7 :(得分:1)
我必须使用XML进行此操作,通过添加内容编辑器Web部件将以下内容放入内容编辑器Web部件中,编辑Web部件,然后单击“编辑源代码”按钮并粘贴到其中:
ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:
Error at Line: 11 Column: 38
检查以下链接以查看您的数据是XML还是JSON:
在接受的答案中,凯特(Kate)使用以下方法:
SELECT a.BG_BUG_ID as "Defect-Id",
a.BG_SEVERITY as "Severity",
a.BG_STATUS as "Status",
a.BG_SUMMARY as "Summary",
a.BG_DETECTION_DATE as "Detected on Date",
a.BG_USER_05 as "Resolution",
a.BG_USER_01 as"Product",
a.BG_DETECTION_VERSION as "Detected in Version",
CAST(CASE
WHEN b.TS_TEST_ID IS NOT NULL
THEN CAST(b.TS_TEST_ID AS varchar)
ELSE "N/A"
END AS varchar) as "Test-Id",
CAST(CASE
WHEN c.RN_RUN_ID IS NOT NULL
THEN CAST(c.RN_RUN_ID AS varchar)
ELSE "N/A"
END AS varchar) as "Run-Id",
CAST(CASE
WHEN d.TC_TESTCYCL_ID IS NOT NULL
THEN CAST(d.TC_TESTCYCL_ID AS varchar)
ELSE "N/A"
END AS varchar) as "Test-Instance-Id",
CAST(CASE
WHEN e.BG_BUG_ID IS NOT NULL
THEN CAST(e.BG_BUG_ID AS varchar)
ELSE "N/A"
END AS varchar) as "Linked-Defect-Id", -- there is no need for this ID. You have it in the first selected coulmn
CAST(CASE
WHEN f.ST_ID IS NOT NULL
THEN CAST(f.ST_ID AS varchar)
ELSE "N/A"
END AS varchar) as "TestStep-Id",
CAST(CASE
WHEN g.CY_CYCLE_ID IS NOT NULL
THEN CAST(g.CY_CYCLE_ID AS varchar)
ELSE "N/A"
END AS varchar) as "Test-Set-Id",
CAST(CASE
WHEN h.RQ_REQ_ID IS NOT NULL
THEN CAST(h.RQ_REQ_ID AS varchar)
ELSE "N/A"
END AS varchar) as "Requirement-Id(Direct Link)",
CAST(CASE
WHEN i.RC_REQ_ID IS NOT NULL
THEN CAST(i.RC_REQ_ID AS varchar)
ELSE "N/A"
END AS varchar) as "Requirement-Id(Coverage Link)"
FROM
BUG a
FULL JOIN LINK l ON (a.BG_BUG_ID = l.LN_BUG_ID AND l.LN_ENTITY_TYPE IN ("TEST","RUN","TESTCYCL","BUG","STEP","CYCLE", "REQ"))
LEFT JOIN TEST b ON (l.LN_ENTITY_ID = b.TS_TEST_ID AND l.LN_ENTITY_TYPE = "TEST")
LEFT JOIN RUN c ON (l.LN_ENTITY_ID = c.RN_RUN_ID AND l.LN_ENTITY_TYPE = "RUN")
LEFT JOIN TESTCYCL d ON (l.LN_ENTITY_ID = d.TC_TESTCYCL_ID AND l.LN_ENTITY_TYPE = "TESTCYCL")
LEFT JOIN BUG e ON (l.LN_ENTITY_ID = e.BG_BUG_ID AND l.LN_ENTITY_TYPE = "BUG")
LEFT JOIN STEP f ON (l.LN_ENTITY_ID = f.ST_ID AND l.LN_ENTITY_TYPE = "STEP")
LEFT JOIN CYCLE g ON (l.LN_ENTITY_ID = g.CY_CYCLE_ID AND l.LN_ENTITY_TYPE = "CYCLE")
LEFT JOIN REQ h ON (l.LN_ENTITY_ID = h.RQ_REQ_ID AND l.LN_ENTITY_TYPE = "REQ")
LEFT JOIN REQ_COVER i ON (i.RC_ENTITY_ID = b.TS_TEST_ID AND i.RC_ENTITY_TYPE="TEST")
答案 8 :(得分: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";
答案 9 :(得分: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());
}
}
答案 10 :(得分:0)
您可以使用sp页面的上下文信息:
_spPageContextOnfo.userLoginName