Raghu's SharePoint Corner: October 2008

My blog has moved!

You will be automatically redirected to the new address. If that does not occur, visit
http://www.sharepointcolumn.com
and update your bookmarks.

October 8, 2008

Iterating UserProfiles in Shared Service Provider (SSP)

Iterating UserProfiles in Shared Service Provider (SSP)

This article deals with retrieving user profiles of all the user that exists in shared service provider. There may be many scenarios where you need to iterate through all the userprofiles that exist in ssp. There is no such method available out of the box in MOSS where you can get list of user in Shared Service Provider. In order to iterate each profile in shared service provider we need to use Ienumerators

I had a scenario where I need to activate features on all the mysites that exist. In order to activate feature on individuals mysite, I first need to check whether the user has a mysite and then activate the feature.

When you are trying to do this you may come across some Permission and Access Denied exception. Here is the links which will help you to solve the issue

http://edinkapic.blogspot.com/2007/08/enumerating-user-profiles.html

You can retrieve users from SSP by using web services. The below code iterates a list of user from SSP

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Collections;
using System.IO;

using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
public namespace SSPUsers
{
public class Program
{
static void Main(string[] args)
{
using (SPSite searchSite = new SPSite("http://sspsiteurl"))
{
UserProfile currentProfile;
bool checkPersonalSite;

// Use the ServerContext to get the SSP associated to the site collection of interest

ServerContext context = ServerContext.GetContext(searchSite);

// Get the user profile enumerator for the targeted SSP

UserProfileManager profileManager = new UserProfileManager(context);

long count = profileManager.Count;

IEnumerator enumerator = profileManager.GetEnumerator();

bool continueEnum = true;

while (continueEnum)

{

try

{

continueEnum = enumerator.MoveNext();

}

catch (Exception e)

{

Console.WriteLine("EXCEPTION in enum - try to move to next: " + e.ToString());

continueEnum = enumerator.MoveNext();

}

currentProfile = (UserProfile)enumerator.Current;

}

}

}

The currentProfile object will give you access to all the properties of a user availabel in the ssp

How to Retreive My Sharepoint Sites Links in a Publishing Site

The main theme of this article is to retrive the list of sites to which a user belongs. In a publishing sharepoint site , you have a hyperlink called “My Links”at the top right side of the site, when you click on this link you will get a drop down menu for “My Sharepoint Site”. If you are not getting this then you have not been added to the sites member group. After verifying if you find that you exist in a Sites member group and still the My Sharepoint Sites links are not getting populated in that case wait for few hours or a day. Microsoft has a time job which updates a users My Sharepoint Sites.

I wanted to create a custom menu where I can show a list of site to which a user belongs. There are 2 different approach

1 Is to loop through site collection and check the user membership and permission, if he his authorized then store it in some array list

2.Is try to find out how Microsoft is able to populate My Sharepoint List and follow the same.

I searched a lot on this but found very few article. Paul Liberand as a written a very good blog on this

http://liebrand.wordpress.com/2007/11/29/moss-2007-and-user-memberships/

The Approach which I am discussing is similar.I createa a stored procedure to retrive a list of site to which a user belongs. This stored procedure will be placed in a content database of Shared Service Provider on which the site is hosted

CREATE PROCEDURE [dbo].[proc_MySharePointSites]
(
@userName VarChar(400)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @RecordId int
SELECT @RecordId = RecordId FROM dbo.UserProfile_Full WHERE NTNAME = @userName;

EXEC dbo.QuickLinksRetrieveAllItems @RecordId,@ViewerItemSecurity=31,@RequestedItemSecurity=16

END

QuickLinksRetrieveAllItems: It’s a stored procedure defined by microsoft which retrives Quick Links For My Sharepoint Sites. The main input parameter for this stored procedure is the RecordID which is unique for a user in a ssp(please correct me if I am wrong)

dbo.UserProfile_Full : This table consist of list of use in a ssp. You can retrieve a Record ID of any user from dbo.UserProfile_Full table by passing email id, login name or domain name of a user. Once you get a RecrodId you can pass this to the stored procedure proc_MySharePointSites create above and you can easily retrieve a list of a site to which a user belongs.

You can locate the table dbo.UserProfile_Full and the store QuickLinksRetrieveAllItems in the content database of a ssp, which you can open through SQL Server 2005 Management Studio

You can execute the stored procedure and get the list of site to which a user belongs programmatic ally

This is a workaround but not a right approach has it deal with SSP Content database