SharePoint Employee Directory web part
One of the frequent requests from my customers in a MOSS 2007 implementation is an employee directory webpart. During the pre-sales cycle they are excited to see the Active Directory integration and the first question they ask is ” Can this replace our existing employee directory out of the box ?”. Unfortunately there are no out of the box web parts at this time to display an employee directory from the user profiles.
Bamboo Solutions(Link) has a great package that provides some of these webparts. However if you don’t need the robust suite from Bamboo, the same can be built using MOSS 2007 Search and the Data View webpart. Let us get started on building the employee directory webpart in this article.
To build an employee directory web part, you need the following
- The free SharePoint Search Query Tool – refer to this article for download instructions
- SharePoint Designer 2007 – Click here to download a free trial
- Information on how to build Data View Web Parts – there is a great white paper here
First, we need to retrieve the properties from the user profile. Since the user profiles are available under the people scope, we will use the SharePoint search query tool to build our people search query. For this article, we will retrieve the “preferredname”, “firstname”. “lastname”, “workemail”,”picturelurl”. Run the Sharepoint search query tool. Since we will be connecting to a MOSS 2007 instance select the MOSS option and enter your server web service URL ( http://<servername>/_vti_bin/search.asmx)
Now we build a SQL search query to retrieve the properties
“select preferredname,firstname,lastname,workemail,workphone,pictureurl,Title,Description,write,Rank,Size, from scope() where “scope” = ‘People’ order by preferredname ASC”
Notice the double quotes around the scope. I am not sure why this is needed but it doesn’t work without the quotes. Enter the number of search results you will need by setting the “Count” field in the query tool. Once the SQL Query is in place click “Build Request XML” . This will build an XML fragment. Once the XML fragment is built, Click on QueryEx to ensure that you are getting the desired results. Remember to click build request XML everytime you make a change to the query.
The screenshot shows a the final results of this exercise. We will need the XML generated by the Build Request XML action. Copy the XML and paste into Notepad and remove all linebreaks in the XML. Ensure that the entire XML is available on a single line within Notepad. Here is how our XML query looks
<QueryPacket xmlns=”urn:Microsoft.Search.Query”>Query> <SupportedFormats><Format>urn:Microsoft.Search.Response.Document:Document</Format></SupportedFormats> <Context> <QueryText type=”MSSQLFT” language=”en-us”>select preferredname, firstname, lastname, workemail, workphone, Title,Path, pictureurl, description, write, rank, size from scope() where “scope”= ‘People’ and firstname != ” order by preferredname asc</QueryText> </Context> <Range> <StartAt>1</StartAt> <Count>1000</Count> </Range> <EnableStemming>true</EnableStemming> <TrimDuplicates>true</TrimDuplicates> <IgnoreAllNoiseQuery>true</IgnoreAllNoiseQuery> <ImplicitAndBehavior>true</ImplicitAndBehavior> <IncludeRelevanceResults>true</IncludeRelevanceResults> <IncludeSpecialTermResults>true</IncludeSpecialTermResults> <IncludeHighConfidenceResults>true</IncludeHighConfidenceResults> </Query></QueryPacket>
If you need to retrieve additional properties, add them to the SQL query. To refer to the fieldnames for the properties, click on
Central Admin -> Shared Service Provider -> User Profiles and Properties -> View Profile Properties
Click Edit on the property that you need and get the property name. In this case I needed the Profile Picture. The name of the property is PictureURL, which is what we need in the query
Now that we have our XML ready, this is time for us to build the web part. We will be using the Data View webpart available within SharePoint Designer for building the UI
The white paper linked in this article above is a must read for building Data View web parts.
First we will create a web part page to host our web part. Click Site Actions-> Create and select web part page to create a new web part page. Here we have used the Full Page Vertical option and named our page “EmployeeDirectory.aspx”. Now that we have our page ready we build the webpart using SharePoint Designer. Open the site within SharePoint designer and navigate to the web part page
Open the site within SharePoint Designer
Once the site is opened, click on the document library where the web part page was created. In our case it is Shared Documents.
Click on the EmployeeDirectory.aspx file to edit
Now we will connect to the MOSS web service and use the XML query to retrieve the user profiles. In the Data Source Library options in the right toolbar, select XML Web services and connect to a web service. Enter the location of the web service, which will be the same as what we entered in the Query tool and click “Connect”. Change the “Operation” to “QueryEx”. All other options can be left at the defaults.
Now we will use the Data View webpart to display these values. Click on the Web Part zone which says “Insert a Web Part” and insert a Data View Web Part by selecting Data View from the menu bar and “insert data view”. Your screen should look like this
The queryXML needs to be modified and our XML fragment from Part-1 needs to be used here. Click on queryXml and click modify and enter the search query from the notepad file. If your query has any line breaks, then your XML will not be copied over correctly. Ensure that your XML ends with a </QueryPacket>. If it does not, then you have some line breaks in your XML fragment. You can now switch to the General tab and set an appropriate name for this connection and set the authentication to Windows in the Login tab.
Click on the connection drop down and click Show Data to view the results of the search query during design time
We can see the fields returned from the web service as well as the data in those fields. There are 115 records returned in this case. Now you can drag and drop these fields on to the data view web part to create your employee directory. The DataView white paper provides information about the different options for formatting and working with the web part. In the code view in SharePoint Designer, search for <XSL>. The entire stylesheet for displaying your data is available within the <XSL></XSL> tags. You can develop the stylesheet externally and include it within these tags. Just ensure that the propertynames in the XSLT tags shown below match the query from the web service.
<xsl:variable name=”fullname” select=”Title” /> <xsl:variable name=”url” select=”Path”/> <xsl:variable name=”email” select=”WorkEmail”/> <xsl:variable name=”pictureurl” select=”PictureURL”/>. In the example above select=”Title” will need to be modified as select=”TITLE” and so on for the other properties.
Our web part is now ready. Save the changes and preview your page.
Here is a great article on how to include the presence information. The presence information is embedded into the XSLT by adding an image with a call to the IMNRC function onload.
ShowOfflinePawn= “1“ />
Click here for the web part. Search for SelectUrl and WsdlPath attributes and replace the links to match your environment
Click here if you just need the XSL.
Any changes to the profile, will be available upon the next indexer crawl, which is set up in the central admin console.
See Gavin’s post below for resizing the image