SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i have a list where based on users area and code there is a group name associated.i need to get the users input of area and areacode, check it from the list and find the appropriate group name and then send the mail to the owners group asking for permissions to the particular group. if granted then the users will be added to the group.

pls give me ur inputs on the best way of doing this..

Thanks in advance

Regards Nishanth

share|improve this question

Looking to the title of the post you need the following code :

public void AddUserToSharePointGroup(string groupName, string userLoginName)
    string sitePath = new "http://localhost/";
    using (SPSite site = new SPSite(sitePath))
        using (SPWeb web = site.OpenWeb())
                web.AllowUnsafeUpdates = true;
                SPUser user = web.EnsureUser[userLoginName];

                if (user != null)
                    SPGroup group = web.Groups[groupName];
                    if (group != null)
            catch (Exception ex){
                web.AllowUnsafeUpdates = false;
share|improve this answer
thank u..i am actually looking for an oob way to do this as much as possible. – Nishanth Jan 18 '13 at 6:28
You could find a workflow automation product that will do such things for you, but out of the box adding users to groups based on some business logic is not available. – Alexey Krasheninnikov Jan 18 '13 at 7:46
Hi all, i am not able to get an welcome email when i add users to the group using above method.pls guide me on how to acheive this – Nishanth Jan 28 '13 at 12:46
up vote 0 down vote accepted

AddUser method doesn't contain send email functionality.we can use sputility.sendemail to configure the mail.the following code will help.

string groupval = sitePath + "/_layouts/people.aspx?MembershipGroupId=" + SPHttpUtility.UrlKeyValueEncode(group.ID);

                if (group != null)
               StringDictionary headers = new StringDictionary();
                headers.Add("to", "");
               headers.Add("from", "");
                headers.Add("subject", "Welcome to the SharePoint group: " + group + " for site: "+web.Title+"");
                headers.Add("content-type", "text/html"); //This is the default type, so isn't neccessary.
                string bodyText = "Welcome to the '" + groupName + "' SharePoint group. " +group.Owner +
                    "has added you to the '"+groupName+"' group for this SharePoint site.<br/><br/>As a member of this SharePoint group, you can:<br/>* Participate in the SharePoint site at:<a href=" + site.RootWeb.Url + ">" + site.RootWeb.Url + "</a>" +
                   "<br/>* View the group home page at:<a href=" + groupval + ">" + groupval + "</a>"+"<br/><br/>What is a SharePoint site?"+"<br/>A SharePoint site is a Web site that provides a central storage and collaboration space for documents, information, and ideas. A SharePoint site is a tool for collaboration, just like a telephone is a tool for communication, or a meeting is a tool for decision making. A SharePoint site helps groups of people (whether work teams or social groups) share information and work together. For example, a SharePoint site can help you: <br/>"+
                   "<br/>* Coordinate projects, calendars, and schedules.<br/>* Discuss ideas and review documents or proposals.<br/>* Share information and keep in touch with other people.<br/><br/>SharePoint sites are dynamic and interactive -- members of the site can contribute their own ideas and content as well as comment on or contribute to other people's. "; 

                SPUtility.SendEmail(web, headers, bodyText);

This code will give the exact mail as out of the box

Regards Nishanth

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.