Get current sharepoint online logged in user from C#

%3CLINGO-SUB%20id%3D%22lingo-sub-682910%22%20slang%3D%22en-US%22%3EGet%20current%20sharepoint%20online%20logged%20in%20user%20from%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-682910%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20are%20creating%20a%20portal%20site%20using%20sharepoint%20online.%26nbsp%3B%20In%20one%20of%20the%20web%20part%20we%20are%20embedding%20our%20internal%20app%20which%20was%20created%20in%20C%23%20code.%20I%20need%20to%20get%20the%20current%20logged%20in%20sharepoint%20user%20in%20this%20c%23%20code%20to%20do%20the%20data%20filter%20based%20on%20users.%20Have%20tried%20sharepoint%20REST%20API%20but%20it%20gives%20the%20user%20of%20the%20registered%20app.%20Appreciate%20if%20any%20idea%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-685276%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20current%20sharepoint%20online%20logged%20in%20user%20from%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-685276%22%20slang%3D%22en-US%22%3EHi%20Kannan%2C%3CBR%20%2F%3ESharePoint%20Online%20doesn't%20allow%20custom%20C%23%20web%20parts.%20You%20might%20have%20SharePoint%20Framework%20web%20parts%20or%20you%20might%20have%20provided%20hosted%20Add-in.%20Put%20some%20code%20sample%20here%20to%20know%20what%20exactly%20you%20trying%20to%20achieve%20and%20would%20able%20to%20comment.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-686491%22%20slang%3D%22en-US%22%3ERE%3A%20Get%20current%20sharepoint%20online%20logged%20in%20user%20from%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-686491%22%20slang%3D%22en-US%22%3ESo%20presumably%20you're%20talking%20about%20a%20SharePoint%20provider%20hosted%20add-in%2C%20in%20which%20case%2C%20you%20can%20get%20your%20Client%20Context%20object%20(which%20will%20be%20different%20depending%20on%20how%20you%20authenticate)%2C%20then%20access%20the%20Web%20property%2C%20which%20has%20a%20%22Current%20User%22.%20This%20article%20shows%20you%20how%20to%20get%20a%20Client%20Context%20in%20your%20provider%20hosted%20add-in%20and%20access%20the%20Web%20property%20-%20from%20which%20you%20can%20then%20access%20the%20%22Web.CurrentUser%22%20property%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsp-add-ins%2Fget-started-creating-provider-hosted-sharepoint-add-ins%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsp-add-ins%2Fget-started-creating-provider-hosted-sharepoint-add-ins%3C%2FA%3E.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-687173%22%20slang%3D%22en-US%22%3ERe%3A%20Get%20current%20sharepoint%20online%20logged%20in%20user%20from%20C%23%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-687173%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F18230%22%20target%3D%22_blank%22%3E%40Rahul%20Suryawanshi%3C%2FA%3E%26nbsp%3B%20and%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F241707%22%20target%3D%22_blank%22%3EThomas%20Berman%3C%2FA%3E%26nbsp%3Bfor%20your%20time.%20I%20have%20fixed%20this%20by%20creating%20new%20SPFx%26nbsp%3Bweb%20part%20that%20can%20show%20an%20iframe%20and%20pass%20the%20username%20as%20a%20parameter%20to%20the%20embedded%20page.%20This%20solve%20the%20issue.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hi, 

 

We are creating a portal site using sharepoint online.  In one of the web part we are embedding our internal app which was created in C# code. I need to get the current logged in sharepoint user in this c# code to do the data filter based on users. Have tried sharepoint REST API but it gives the user of the registered app. Appreciate if any idea?

 

Thanks. 

3 Replies
Highlighted
Hi Kannan,
SharePoint Online doesn't allow custom C# web parts. You might have SharePoint Framework web parts or you might have provided hosted Add-in. Put some code sample here to know what exactly you trying to achieve and would able to comment.
Highlighted
So presumably you're talking about a SharePoint provider hosted add-in, in which case, you can get your Client Context object (which will be different depending on how you authenticate), then access the Web property, which has a "Current User". This article shows you how to get a Client Context in your provider hosted add-in and access the Web property - from which you can then access the "Web.CurrentUser" property https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-started-creating-provider-hosted-shar....
Highlighted

Thank you @Rahul Suryawanshi  and Thomas Berman for your time. I have fixed this by creating new SPFx web part that can show an iframe and pass the username as a parameter to the embedded page. This solve the issue.