最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SharePoint get current user accountname? - Stack Overflow

programmeradmin4浏览0评论

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
Share Improve this question edited Jun 20, 2012 at 17:33 Sinetheta asked Jun 20, 2012 at 17:24 SinethetaSinetheta 9,4595 gold badges32 silver badges52 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

For 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
});
发布评论

评论列表(0)

  1. 暂无评论