UserProfile WebPart Part 2


Sorry for taking it too long to start over with Part 2..was really held up with so many other things…

Part 5:In Part 1, we saw till the place of adding a new Visual Web Part. Now the coding part.. You can design your tables and image box in ascx design page. It looks like the below image

The code for the front end uses a panel with a table inside. I have just used one image box and one label to specify the name of the user. You can design it in anyway you want. The requirement of the project is to show up the user’s photo and his name beneath.

Now for the code behind, open up UserPhotoUserControl.ascx.cs file and write your code in the page load event. Before that as mentioned earlier, refer to the directives required. Here is the full length code.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace UserProfilePhoto.UserPhoto
{
public partial class UserPhotoUserControl : UserControl
{
SPWeb _web = null;
protected void Page_Load(object sender, EventArgs e)
{
try
{
_web = SPContext.Current.Web;
using (SPSite siteColl = new SPSite(SPContext.Current.Site.Url))
{
string userName = “NA”;
string strAccountName = “”;
SPContext currentContext;
currentContext = SPContext.Current;
userName = SPContext.Current.Web.CurrentUser.Name;
strAccountName = SPContext.Current.Web.CurrentUser.LoginName;
SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
UserProfileManager userProfileManager = new UserProfileManager(serviceContext);
Microsoft.Office.Server.UserProfiles.UserProfile profile = null;
bool existingUser = userProfileManager.UserExists(strAccountName);
if (existingUser)
{
profile = userProfileManager.GetUserProfile(strAccountName);
}

imgUser.ImageUrl = profile[PropertyConstants.PictureUrl].Value.ToString();
lblName.Text = userName;
}
}
catch (Exception ex)
{
Error.Text = ex.Message;
}
}
}
}

And thats it! Build the code and click on Deploy… Check the website where you had deployed this webpart. It will be enabled automatically and when you try to add in a new webpart, this will be listed under custom category. Add and Voila…you see this…:)

Advertisements

About gai3kannan

I am working with NCS Pte Ltd as a Lead Consultant I have taken SharePoint as my full time action and thoughts.
This entry was posted in My Own Posts, SharePoint 2010, Visual Studio 2010 and tagged , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s