Sometimes when we are developing a custom solution, we need to get user info programmatically in SharePoint. It doesn’t matter if you need to show a special message to an specific user using its Personal Name or maybe just fill a SPFieldUser column in a specific list, you’ll probably need one of those code snippets to do the trick.
I’m going to split it into Server-side object model for C# developers, REST API (for SharePoint 2013 projects or newer) and the old-fashioned JavaScript object model introduced in SharePoint 2010. Each one wil let you capture the current user or get an user by its ID or login name. After that it’s up to you what you are going to do with the data.
Server side way to get user info
This code must be used when you are developing some custom Web Parts, Event Receivers or Timer Jobs. It’s only possible because you can reference the SharePoint libraries available inside Microsoft.SharePoint.dll in your projects.
SharePoint REST API way to get user info
The SharePoint REST API was once introduced in the SharePoint 2013 version and kept being improved since then. It helps you a lot when developing solutions using JavaScript because the way it works is easy to learn and standardized.
SharePoint JavaScript Client Object Model way to get user info
The old-fashioned SharePoint JSOM still holds up offering some functionalities only available through this library and working with callback success/fail oriented model.
Well, that’s all for today. Thanks for reading and I hope it can be useful to you when you are trying to get user info programmatically in SharePoint.
References:
Samarendra Swain – SharePoint
MSDN Library
Code Project – SharePoint REST API – Get User Id by User Name
Very nice!
Nice compilation. Thanks
how can I get the same using powershell
Good question Bibhudutta Bhanja! I’ll add those steps in PowerShell in this post later.
Very Nice Will, Thank you for shared!
I´ll add you in my Linked ok?
God Bless you!
I’m glad it was helpful 🙂
And for sure, feel free to add me in LinkedIn as well!