Showing posts with label SharePoint User Groups. Show all posts
Showing posts with label SharePoint User Groups. Show all posts

Thursday, December 10, 2009

Add User To SharePoint Group Programmatically


Let’s see how we can programmatically add a user to existing SharePoint user group . You can use the following method which has two string parameters, one for group name and other for user login name for get the above task done.
public void addUserToGroup(string groupName, string userLoginName)
{
    string strUrl = "http://mysite:5050/";
    using (SPSite site = new SPSite(strUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            try
            {
                web.AllowUnsafeUpdates = true;
                SPUser spUser = web.EnsureUser[userLoginName];

                if (spUser != null)
                {
                    SPGroup spGroup = web.Groups[groupName];
                    if (spGroup != null)
                    spGroup.AddUser(spUser);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
            }
        }
    }
}
Here you should provide the login name in correct format (DomainName\UserName). For example you can call this method like follow,

addUserToGroup("Test Group", "SA\\saranga.rathnayaka");

Tuesday, December 8, 2009

Add Permission Level To SharePoint User Group


In this post I'm going to discuss how you can add a permission level (Read, Contribute, Full Control, etc) to a SharePoint user group. Here is a sample code,
        public void addPermissionToGroup()
        {
            SPSite site = new SPSite("http://mysite:5050/");
            SPWeb spWeb = site.OpenWeb();
            string permissionName = "Read";
            string groupName = "Project Manager";

            try
            {
                spWeb.AllowUnsafeUpdates = true;
                SPRoleAssignment roleAssignment = new SPRoleAssignment(spWeb.SiteGroups[groupName]);
                roleAssignment.RoleDefinitionBindings.Add(spWeb.RoleDefinitions[permissionName]);

                if (!spWeb.HasUniqueRoleAssignments)
                    spWeb.BreakRoleInheritance(false);

                spWeb.RoleAssignments.Add(roleAssignment);
                spWeb.Update();
            }
            catch (Exception _exception)
            {
                throw _exception;
            }
            finally
            {
                spWeb.AllowUnsafeUpdates = false;
            }
        }
You can view RoleDefinitions defined for your site by visiting to Site Settings --> Advanced Permissions.

Friday, October 2, 2009

SharePoint User Groups

Check User Group in SharePoint

To store and manage permissions we can create User Groups in SharePoint. In this article I’ll discuss how you can check whether the current user is in a particular group using C#. This method will return Boolean value "true" if the user in the given group. You can use this method in your custom webpart.

        public bool isMemberInGroup(string groupName)
        {
            string siteUrl = "http://mysite:5050/";
            SPSite site = new SPSite(siteUrl);
            SPWeb web = site.OpenWeb();

            bool memberInGroup = web.IsCurrentUserMemberOfGroup(web.Groups[groupName].ID);

            return memberInGroup;
        }

If you want to compare the current user in "Person or Group" column type, then you can also get the current user’s user name using  "web.CurrentUser.Name".