SharePoint 2007 and working with users stored in Membership Providers


I recently had a requirement where a user had to be added to a group as part of a SharePoint 2007 Workflow but the users accounts were stored in a SQLMembershipProvider and LDAPMembershipProvider as opposed to a Windows Domain.

The trick is you have to append the name of your provider to the users login for SharePoint to recognize the account.  Normally you would use the DOMAIN\LOGIN syntax for users belonging to the AD.

string _userAccount = Membership.GetUser().UserName;
string _groupName = “Members”;
string _providerName = Membership.GetUser().ProviderName;
string _usernameWithProvider = String.Format("{0}:{1}", _providerName, _userAccount);

using (SPWeb currentTeamSite = workflowProperties.Web)
{
  SPGroup spGroup = currentTeamSite.SiteGroups[_groupName];
  spGroup.AddUser(_usernameWithProvider, Membership.GetUser().Email, Membership.GetUser().Email, "Added to group via SharePoint Workflow");
}

You do the same thing for subscribing to Alerts as well:

SPList list = currentTeamSite .Lists[0];
SPUser spUser = currentTeamSite .Users[_usernameWithProvider];
spUser.Alerts.Add(list, SPEventType.All, SPAlertFrequency.Immediate);

 

About wesmacdonald

Wes MacDonald is a Visual Studio ALM MVP, PSD, MCT and a Principal Consultant for LIKE 10 INC., a SharePoint Server, Visual Studio and Team Foundation Server Consulting Firm providing premium support and guidance services.

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: