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 Wes MacDonald

Wes MacDonald is a DevOps Consultant for LIKE 10 INC., a DevOps consulting firm providing premium support, guidance and services for Azure, Microsoft 365 and Azure DevOps.

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: