Tuesday, February 12, 2013

Avoid memory leak issue in custom developed code

Many times we are involved in developing public SharePoint site and customer complaints about site loading slowly. This could be to due memory leakage problem in custom functionality integrated on the SharePoint site.

We all know that SharePoint objects can occupy huge memory. Thus SPDisposeCheck is nice tool that recognize where we forgot to dispose SharePoint objects and helps in avoiding any memory leaks in custom code.

Below is the link to download this tool

Saturday, February 2, 2013

Fetch User Profile Property for logged IN user using SharePoint Object Model

In this post, I will explain how to fetch user profile property for logged IN user using SharePoint object model.

To use user profile API we need to have reference to the Microsoft.Office.Server.UserProfiles. You can refer below code snippet to fetch user profile property

Code snippet
try
   {
     string department;
     string email;
     SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
 
     UserProfileManager upm = new UserProfileManager(serviceContext);
 
     String domainuserName = String.Format(SPContext.Current.Web.CurrentUser.LoginName);
 
     UserProfile up = upm.GetUserProfile(domainuserName);
 
     department = up.GetProfileValueCollection("Department").Value == null ? String.Empty : (String)up.GetProfileValueCollection("Department").Value;
 
     email = up.GetProfileValueCollection("WorkEmail").Value == null ? String.Empty : (String)up.GetProfileValueCollection("WorkEmail").Value;
 
     lbldepartment.Text = department;
     lblemail.Text= email;
    }
    catch (Exception ex)
      {
        lblError.Text = ex.Message;
      }