Wednesday, April 20, 2016

Document Library & Page -- some of operations- Security, Groups, Users, Adding Webpart, Deleting webpart


Document Library & Page -- some of operations- Security, Groups, Users, Adding Webpart, Deleting webpart


//Get only Document libraries from web not lists

public static void docLibColl(SPWeb web)
        {
            SPListCollection listColl = web.GetListsOfType(SPBaseType.DocumentLibrary);
        }
        public void addUserToTheGroup(SPWeb web, SPUser user)
        {
            SPGroup grp = web.Groups.GetByName("");
            grp.AddUser(user);
        }


//Break inheritance and remove all user roles except one Group users

        public static void breakInheritanceNRemoveUsersExcept1Group(SPWeb web, string selectedLibraryName)
        {
            string libGrpName = selectedLibraryName + "_ViewOnly";
            SPList list = web.Lists.TryGetList("testLib1");
            list.BreakRoleInheritance(true);
            SPRoleAssignmentCollection rolColl = list.RoleAssignments;
            for (int i = rolColl.Count - 1; i >= 0; i--)
            {
                 SPRoleAssignment role = rolColl[i];
                 if (role.Member is SPGroup)
                 {
                     if (role.Member.Name != libGrpName)
                     {
                         rolColl.Remove(i);
                     }                  
                 }
                 else
                     rolColl.Remove(i);
               // rolColl.Remove(i);
            }
            list.Update();
        }

//Remove user permission from the Library

        public static void removeUserPermission4mLib(SPWeb web)
        {
            // bool userDeleted = false;
            SPUser user = web.Users.GetByID(10);
            SPList list = web.Lists.TryGetList("TestRku");
            SPRoleAssignmentCollection SPRoleAssColn = list.RoleAssignments;

            for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
            {
                SPRoleAssignment role = SPRoleAssColn[i];

                if (role.Member is SPUser)
                    if (role.Member.ID == user.ID)
                    {
                        SPRoleAssColn.Remove(i);
                        list.Update();
                        break;
                    }

                //if (userDeleted)
                //{
                //    break;
                //}
            }
        }

// Create a Group and assign Readonly permission and add to the web

        public static void CreateGrpNewGroup4List(SPWeb web, string selectedLibraryName)
        {
            SPGroup grp = null;
            string newGrpName = selectedLibraryName + "_ViewOnly";

            try
            {
                grp = web.Groups.GetByName(newGrpName);
            }
            catch { }

            if (grp == null)
            {
                web.SiteGroups.Add(newGrpName, web.CurrentUser, web.Author, "Used for DATAROOMs");
                SPRoleAssignment assignment = new SPRoleAssignment(web.SiteGroups[newGrpName]);
                SPRoleDefinition roleApp = web.RoleDefinitions["View only"];
                assignment.RoleDefinitionBindings.Add(roleApp);
                web.RoleAssignments.Add(assignment);
                web.Update();
            }

        }


        public static void makeWordreadOnly(SPFile path)
        {
            //  FileAttributes attributes = File.GetAttributes(path);
            // File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);       
        } 


// Remove User Permission from web

        public static void removeUserPermission4mWeb(SPWeb web)
        {
            SPUser user = web.Users.GetByID(10);

            SPRoleAssignment roleAssignment = new SPRoleAssignment(user.LoginName, user.Email, user.Name, user.Notes);
            SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
            roleAssignment.RoleDefinitionBindings.Remove(roleDefinition);
            web.Update();
        }
        public static void addUserTotheList(SPWeb web, SPUser user)
        {
            SPList list = web.Lists.TryGetList("TestRku");
            SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Administrator);

            string userEmail = "";

            if (user.Email == null)
            {
                userEmail = "Test@testab.com";
            }
            else { userEmail = user.Email; }
            SPRoleAssignment roleAssignment = new SPRoleAssignment(user.LoginName, userEmail, user.Name, user.Name);
            roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
            list.RoleAssignments.Add(roleAssignment);
            list.Update();

        }

