• Javascript
  • Python
  • Go

Displaying the Current Authenticated SharePoint User in an ASP.NET Page Viewer Web Part

SharePoint is a powerful platform used by businesses and organizations to manage and collaborate on documents, projects, and other important...

SharePoint is a powerful platform used by businesses and organizations to manage and collaborate on documents, projects, and other important information. One of the key features of SharePoint is its ability to authenticate users and provide access to specific content based on their permissions. In this article, we will explore how to display the current authenticated SharePoint user in an ASP.NET Page Viewer Web Part.

Before we begin, it is important to understand that the ASP.NET Page Viewer Web Part is a web part used to display an external webpage within a SharePoint site. This makes it a perfect tool for displaying information from other sources, such as a user's information from SharePoint.

To get started, we will need to first create a SharePoint site and add an ASP.NET Page Viewer Web Part to a page. Once the web part is added, we can customize its properties by clicking on the "Edit Web Part" option. In the web part's properties, we will see a section called "Link" where we can enter the URL of the webpage we want to display.

In this case, we will enter the URL of a custom ASP.NET page that we will create to display the current user's information. This page can be hosted on an external server or within the SharePoint site itself. For simplicity, we will host the page within the SharePoint site.

Next, we will need to add some code to our ASP.NET page to retrieve and display the current user's information. We can do this by using the SharePoint object model and the SPContext class. This class provides access to the current context of the SharePoint site, including information about the current user.

First, we will need to add a reference to the Microsoft.SharePoint.dll assembly to our project. This assembly contains the necessary classes and methods to interact with SharePoint. Then, we will add the following code to our page:

<%@ Import Namespace="Microsoft.SharePoint" %>

<%

SPUser user = SPContext.Current.Web.CurrentUser;

Response.Write("Welcome, " + user.Name);

%>

This code will retrieve the current user's information and display their name on the page. We can also access other properties of the user, such as their email address or login name, by using the appropriate methods of the SPUser class.

Once we have saved our ASP.NET page, we can go back to the SharePoint site and refresh the page with the Page Viewer Web Part. We should now see the current user's name displayed on the page.

But what if we want to display more than just the user's name? We can easily do this by using SharePoint's user profile service. This service stores additional information about users, such as their job title, department, and phone number.

To access this information, we will need to make some changes to our code. First, we will need to add a reference to the Microsoft.Office.Server.dll assembly. This assembly contains the necessary classes and methods to interact with the user profile service.

Then, we will modify our code to retrieve the user's profile and display their job title:

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Import Namespace="Microsoft.Office.Server.UserProfiles" %>

<%

SPUser user = SPContext.Current.Web.CurrentUser;

UserProfile userProfile = ProfileLoader.GetProfileLoader(SPServiceContext.Current).GetUserProfile(user.LoginName);

Response.Write("Welcome, " + user.Name + ". Your job title is " + userProfile["Title"].ToString());

%>

By using the user profile service, we can display more personalized information about the current user, making the experience more dynamic and relevant.

In addition to displaying the current user's information, we can also use the ASP.NET Page Viewer Web Part to display other external sources, such as a news feed or a weather forecast. This makes it a versatile tool for customizing and enhancing the user's experience on a SharePoint site.

In conclusion, the ASP.NET Page Viewer Web Part is a useful tool for displaying external information on a SharePoint site. By using the SharePoint object model and the user profile service, we can easily display the current user's information and personalize their experience. This not only adds a personal touch to the site, but also improves its functionality and usefulness.

Related Articles