16.01.2013 Views

Microsoft Sharepoint Products and Technologies Resource Kit eBook

Microsoft Sharepoint Products and Technologies Resource Kit eBook

Microsoft Sharepoint Products and Technologies Resource Kit eBook

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 34: The SharePoint Portal Server Object Model 947<br />

This final example will create a report which displays all users in the profile<br />

database <strong>and</strong> the URL of their My Site. It will display “None” for the URL if the user<br />

does not have a My Site. It will also display the total number of users who have<br />

already created a My Site. This report could easily be modified to calculate the size of<br />

each user’s My Site <strong>and</strong> the total size of all My Sites, among other useful information.<br />

C#<br />

public static void Report()<br />

{<br />

int usersWithProfiles = 0, usersWithoutProfiles = 0;<br />

//Connect to the portal <strong>and</strong> get the portal context.<br />

TopologyManager topology = new TopologyManager();<br />

PortalSite portal = topology.PortalSites[new Uri("http://test-server")];<br />

PortalContext context = PortalApplication.GetContext(portal);<br />

//initialize user profile config manager object<br />

UserProfileManager profileManager = new UserProfileManager(context);<br />

foreach (UserProfile profile in profileManager)<br />

{<br />

<strong>Microsoft</strong>.SharePoint.SPSite personalSite = profile.PersonalSite;<br />

if (personalSite == null)<br />

{<br />

Console.WriteLine(profile["PreferredName"] + " : " + "NONE");<br />

usersWithoutProfiles++;<br />

}<br />

else<br />

{<br />

Console.WriteLine(profile["PreferredName"] + " : " + personalSite.Url);<br />

usersWithProfiles++;<br />

}<br />

}<br />

Console.WriteLine("Users with Personal Site: " + usersWithProfiles.ToString());<br />

Console.WriteLine("Users without Personal Sites: " + usersWithoutProfiles.ToString());<br />

}<br />

VB.NET<br />

Public Shared Sub Report()<br />

Dim usersWithProfiles As Integer = 0<br />

Dim usersWithoutProfiles As Integer = 0<br />

'Connect to the portal <strong>and</strong> get the portal context.<br />

Dim topology As New TopologyManager()<br />

Dim portal As PortalSite = topology.PortalSites(New Uri("http://test-server"))<br />

Dim context As PortalContext = PortalApplication.GetContext(portal)<br />

'initialize user profile config manager object<br />

Dim profileManager As New UserProfileManager(context)<br />

Dim profile As UserProfile<br />

For Each profile In profileManager<br />

Dim personalSite As <strong>Microsoft</strong>.SharePoint.SPSite = profile.PersonalSite<br />

If personalSite Is Nothing Then<br />

Console.WriteLine((profile("PreferredName") + " : " + "NONE"))<br />

usersWithoutProfiles += 1<br />

Else<br />

Console.WriteLine((profile("PreferredName") + " : " + personalSite.Url))<br />

usersWithProfiles += 1<br />

End If

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!