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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

944 Part IX: Maintaining a Server in Windows SharePoint Services<br />

The following code snippet demonstrates how to import user profiles from a<br />

text file. It assumes that each property is delimited by a colon (:) character. Although<br />

this is a simple example, it can easily be modified to provide user profile imports for<br />

any other type of directory service, be it a database, a Lightweight Directory Access<br />

Protocol (LDAP) service, or a corporate directory.<br />

C#<br />

public static void Import (string filename)<br />

{<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 />

//Open Text File containing user information<br />

System.IO.FileInfo file = new FileInfo(filename);<br />

System.IO.StreamReader fs = file.OpenText();<br />

string read;<br />

while ((read = fs.ReadLine()) != null)<br />

{<br />

//parse line<br />

string[] userInfo = read.Split(":".ToCharArray());<br />

//create user profile if it doesn't exist<br />

string userAccount = userInfo[0];<br />

if (!profileManager.UserExists(userAccount))<br />

profileManager.CreateUserProfile(userAccount);<br />

//Get the userprofile so it can be changed<br />

UserProfile userProfile = profileManager.GetUserProfile(userAccount);<br />

// Set users Preferred name to their first + last name.<br />

userProfile["PreferredName"] = userInfo[2] + " " + userInfo[1];<br />

userProfile["WorkEmail"] = userInfo[0] + "@domain.com";<br />

userProfile.Commit();<br />

}<br />

fs.Close();<br />

}<br />

VB.NET<br />

Public Shared Sub Import(filename As String)<br />

Dim topology As New TopologyManager()<br />

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

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

'initialize user profile config manager object<br />

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

'Open Text File containing user information<br />

Dim file = New FileInfo(filename)<br />

Dim fs As System.IO.StreamReader = file.OpenText()<br />

Dim read As String<br />

read = fs.ReadLine()<br />

While Not (read Is Nothing)<br />

'parse line<br />

Dim userInfo As String() = read.Split(":".ToCharArray())

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

Saved successfully!

Ooh no, something went wrong!