Tuesday, April 24, 2012

SharePoint My Site Analysis Report - A Practial Business Scenario

Call from my CIO: "Salaudeen, I need the SharePoint 'My Site' Report with Department wise users along with their other details such as E-Mail."

Well, E-Mail I can retrieve by querying Site Owner object, How about Department? Ah, Its available in Profile DB. Let me query that.

Here is my code:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using System.Web;

namespace SharePoint.AdminReports
{
    class MySiteAnalysis
    {
        static void Main(string[] args)
        {

            //Get the My Site web application URL
            string site;
            string WorkEmail;
            if (args.Length == 0)
            {
                Console.WriteLine("Enter the My site URL:");
                site = Console.ReadLine();
            }
            else
            {
                site = args[0];
            }

            //objects for the CSV file generation
            StreamWriter SW;
            SW = File.AppendText("c:\\SiteOwnersReport.csv");
            //Write the CSV Header
            SW.WriteLine("Site URL, Owner Name, E-Mail, Department");

            try
            {
                SPSite tmpRoot = new SPSite(site);
                SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites;

                //Enumerate through each site collection
                foreach (SPSite tmpSite in tmpRootColl)
                {
                    try
                    {
                        WorkEmail = tmpSite.Owner.Email;
                    }
                    catch (NullReferenceException ex1)
                    {
                        WorkEmail = string.Empty;
                    }

                    SW.WriteLine(tmpSite.Url + "," + tmpSite.Owner.LoginName + "," + WorkEmail + "," + GetDepartment(site, tmpSite.Owner.LoginName));
                }
                //Dispose the objects
                SW.Close();
                SW.Dispose();
                tmpRoot.Dispose();

            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Site Owner Report", ex.Message);
            } 
            }


        //Method to Get the department of the Site owner
        private static string GetDepartment(string siteURL, string loginName)
        {
            string Department;
            try
            {
                
                using (SPSite site = new SPSite(siteURL))
                {
                    // if the Profile not found in Profile store, SharePoint will throw: UserNotFoundException exception
                    try
                    {
                        ServerContext context = ServerContext.GetContext(site);
                        UserProfileManager profileManager = new UserProfileManager(context);
                        UserProfile profile = profileManager.GetUserProfile(loginName);

                        //Department may or May not be associated.
                        try
                        {
                            Department = profile["Department"].Value.ToString();
                        }
                        catch (NullReferenceException ex2)
                        {
                            Department = string.Empty;
                        }
                    }
                    catch (UserNotFoundException ex3)
                    {
                        Department = "** User not Found **";
                    }
                }
            }
            catch (Exception ex4)
            {
                Department = string.Empty;
            }
            return Department;
        }
    }
} 
 
and the code brings the Report in CSV format:
SharePoint My Site Analysis

By the way, This report also gives "Orphaned Mysites" information also. Since I've disabled "Site usage confirmation and deletion" feature, it resulted orphaned my sites.

Tail: 
In case you don't have dedicated web application for My Site (which is bad, in-fact) you can check the "WebTemplate" Property of SPWeb object. My Site's web template is: "SPSPERS"



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

1 comment :

  1. Thanks Salaudeen, this was very informative, Please keep posting

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...