membership requests e-mail address for SPGroup sharepoint

893 Views Asked by At

While creating Group in sharepoint we have an option "Send membership requests to the following e-mail address"

It is used to send membership request to the SPGroup.

But how can we set the e-mail address programmatically

2

There are 2 best solutions below

0
On

I'm trying to accomplish the same thing in a feature activated event. I have found how to create the group and how to access these settings in the object model. You can use my example below. The problem is, my changes to these boolean properties of the SPGroup don't take, despite calling SPGroup.Update(). The SPGroup created still uses the default settings (membership requests are turned off).

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {

        SPSite site = (SPSite)properties.Feature.Parent;
        {
            using (SPWeb web = site.RootWeb)
            {
                SPGroupCollection collGroups = web.SiteGroups;
                SPUser user = web.EnsureUser("DOMAIN\\username");

                collGroups.Add("MySPGroupName", user, user, "MySPGroupDescription");
                if (!web.AssociatedGroups.Contains(collGroups["MySPGroupName"]))
                {
                    web.AssociatedGroups.Add(collGroups["MySPGroupName"]);
                }
                SPRoleAssignment assignment = new SPRoleAssignment(collGroups["MySPGroupName"]);
                SPRoleDefinition def = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                assignment.RoleDefinitionBindings.Add(def);
                web.RoleAssignments.Add(assignment);
                web.Update();
                collGroups["MySPGroupName"].AllowMembersEditMembership = true;
                collGroups["MySPGroupName"].AllowRequestToJoinLeave = true;
                collGroups["MySPGroupName"].OnlyAllowMembersViewMembership = false;
                string emailForRequests = "[email protected]";
                if (!String.IsNullOrEmpty(user.Email))
                    emailForRequests = user.Email;
                collGroups["MySPGroupName"].RequestToJoinLeaveEmailSetting = emailForRequests;
                collGroups["MySPGroupName"].Update();
            }
        }

    }
0
On

If using SP 2013, using PowerShell you can use the following code:

$membersGroup = $siteCollection.SiteGroups["$groupName"]
$membersGroup.RequestToJoinLeaveEmailSetting = "[email protected]"
$membersGroup.Update()