Friday, 3 July 2015

Create Group, Add/Remove Permission To Group,Add User in Group programmatically




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace AddGroupAndPermission
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbGroupName.Text) || string.IsNullOrEmpty(txtUserName.Text))
            {
                lblMsg.Text = "Please Enter Some Values";
                lblMsg.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                using (SPSite oSPsite = new SPSite(txtUrl.Text))
                {
                    using (SPWeb oSPWeb = oSPsite.OpenWeb())
                    {
                        //oSPWeb.AllowUnsafeUpdates = true;

                        SPGroup group = oSPWeb.Groups[cbGroupName.Text];
                        SPUser user = oSPWeb.EnsureUser(txtUserName.Text);
                        group.AddUser(user.LoginName, user.Email, user.Name, user.Notes);
                        oSPWeb.Update();
                        lblMsg.Text = txtUserName.Text + " - User Added to Group Successfully";
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        cbMyRole.Text = string.Empty;
                        txtUserName.Text = string.Empty;
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //bindGroup();
        }

        public void bindGroup()
        {
            string strGrpName, strGrpID;
            using (SPSite oSPsite = new SPSite(txtUrl.Text))
            {
                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                {
                    SPGroupCollection oGroups = oSPWeb.SiteGroups;

                    foreach (SPGroup oGroupsInner in oGroups)
                    {
                        strGrpID = oGroupsInner.ID.ToString();
                        strGrpName = oGroupsInner.Name;
                        cbGroupName.Items.Add(strGrpName);
                        //strGrpDesc = oGroupsInner.Description;
                  }

                    //cbGroupName.DataSource = oGroups;
                    //    cbGroupName.Text="Name";

                 
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindGroup();
            bindList();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbGroupName.Text) || string.IsNullOrEmpty(cbMyRole.Text))
            {
                lblMsg.Text = "Please Enter Some Values";
                lblMsg.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                using (SPSite oSPsite = new SPSite(txtUrl.Text))
                {
                    using (SPWeb oSPWeb = oSPsite.OpenWeb())
                    {
                        //oSPWeb.AllowUnsafeUpdates = true;
                        SPGroup group = oSPWeb.SiteGroups[cbGroupName.Text];
                        SPRoleDefinition roleDefinition;
                        roleDefinition = oSPWeb.RoleDefinitions[cbMyRole.Text];

                        SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
                        roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

                        //add role to web
                        oSPWeb.RoleAssignments.Add(roleAssignment);
                     
                        oSPWeb.Update();

                        lblMsg.Text = cbMyRole.Text + " - Role Added to Group Successfully";
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtNewGroup.Text))
            {
                lblMsg.Text = "Please Enter Some Values";
                lblMsg.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                using (SPSite oSPsite = new SPSite(txtUrl.Text))
                {
                    using (SPWeb oSPWeb = oSPsite.OpenWeb())
                    {
                        //oSPWeb.AllowUnsafeUpdates = true;
                        oSPWeb.SiteGroups.Add(txtNewGroup.Text, oSPWeb.CurrentUser, oSPWeb.CurrentUser, txtNewGroup.Text);
                        //oSPWeb.Groups.Add(groupname, checkehat , checkwhat , groupdescription);
                        oSPWeb.Update();
                        lblMsg.Text = txtNewGroup.Text + " - Group Created Successfully";
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        txtNewGroup.Text = string.Empty;
                        txtNewGroup.Text = string.Empty;
                    }
                }
            }
        }

        private void btnRemovePermission_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cbGroupName.Text) || string.IsNullOrEmpty(cbList.Text))
            {
                lblMsg.Text = "Please Enter Some Values";
                lblMsg.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                using (SPSite oSPsite = new SPSite(txtUrl.Text))
                {
                    using (SPWeb oSPWeb = oSPsite.OpenWeb())
                    {
                       // SPWeb web = SPContext.Current.Web;
                        SPGroup group = oSPWeb.SiteGroups[cbGroupName.Text];
                        SPPrincipal principal = (SPPrincipal)group;
                        SPRoleAssignment rAssignment = new SPRoleAssignment(group);
                        SPList list = oSPWeb.Lists[cbList.Text];
                        list.BreakRoleInheritance(true);
                        list.RoleAssignments.Remove(principal);
                        list.Update();
                        lblMsg.Text = cbGroupName.Text + " - Group Removed Successfully for " + cbList.Text + " List";
                        lblMsg.ForeColor = System.Drawing.Color.Green;
                        lblMsg.Text = string.Empty;
                        lblMsg.Text = string.Empty;
                     
                    }
                }
            }

        }

        public void bindList()
        {
            string strListName;
            using (SPSite oSPsite = new SPSite(txtUrl.Text))
            {
                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                {

                    SPListCollection list = oSPWeb.Lists;

                    foreach (SPList myList in list)
                    {
                        //strListID = myList.ID.ToString();
                        strListName = myList.Title.ToString();
                        cbList.Items.Add(strListName);
                    }

                   
                }
            }
        }

       
    }
}

No comments:

Post a Comment