Tuesday 23 July 2013

Create Custom Groups in SharePoint Using C#


public void CreateCustomGroup()
        {
            Hashtable groupList = new Hashtable();
            SPUser sUser = null;
            object lobj = null;

            try
            {
                groupList.Add("My_Custom Group", "Will have all kind of access to the application");
                groupList.Add("My_Custom Group_For_USer", "Will have all kind of access to the application");
           
                using (SPWeb oweb = SPSite.OpenWeb())
                {
                  SPUSer  aUser = oweb.CurrentUser;
                    IDictionaryEnumerator lDE = groupList.GetEnumerator();
                    lobj = new object();
                    while (lDE.MoveNext())
                    {
                        if (!this.IsGroupExist(oweb.SiteGroups, lDE.Key.ToString()))
                        {
                            mSPWeb.AllowUnsafeUpdates = true;
                            mSPWeb.SiteGroups.Add(lDE.Key.ToString(), sUser, sUser, Convert.ToString(lDE.Value.ToString()));
                            if (lDE.Key.ToString().Contains("My_Custom Group"))
                            {
                                this.SetRoleDefinition(Convert.ToString(lDE.Key.ToString()),"permission Level Name");
                            }
                            else if (Convert.ToString(lDE.Key.ToString()).Contains("My_Custom Group_For_USer", "Will have all kind of access to the application");
              "))
                            {
                               this.SetRoleDefinition(Convert.ToString(lDE.Key.ToString()),"permission Level Name");
                            }
                       
                        }
                    }
                }
            }
            catch (Exception e)
            {
         
                throw e;
            }
         
        }