Use Javascript to obtain the username and Group information of the currently logged-on SharePoint user.

Source: Internet
Author: User

When developing a program to be deployed in SharePoint, you must use Javascript to obtain the user information currently logged on to SharePoint.

The key issue here is how to obtain the username of the current logon. With the username, you can use WebService to query the group to which the username belongs.

Because the top right of each page in SharePoint has a Welcome information (such as Welcome Domain \ Logoned User Name ), therefore, one of the more unorthodox methods is to traverse the <a> </a> element of a page using JavaScript. If the content in the <a> element starts with Welcome, the User Name of the currently logged-on user is followed by the Welcome.

Function getCurrentUser ()
{
Var tags = document. getElementsByTagName ('A ');
For (var I = 0; I <tags. length; I ++)
{
If (tags [I]. innerText. substr (0, 7) = 'Welcome ')
Return tags [I]. innerText. substr (8, tags [I]. innerText. length );
Return null;
}
}
Later, after reading an article WSS 3.0: getting the current user login name via javascript, we found a more reasonable way to obtain the current login user information.

On the SharePoint page, Welcome control provides a _ spUserId variable and saves the ID of the currently logged-on user. With this variable, we can obtain the message of this user through WebService based on this ID. The following post the sample code. The SPAPI_Core.js, SPAPI_Lists.js, and SPAPI_UserGroup.js files are uploaded to the appropriate library of SharePoint and corresponding paths. These three files are part of SPAPI_Javascript. SPAPI encapsulates a series of methods for javascript to call webservice. The entire file can be downloaded from downloadsat http://darrenjohnstone.net/website.

<Script language = "javascript" src = "Shared % 20 Documents/SPAPI_Core.js"> </script>
<Script language = "javascript" src = "Shared % 20 Documents/SPAPI_Lists.js"> </script>
<Script language = "javascript" src = "Shared % 20 Documents/SPAPI_UserGroup.js"> </script>

<Script language = "JavaScript">

Function getCurrentUserName ()
{
Var curUserName = null;
Var lists = new SPAPI_Lists ('')
Var items = lists. getListItems (
'User Information list ',
'',
'<Query> <Where> <Eq> <FieldRef Name = "ID"/> <Value Type = "Counter">' + _ spUserId + '</Value> </Eq> </Where> </Query> ', // query
'<ViewFields> <FieldRef Name = "Name"/> </ViewFields> ',
1, // rowLimit
''// QueryOptions
);

If (items. status = 200)
{
Var rows = items. responseXML. getElementsByTagName ('z: row ');

If (rows. length = 1)
CurUserName = rows [0]. getAttribute ('oss _ name ');
}

Return curUserName;
}

Function getGroupCollection (userName)
{
Var arrGroup = new Array ();
Var userGroup = new SPAPI_UserGroup ('')
Var groupItems = userGroup. getGroupCollectionFromUser (userName)

If (groupItems. status = 200)
{
Var groupTags = groupItems. responseXML. getElementsByTagName ('group ');
For (var I = 0; I <groupTags. length; I ++)
ArrGroup. push (groupTags [I]. getAttribute ("Name "))
}
Return arrGroup;
}

// Test code
Var userName = getCurrentUserName ();
Var groups = getGroupCollection (userName)

Document. writeln ("UserName:" + userName + "<p/> ")
Document. write ("Groups:" + groups );

</Script>
Deploy the above Code to the Content of the Editor Content Web Part on the SharePoint page.

 

Reprinted from: http://hi.baidu.com/mr_indigo/blog/item/a9196b59e634f688810a1855.html

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.