How to verify the logged in user using Javascript in sharepoint
Asked Answered
B

3

6

I need to check whether the logged in user is a administrator or non-admin user by using JavaScript in sharepoint designer or content editor webpart.

Can anybody suggest?

Betsey answered 10/1, 2012 at 10:0 Comment(0)
T
2

You can use JQuery and SPServices to examine the group a logged in user belongs to, see my post here which explains the principle. In that case, it hides an element if a user belongs to a particular group.

Tengler answered 13/1, 2012 at 17:55 Comment(0)
C
6

You can use the HasRights javascript function defined in SharePoints core.js, you just have to pass the lower (limit) permission mask as well as the upper limit.

See the post about Checking Permissions with Javascript in Sharepoint 2007 and the list of permission masks. As you want to check whether it is an administrator you could try the ManageWeb permission mask: 0x0000000040000000


Edit: Another possibility is not using the HasRights function, but a SPSecurityTrimmedControl to simply set a global Javascript variable. An example you can find here: SharePoint: Run JavaScript based on user rights / permissions. This of course means extra markup (and doesn't work in a content editor webpart), but it does work.
Caster answered 10/1, 2012 at 11:29 Comment(4)
Thanks for the links.. I want to verify the user in newform.aspx of a servey list. Can we use the HasRights method in that page?.. If yes then what are the parameters I have to pass ti itBetsey
I don't have a SharePOint 2007 availalbe, so I can't test. As long as the core.js is loaded you can use HasRights. The parameters are explained in the links I posted.Caster
I just added the below script in my content editor webpart. No result. Can you check the code and let me know what needs to be done. I checked the HasRights is defined or not in core.jas, it is not defined in it. <script language="javascript" type="text/javascript"> if(HasRights(0x0, 0x0000000040000000)) { alert("You have admin rights."); } </script>Betsey
This seems to always return true for me.. even when I don't have permission. I do see it in core.js though.Hartzel
T
2

You can use JQuery and SPServices to examine the group a logged in user belongs to, see my post here which explains the principle. In that case, it hides an element if a user belongs to a particular group.

Tengler answered 13/1, 2012 at 17:55 Comment(0)
S
-1

On the web part, after you edit it there is a a field called target audience. You can only put groups here so put the user in group first.

Sideslip answered 8/3, 2018 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.