使用Javascript代码查找当前SharePoint用户
Hy 我需要从SharePoint中查找当前用户。 我试过很多东西:使用Javascript代码查找当前SharePoint用户,javascript,sharepoint,sharepoint-2010,Javascript,Sharepoint,Sharepoint 2010,Hy 我需要从SharePoint中查找当前用户。 我试过很多东西: SP.Utilities.princicalInfo.get\u loginName() \u spPageContextInfo.userId 在任何时候,我都会得到相同的结果Undefined=(答案可能就在这里。我唯一改变的是获得LoginName而不是Title: 如果您未定义..可能您未经过身份验证或在母版页中未包含某些相关javascript文件。答案可能在这里。我唯一更改的是获取LoginName而不是标
SP.Utilities.princicalInfo.get\u loginName()
\u spPageContextInfo.userId
在任何时候,我都会得到相同的结果
Undefined
=(答案可能就在这里。我唯一改变的是获得LoginName而不是Title:
如果您未定义..可能您未经过身份验证或在母版页中未包含某些相关javascript文件。答案可能在这里。我唯一更改的是获取LoginName而不是标题:
如果您未定义..可能您未经过身份验证或在母版页中未包含某些相关javascript文件。使用CSOM API检索当前用户对象时,请将代码包装在其中 要确保在加载SharePoint JS库(
sp.JS
)后执行指定的代码,请执行以下操作:
SP.SOD.executeOrDelayUntilScriptLoaded(function(){
//your code goes here..
}, 'sp.js');
如何使用CSOMAPI检索当前用户对象
用法
使用CSOMAPI检索当前用户对象时,请将代码包装在其中 要确保在加载SharePoint JS库(
sp.JS
)后执行指定的代码,请执行以下操作:
SP.SOD.executeOrDelayUntilScriptLoaded(function(){
//your code goes here..
}, 'sp.js');
如何使用CSOMAPI检索当前用户对象
用法
没有jquery:
var userid= _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
function createXMLHttp() {
//If XMLHttpRequest is available then using it
if (typeof XMLHttpRequest !== undefined) {
return new XMLHttpRequest;
//if window.ActiveXObject is available than the user is using IE...so we have to create the newest version XMLHttp object
} else if (window.ActiveXObject) {
var ieXMLHttpVersions = ['MSXML2.XMLHttp.5.0', 'MSXML2.XMLHttp.4.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp', 'Microsoft.XMLHttp'],
xmlHttp;
//In this array we are starting from the first element (newest version) and trying to create it. If there is an
//exception thrown we are handling it (and doing nothing ^^)
for (var i = 0; i < ieXMLHttpVersions.length; i++) {
try {
xmlHttp = new ActiveXObject(ieXMLHttpVersions[i]);
return xmlHttp;
} catch (e) {
}
}
}
}
function getData() {
var xmlHttp = createXMLHttp();
xmlHttp.open('get', requestUri , true);
xmlHttp.setRequestHeader("Content-Type", "application/json;odata=verbose");
xmlHttp.setRequestHeader("accept", "application/json;odata=verbose");
xmlHttp.send(null);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) {
var data = JSON.parse(xmlHttp.responseText);
var loginName = data.d.LoginName;
alert(loginName);
} else {
}
} else {
//still processing
}
};
}
getData();
var userid=\u spPageContextInfo.userid;
var requestUri=\u spPageContextInfo.webAbsoluteUrl+“/\u api/web/getuserbyid(“+userid+”);
函数createXMLHttp(){
//如果XMLHttpRequest可用,则使用它
if(XMLHttpRequest的类型!==未定义){
返回新的XMLHttpRequest;
//如果window.ActiveXObject可用,那么用户使用的IE…因此我们必须创建最新版本的XMLHttp对象
}else if(window.ActiveXObject){
var ieXMLHttpVersions=['MSXML2.XMLHttp.5.0'、'MSXML2.XMLHttp.4.0'、'MSXML2.XMLHttp.3.0'、'MSXML2.XMLHttp'、'Microsoft.XMLHttp'],
xmlHttp;
//在这个数组中,我们从第一个元素(最新版本)开始创建它
//抛出异常我们正在处理它(并且什么也不做^^)
对于(var i=0;i
不带jquery:
var userid= _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
function createXMLHttp() {
//If XMLHttpRequest is available then using it
if (typeof XMLHttpRequest !== undefined) {
return new XMLHttpRequest;
//if window.ActiveXObject is available than the user is using IE...so we have to create the newest version XMLHttp object
} else if (window.ActiveXObject) {
var ieXMLHttpVersions = ['MSXML2.XMLHttp.5.0', 'MSXML2.XMLHttp.4.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp', 'Microsoft.XMLHttp'],
xmlHttp;
//In this array we are starting from the first element (newest version) and trying to create it. If there is an
//exception thrown we are handling it (and doing nothing ^^)
for (var i = 0; i < ieXMLHttpVersions.length; i++) {
try {
xmlHttp = new ActiveXObject(ieXMLHttpVersions[i]);
return xmlHttp;
} catch (e) {
}
}
}
}
function getData() {
var xmlHttp = createXMLHttp();
xmlHttp.open('get', requestUri , true);
xmlHttp.setRequestHeader("Content-Type", "application/json;odata=verbose");
xmlHttp.setRequestHeader("accept", "application/json;odata=verbose");
xmlHttp.send(null);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) {
var data = JSON.parse(xmlHttp.responseText);
var loginName = data.d.LoginName;
alert(loginName);
} else {
}
} else {
//still processing
}
};
}
getData();
var userid=\u spPageContextInfo.userid;
var requestUri=\u spPageContextInfo.webAbsoluteUrl+“/\u api/web/getuserbyid(“+userid+”);
函数createXMLHttp(){
//如果XMLHttpRequest可用,则使用它
if(XMLHttpRequest的类型!==未定义){
返回新的XMLHttpRequest;
//如果window.ActiveXObject可用,那么用户使用的IE…因此我们必须创建最新版本的XMLHttp对象
}else if(window.ActiveXObject){
var ieXMLHttpVersions=['MSXML2.XMLHttp.5.0'、'MSXML2.XMLHttp.4.0'、'MSXML2.XMLHttp.3.0'、'MSXML2.XMLHttp'、'Microsoft.XMLHttp'],
xmlHttp;
//在这个数组中,我们从第一个元素(最新版本)开始创建它
//抛出异常我们正在处理它(并且什么也不做^^)
对于(var i=0;i
请参阅以下帖子[此代码不起作用web.get\u currentUser()
。我已经有了相同的结果未定义
…因此,如果你在你的网站上进入chrome,打开开发工具,键入_spUserId或_spPageContextInfo.userId,你得到了未定义的?如果你在javascript控制台中有值,那么Vadim的答案应该可以工作——如果你仍然有未定义的,那么还有其他的东西页面错误,导致JS无法正确执行。请参阅以下帖子[此代码无法工作web.get\u currentUser())
。我已经有了相同的结果未定义
…因此,如果你在你的网站上进入chrome,打开开发工具,键入_spUserId或_spPageContextInfo.userId,你得到了未定义的?如果你在javascript控制台中有值,那么Vadim的答案应该可以工作——如果你仍然有未定义的,那么还有其他的东西页面错误,导致JS无法正确执行。使用此解决方案,我有一个错误,$未定义
。我需要导入一个JQuery脚本。但我无法导入脚本。@Samuel_u。您应该能够在内容编辑器中使用标准脚本标记——使用此解决方案,我有一个错误,$未定义ed
。我需要导入JQuery脚本。但我无法导入脚本。@Sam
var userid= _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
function createXMLHttp() {
//If XMLHttpRequest is available then using it
if (typeof XMLHttpRequest !== undefined) {
return new XMLHttpRequest;
//if window.ActiveXObject is available than the user is using IE...so we have to create the newest version XMLHttp object
} else if (window.ActiveXObject) {
var ieXMLHttpVersions = ['MSXML2.XMLHttp.5.0', 'MSXML2.XMLHttp.4.0', 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp', 'Microsoft.XMLHttp'],
xmlHttp;
//In this array we are starting from the first element (newest version) and trying to create it. If there is an
//exception thrown we are handling it (and doing nothing ^^)
for (var i = 0; i < ieXMLHttpVersions.length; i++) {
try {
xmlHttp = new ActiveXObject(ieXMLHttpVersions[i]);
return xmlHttp;
} catch (e) {
}
}
}
}
function getData() {
var xmlHttp = createXMLHttp();
xmlHttp.open('get', requestUri , true);
xmlHttp.setRequestHeader("Content-Type", "application/json;odata=verbose");
xmlHttp.setRequestHeader("accept", "application/json;odata=verbose");
xmlHttp.send(null);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) {
var data = JSON.parse(xmlHttp.responseText);
var loginName = data.d.LoginName;
alert(loginName);
} else {
}
} else {
//still processing
}
};
}
getData();