Thursday, 14 May 2015

Get Current User Information from a provider hosted app



In order to get current user information for a provider-hosted app, you need to write the following method in the web application part of the provider-hosted app. The method is written below:




private User GetCurrentUserInfo(SharePointContext context)
        {
            try
            {
                using (var clientContext = context.CreateUserClientContextForSPHost())
                {
                    var currentUser = clientContext.Web.CurrentUser;
                    clientContext.Load(currentUser);
                    clientContext.ExecuteQuery();
                    return currentUser;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return null;
        }



The returned  object currenUser contains all the users infomation like login name, email etc.

No comments:

Post a Comment