Thursday, May 31, 2012

Creating My Site in SharePoint 2010


This article describes how to set up My Site in SharePoint 2010 environment. There are some pre-requirements to configure My Site. User Profile Service Application is required to set up My Site. Managed Meta Data Service Application and Search Service Application are optional. The Profile Synchronization is important to synch the data from active directory to SharePoint.

You can use existing web application to set up My Site. But Microsoft recommends creating a dedicated web application for optimal performance and security.

Follow the following steps to create a web application:
1.       Open central administration and click manage web Applications link 
2.       Click new from the ribbon; complete the Create New Web Application dialog box to create a new web application.

To create My Site site collection: 
1.       Go to Application Management from the Central Administration. 
2.       Click create site collections. 
3.       In the Create Site Collection page select the correct web application. 
4.       Give the site collection a name and the description. 
5.       Choose My Site Host template under Enterprise tab. 
 
6.       Provide username for primary/secondary site collection administrators. 
7.       Leave the quota option default. 
8.       Click ok

Configuring Managed Paths:
Now you are ready to configure managed paths. 
1.       Go to Application Management. 
2.       Click Manage web applications link 
3.       Click to the right of the My Site host application 
4.       From the ribbon click Managed Paths icon. 
5.       Add two managed paths: personal as wildcard and my as explicit.

Now you should have a working My Site host application and root site collection. You also have a managed path to host all user site collection (personal) and the host application path (my).
There are some more steps to complete. 
1.       Go to Application Management, Manage Service Application. 
2.       Click to the right of the User Profile Service Application. 
3.       Click Manage icon in the ribbon. 
4.       Under My Site Settings, click Setup My Sites. 
5.       Set My Site host location to http://hostapplication:port/. This is the location of the My Site host application and contain pages to show profile information. Ex: http://bll-shpdev-01:7879/ 
6.       Set up Personal site location to ‘personal’. This is the location where all personal My Site belongs. Ex: http://bll-shpdev-01:7879/personal/t-ictdev05 
7.       Under Newsfeed, select Enable newsfeed on My Sites.

Once you complete above steps, now you can start My Sites. The easiest way to test this is go to any site collection, click the name of the logged in user in the top right. In the drop down select My Site.

Friday, May 11, 2012

Retrieve user’s photo from Active directory in ASP.NET

This article describes how to get the user’s photo from Active directory and display in to an aspx page. I’m creating a new ASP web application and the users photo will be displayed in Default.aspx page.  To do this you need to include 
System.DirectoryServices,  
System.DirectoryServices.AccountManagement           

assemblies to the project references.


Include following using statements in Default.aspx.cs page:


using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
 
The following method gets the users photo from the AD and return as a Bitmap.

public static Bitmap GetThumbnailPhoto(string userName)
        {
            using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain))
            {
                using (UserPrincipal userPrincipal = new UserPrincipal(principalContext))
                {
                    userPrincipal.SamAccountName = userName;
                    using (PrincipalSearcher principalSearcher = new PrincipalSearcher())
                    {
                        principalSearcher.QueryFilter = userPrincipal;
                        Principal principal = principalSearcher.FindOne();
                        if (principal!=null)
                        {
                            DirectoryEntry directoryEntry = (DirectoryEntry)principal.GetUnderlyingObject();
                            PropertyValueCollection collection = directoryEntry.Properties["thumbnailPhoto"];

                            if (collection.Value!=null && collection.Value is byte[])
                            {
                                byte[] thumbnailInBytes = (byte[])collection.Value;
                                return new Bitmap(new MemoryStream(thumbnailInBytes));
                            }                           
                        }
                        return null;
                    }
                }
            }
        }

thumbnailPhoto is the property returns the user’s thumbnail photo from the AD.


The above method gives the user's photo as bitmap. Next we have to display the image in the Default.aspx page. Add asp image control to the page.


<asp:Image ID="imbThumbnail" runat="server" />


At the Page_Load event add the image to the image control.


protected void Page_Load(object sender, EventArgs e)
        {
            Bitmap thumbnail = GetThumbnailPhoto("t-ictdev05");

            String saveImagePath = Server.MapPath("~/Images/image.jpg");
            thumbnail.Save(saveImagePath, ImageFormat.Jpeg);
            imbThumbnail.ImageUrl = "~/Images/image.jpg";
        }
 
To set the ImageUrl, I am saving the image to Images/image.jpg and displaying the photo.