// Document Library USers list

        public static void docLibUsers(SPWeb web)
        {
            SPList list = web.Lists.TryGetList("TestRku");
            SPRoleAssignmentCollection assignColl = list.RoleAssignments;

            foreach (SPRoleAssignment assignment in assignColl)
            {
                //perms.Add(assignment.Member.LoginName);
                //Console.WriteLine("Perms: " + assignment.Member.LoginName);

                if (assignment.Member is SPUser)
                {
                    SPUser user = ((SPUser)assignment.Member);
                    // Console.WriteLine(((SPUser)assignment.Member).LoginName);
                    users.Add(user.LoginName);

                }
                else if (assignment.Member is SPGroup)
                {
                    // Console.WriteLine(((SPGroup)assignment.Member).Name);
                    getUsersFromGroup(web, ((SPGroup)assignment.Member).Name);
                }
            }

            Console.WriteLine(users);
        }


// Add User to the Group

        public static void addUsersTotheGroup(SPWeb web, string groupName)
        {
            SPGroup group = web.Groups[groupName];
            foreach (SPUser user in group.Users)
            {
                users.Add(user.LoginName);
            }


        }

// Inherit default permissions to the list

        public static void inheritUserPermissions(SPWeb web)
        {
            SPList list = web.Lists.TryGetList("CustLib");
            list.ResetRoleInheritance();
            list.Update();
        }   


 // Remove/delete from page webpart

        public static void RemoveWebPart(SPWeb web, string page, string webPartTitle)
        {
            SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager("http://ravim:555/lib1/Forms/AllItems.aspx", PersonalizationScope.Shared);

            IEnumerable webPartList = from System.Web.UI.WebControls.WebParts.WebPart webPart in webPartManager.WebParts
                                      where webPart.Title == webPartTitle
                                      select webPart;

            foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in webPartList)
            {
                if (webPartTitle == "testRavi")
                {
                    webPartManager.DeleteWebPart(webPart);
                    break;
                }
            }

            web.Update();
        }


// Remove Script Editor Webpart from pahe

        public static void RemoveScriptEdWebPart(SPWeb web)
        {
            string pageUrl = getLibraries2Add(web);
            //SPFile page = web.GetFile(pageUrl); //GetFile("SitePages/prg.aspx");

            SPWebPartCollection webparts = web.GetWebPartCollection(pageUrl, Storage.Shared);
            for (int k = 0; k < webparts.Count; k++)
            {
                //get reference to webpart
                Microsoft.SharePoint.WebPartPages.WebPart wp = webparts[k];

                //check webpart Title to find webpart which is to be removed
                if (wp.Title == "Ravi Test")
                {
                    //delete webpart
                    //   webparts.Delete(wp.StorageKey);

                    //update spWeb object
                    //   web.Update();
                }
            }

        }

// Add Script Editor webpart to the Page

        public static void addScriptEdWebPart(SPWeb web)
        {

            string pageUrl = getLibraries2Add(web);
            SPFile page = web.GetFile(pageUrl); //GetFile("SitePages/prg.aspx");
            page.CheckOut();

            using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                var wpExist = false;
                var webpartCollection = wpmgr.WebParts.GetEnumerator();
                while (webpartCollection.MoveNext())
                {
                    var cWpart = webpartCollection.Current;
                    if (cWpart.ToString() == "Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart")
                    {
                        if (((Microsoft.SharePoint.WebPartPages.ScriptEditorWebPart)(webpartCollection.Current)).Content.ToString().Contains("'/_Layouts/Ravi.test.aspx?"))
                        {
                            wpExist = true;
                            break;
                        }
                    }
                }

                if (wpExist == false)
                {
                    ScriptEditorWebPart scwp = new ScriptEditorWebPart();
                    scwp.Title = "BE ECBDatarooms";
                    scwp.ChromeType = PartChromeType.None;

                    StreamReader reader = new StreamReader(@"C:\zzScript\ScriptEditor.txt");
                    string scwpContent = "";
                    while (reader.Peek() != -1)
                    {
                        scwpContent += reader.ReadLine() + "\n";
                    }

                    scwp.Content = scwpContent;
                    wpmgr.AddWebPart(scwp, "Body", 1);
                    wpmgr.SaveChanges(scwp);
                }

            } 
            page.CheckIn(String.Empty);

        }
     

        

No comments:

Post a Comment