Wednesday, 5 September 2012

Add the user to a Group

SPSite site = new SPSite(@"http://madhanpc:8089");
        SPWeb web = site.OpenWeb();
        web.AllowUnsafeUpdates = true;
        //Get the group that we want to add the user
        SPGroup grp = web.Groups["Viewers"];
        try
        {
            //Get the user that we want to add to the group
            SPUser usr = web.AllUsers[@"madhanpc\emp1"];
            //Now we add the user to the group collection and update
            grp.AddUser(usr);
            grp.Update();
            Response.Write("User is added successfully..");
        }
        catch (Exception ex)
        {
            Response.Write("User is not added " + ex.Message);
        }

No comments:

Post a Comment