SharePoint Add Users To SharePoint Group

Saturday, December 7, 2013



//Add User in SharePoint Groups
public void addUserToGroup(string groupName, string useName)
{
    string strUrl = "http://MySites:9091/";
    using (SPSite site = new SPSite(strUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            try
            {
                web.AllowUnsafeUpdates = true;
                SPUser spUser = web.EnsureUser[userName];

                if (spUser != null)
                {
                    SPGroup spGroup = web.Groups[groupName];
                    if (spGroup != null)
                    spGroup.AddUser(spUser);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }
    }
}

ensure that user have permission to execute this method.

Helpful Link and Ref:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.ensureuser.aspx
http://msdn.microsoft.com/en-us/library/office/ms414400.aspx
http://gallery.technet.microsoft.com/office/Bulk-remove-Unique-bddd8764

share this post
Share to Facebook Share to Twitter Share to Google+ Share to Stumble Upon Share to Evernote Share to Blogger Share to Email Share to Yahoo Messenger More...

0 comments: