Share via


How to: Create Memberships

Applies to: SharePoint Server 2010

Microsoft SharePoint Server 2010 supports two types of memberships: Distribution List (DL) memberships and Windows SharePoint Services site memberships. DL membership information is obtained from the Active Directory directory service, and Windows SharePoint Services site membership information is obtained by pulling membership information from the SharePoint site. A user's public My Site page, called the Profile page, displays the user's memberships, as well as memberships the user and the viewing user have in common, among other information.

The User Profile object model adds classes and methods to support the new Memberships feature. The new classes and methods in the User Profiles object model support the following operations:

  • Retrieve user's memberships

  • Set user's membership privacy settings

  • Get common membership between two users

  • Enumerate member groups

  • Extend new membership type, and allow you to add new memberships through the object model

Membership is a first-class object in the User Profiles object model. You can get to the memberships from the UserProfile object.

The MemberGroup object defines a DL or a Windows SharePoint Services site or any other new member group that you create using the object model. It defines a URL property, which represents the archive location (the mailto: link) in the case of a DL, or a Windows SharePoint Services site URL, in the case of the MemberGroup object. The SourceReference property returns the DirectoryEntry of the DL from Active Directory, or the SPWeb or SPSite object depending on the MemberGroup.

The UserProfileManager class adds methods to return member group definitions and create new membership types.

The following example creates a new member group and adds a member to the newly created group. Replace servername, domainname, username, and other placeholders with actual values before using this code example. Also add references to the following in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • Microsoft.SharePoint

  • System.Web

Example

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;

namespace UserProfilesApp
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SPSite site = new SPSite("https://servername"))
            {
                   SPServiceContext context = 
                       SPServiceContext.GetContext(site);
                  UserProfileManager profileManager = 
                      new UserProfileManager(context);
                
                //Create a member group
                 MemberGroup newGroup = 
                     profileManager.GetMemberGroups().
                     CreateMemberGroup(PrivacyPolicyIdConstants.
                     MembershipsFromDistributionLists, 
                     "Customer Connection Team","Customer Connection VTeam","Customer Connection","http://example","abcd");
               
                //Create a membership
                 string sAccount = "domainname\\username";
                UserProfile u = profileManager.GetUserProfile(sAccount);
                u.Memberships.Create(newGroup,
                    MembershipGroupType.UserSpecified, 
                    "Customer Connection Team", Privacy.Organization);
               }
            }

        }
    }

See Also

Tasks

How to: Create User Profiles and Organization Profiles