When new users are added to our MOSS sites, we wanted to make sure that they go into the correct Audience as soon as possible. Since the ‘Manage Audiences' functionality of the Shared Services Administration allows for daily updates only, we needed a more granular way to re-compile our defined audiences. We ended up writing a small application in order to do this.
The first thing we had to do was to make sure that our Profiles were up to date. If new users had been added, but their information had not been downloaded, compiling the audiences wouldn't do any good. The code snippet below illustrates how we update the profiles.
Dim site As Microsoft.SharePoint.SPSite = Nothing
Dim SPcontext As Microsoft.Office.Server.ServerContext
Dim UPCM As Microsoft.Office.Server.UserProfiles.UserProfileConfigManager
Try
site = New Microsoft.SharePoint.SPSite("http://server/site")
SPcontext = Microsoft.Office.Server.ServerContext.GetContext(site)
UPCM = New Microsoft.Office.Server.UserProfiles.UserProfileConfigManager(SPcontext) Dim MaxStatusLoops As Integer = 0
If UPCM.GetImportStatus.CrawlStatus = Microsoft.Office.Server.Search.Administration.CrawlStatus.Idle Then
Dim LastCrawlTime As Date = UPCM.GetImportStatus.LastCrawlTime
' Run incremental import
UPCM.StartImport(True)
Do While UPCM.GetImportStatus.LastCrawlTime = LastCrawlTime And _
UPCM.GetImportStatus.CrawlStatus = Microsoft.Office.Server.Search.Administration.CrawlStatus.Idle
MaxStatusLoops += 1
If MaxStatusLoops > UPTimeout Then
Throw New ApplicationException("UserProfileImport crawl failed to start.")
End If
' 1 second pause
System.Threading.Thread.Sleep(1000)
Loop
End If
' Wait for crawl to complete
MaxStatusLoops = 0
Do While UPCM.GetImportStatus.CrawlStatus = _
Microsoft.Office.Server.Search.Administration.CrawlStatus.CrawlingIncremental
MaxStatusLoops += 1
If MaxStatusLoops > UPTimeout Then
Throw New ApplicationException("Waiting for user profile import timed out.")
End If
System.Threading.Thread.Sleep(1000)
Loop
Catch ex As Exception
LogError("Error importing user profiles.", ex)
Finally
If Not IsNothing(site) Then
site.Dispose()
End If
End Try
After importing the profiles, we needed to compile the audiences. I came across the RunAudienceJob method of the AudienceJob class in the SharePoint SDK. As typical, the documentation left a lot ‘as an exercise for the reader'. The least documented part (and most important part) of this method is the ‘args' parameter. The documentation is clear enough on the 2nd through 4th elements, but the first element is just described as 'ApplicationID'. After spending a couple of days unsuccessfully passing in a variety of IDs (site.ID, web.ID, webapplication.ID), I discovered that what was needed was stored in the name property of the Search Context.
We used the code below, in a timed job, to update the audiences.
<Kevin Koehne/>
Dim site As Microsoft.SharePoint.SPSite = Nothing
Dim SPcontext As Microsoft.Office.Server.ServerContext
Dim am As Microsoft.Office.Server.Audience.AudienceManager
Dim searchContext As Microsoft.Office.Server.Search.Administration.SearchContext Try
site = New Microsoft.SharePoint.SPSite("http://server/site")
SPcontext = Microsoft.Office.Server.ServerContext.GetContext(site)
searchContext = Microsoft.Office.Server.Search.Administration.SearchContext.GetContext(site)
am = New Microsoft.Office.Server.Audience.AudienceManager(SPcontext)
Dim args(2) As String
args(0) = searchContext.Name
args(1) = "1" ' 1=Start compile, 0=Stop
args(2) = "1" ' 1=Full, 0=Incremental
'args(3) = "audience name"
Dim RunJob As Integer = Microsoft.Office.Server.Audience.AudienceJob.RunAudienceJob(args)Catch ex As Exception
LogError("Error compiling audience.", ex)
Finally
If Not IsNothing(site) Then
site.Dispose()
End If
End Try
Note: This code is provided as-is and may not work the same way in your environment. Use at your own risk.
Posted
05-02-2007 6:24 PM
by
wkkf