admin管理员组文章数量:1401444
Does SharePoint 2010 store the current user's accountname somewhere in a globally accessible JS object?
Every solution I can find involves some variation of an ajax web service call and this seems like an extremely heavy solution to access what should be a trivial piece of information.
Things I can access easily:
- Get current user id from
__spUserId
- Get current user name by scraping the html of the ribbon (eg:
$('#zz17_Menu').text()
)
But neither of these is the accountname.
Things I would rather not do:
- Get current user info with a SOAP call to GetUserProfileByName
- Get current user info by making an Ajax call for ../_layouts/userdisp.aspx
Does SharePoint 2010 store the current user's accountname somewhere in a globally accessible JS object?
Every solution I can find involves some variation of an ajax web service call and this seems like an extremely heavy solution to access what should be a trivial piece of information.
Things I can access easily:
- Get current user id from
__spUserId
- Get current user name by scraping the html of the ribbon (eg:
$('#zz17_Menu').text()
)
But neither of these is the accountname.
Things I would rather not do:
- Get current user info with a SOAP call to GetUserProfileByName
- Get current user info by making an Ajax call for ../_layouts/userdisp.aspx
2 Answers
Reset to default 5For anyone else who stumbles across this, a year later I figured out how.
To your master page register SPSWC (if you're using Randy Drisgill's starter master it will already be there).
<%@ Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Then, before you want to access a property (eg: UserName) pull in the data
<SPSWC:ProfilePropertyLoader runat="server"/>
Next, access one of the properties, I would remend using TitleMode to embed it in a <script>
element.
<script>
var username = '<SPSWC:ProfilePropertyValue PropertyName="UserName" TitleMode="true" runat="server"/>';
</script>
We now use it as shown in this PasteBin demo to populate a global with a few properties like first name, last name, user name, profile picture, status.
You really need to make a web service call to do it reliably. Thankfully, SPServices makes it quick and painless to do so.
http://spservices.codeplex./wikipage?title=$%28%29.SPServices.SPGetCurrentUser
$().SPServices.SPGetCurrentUser({
fieldName: "Name",
debug: false
});
本文标签: javascriptSharePoint get current user accountnameStack Overflow
版权声明:本文标题:javascript - SharePoint get current user accountname? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744261421a2597734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论