Thursday, February 24, 2011

How to get SharePoint User by User Id in C#

This is simple.

SPSite site = new SPSite(http://server);
SPWeb myweb = site.OpenWeb();
SPUser found = myweb.AllUsers.GetByID(1);


Jason said...

Simple, but useful.

Thanks for the post!

Melick Rajee said...

You are welcome Jason

Anonymous said...

Will this work for readonly access users?

melick baranasooriya said...

@ Anonymous

Yeh. Though users are readonly they are still users in the site. In Alternatively you can use EnsureUser method by parsing userlogin

Anonymous said...

Do you get users from groups also? or just users added individually to site?

melick baranasooriya said...

This is getting all the users in the site including user in groups but if you have LoginName it is better to use EnsureUser("domain\Loginname")

Anonymous said...

Is there a way to get the current logged in user's username? I have a 3rd party web app that's running in a SharePoint environment and needs to log in automatically using the SharePoint username when the site is accessed.

melick baranasooriya said...

Yeah you can get the logged on user detail by accessing spcontext.current user