Tuesday, 12 July 2011

Insert Groups,Assign Role and add folder in document libarary‏

private void InsertGroups(string CompanyName, string GroupDes)
        {
            string DeptName = string.Empty;
            for (int i = 0; i < 6; i++)
            {
                using (SPSite site = new SPSite("http://lbs-sharepoint:1984/"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {


                        SPUserCollection users = web.AllUsers;
                        SPUser owner = users[string.Format("{0}\\{1}", "cybernet", "laksolcom_adeel")];
                        //SPUser member = users[string.Format("{0}\\{1}", "cybernet", "laksolcom_adeel")];
                        SPGroupCollection groups = web.SiteGroups;

                        string GroupName = "";
                        if (i == 0)
                        {
                            DeptName = "Sales";
                            GroupName = CompanyName.Trim() + "-Sales-reader";
                        }
                        if (i == 1)
                        {
                            DeptName = "Marketing";
                            GroupName = CompanyName.Trim() + "-Marketing-reader";
                        }
                        if (i == 2)
                        {
                            DeptName = "Product";
                            GroupName = CompanyName.Trim() + "-Product-reader";
                        }
                        if (i == 3)
                        {
                            DeptName = "Supply Chain";
                            GroupName = CompanyName.Trim() + "-Supply Chain-reader";
                        }
                        if (i == 4)
                        {
                            DeptName = "Technical";
                            GroupName = CompanyName.Trim() + "-Technical-reader";
                        }
                        if (i == 5)
                        {
                            DeptName = "Fianance";
                            GroupName = CompanyName.Trim() + "-Fianance-reader";
                        }
                        string GroupDescription = GroupDes;//"Company Name group description.";


                        groups.Add(GroupName, owner, null, GroupDescription);
                        SPGroup NewSPGroup = groups[GroupName];


                        // here’s where you define your role type:
                        SPRoleDefinition role = web.RoleDefinitions["Read"];
                        SPRoleAssignment roleAssignment = new SPRoleAssignment(NewSPGroup);
                        roleAssignment.RoleDefinitionBindings.Add(role);
                        web.RoleAssignments.Add(roleAssignment);
                        web.Update();


                        SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)web.Lists["LBS Document"];
                        SPFolderCollection _MyFolders = web.Folders;
                        string folderName = txtTo.Text + " " + DeptName;
                        _MyFolders.Add("http://lbs-sharepoint:1984/LBS%20Document/" + folderName + "/");
                        _MyDocLibrary.Update();
                        SPFolder folder = web.GetFolder("http://lbs-sharepoint:1984/LBS%20Document/" + folderName + "/");

                        SPGroupCollection spc = web.SiteGroups;


                        SPGroup spGroup = web.SiteGroups[GroupName];
                        SPRoleDefinition spRole = web.RoleDefinitions["Read"];
                        SPRoleAssignment roleAssignment1 = new SPRoleAssignment(spGroup);
                        roleAssignment1.RoleDefinitionBindings.Add(spRole);
                        folder.Item.BreakRoleInheritance(true);
                        folder.Item.RoleAssignments.Add(roleAssignment1);
                        folder.Update();


                    }

                }
            }
        }

No comments:

Post a Comment