Tuesday, May 31, 2016

List/library roles/permissions & folder/item level - unique permissions report to csv file

List level roles.. folder/item level - unique permissions report to csv file




        public static void GetAllFolderNfilesPermissionNAdd2MYSPGroup(SPWeb webElevated, string selectedLibrary)
        {
            try
            {
                SPList list = webElevated.Lists.TryGetList(selectedLibrary);
                if (list != null)
                {
                    string selectedLibraryRolesCSV = selectedLibrary.ToString() + "_UserRoles_Report.csv";

                    StreamWriter SW;
                    SPListItem itemNull = null;

                    SW = File.AppendText("C:\\" + selectedLibraryRolesCSV);

                    SW.WriteLine("File Name, User Name, Group Name, Permission Type, FileID, File Location, Folder Name, Unique Permission");
                    // List level permissions
                    if (list.RoleAssignments.Count > 0)
                    {
                        foreach (SPRoleAssignment asignment in list.RoleAssignments)
                        {
                            rolesAssigmentNWrite2Report(asignment, selectedLibrary, SW, itemNull, false, false);
                        }
                    }
                    else
                    {
                        bool listUniqueRightes = list.HasUniqueRoleAssignments;
                        SW.WriteLine("" + "," + "" + "," + "" + "," + "LIST OR LIB IS NOT SHARED WITH ANYONE" + "," + " " + "," + " " + "," + " " + "," + listUniqueRightes);
                    }


                    // Folder level permissions
                    foreach (SPListItem folderItem in list.Folders)
                    {
                        bool uniqueRoleAssignmentObj = folderItem.HasUniqueRoleAssignments;

                        if (uniqueRoleAssignmentObj)
                        {
                            if (folderItem.RoleAssignments.Count > 0)
                            {
                                foreach (SPRoleAssignment asignment in folderItem.RoleAssignments)
                                {
                                    rolesAssigmentNWrite2Report(asignment, selectedLibrary, SW, folderItem, true, false);
                                }
                            }
                            else
                            { // Not Share with anyone
                                SPRoleAssignment asignment = null;
                                rolesAssigmentNWrite2Report(asignment, selectedLibrary, SW, folderItem, true, true);
                            }
                            // Get files in side folder
                            //SPFolder folder = folderItem.Folder;
                            //foreach (SPFile file in folder.Files)
                            //{ // file operations }
                        }
                    }
                   
                    //Item/file level permissions
                    SPView Myview = list.Views["All Documents"];
                    SPQuery oQuery = new SPQuery(Myview);
                    oQuery.ViewAttributes = "Scope=\"Recursive\"";
                    SPListItemCollection itemColl = list.GetItems(oQuery);

                    foreach (SPListItem item in itemColl)
                    {
                        bool uniqueRoleAssignmentObj = item.HasUniqueRoleAssignments;

                        if (uniqueRoleAssignmentObj)
                        {
                           //
                            if (item.RoleAssignments.Count > 0)
                            {
                                foreach (SPRoleAssignment asignment in item.RoleAssignments)
                                {
                                    rolesAssigmentNWrite2Report(asignment, selectedLibrary, SW, item, true, false);
                                }
                            }
                            else
                            { // Not Share with anyone
                                SPRoleAssignment asignment = null;
                                rolesAssigmentNWrite2Report(asignment, selectedLibrary, SW, item, true, true);
                            }
                        }

                    }
                    SW.Close();
                }
            }
            catch (Exception ex)
            {

            }

        }
        public static void rolesAssigmentNWrite2Report(SPRoleAssignment asignment, string selectedLibrary, StreamWriter SW, SPListItem item, bool isSPListItem, bool notSharedwithAnyone)
        {
            try
            {
                if (notSharedwithAnyone)
                {   //Item/folder if not shared with anyone                
                    SW.WriteLine(item.Name + "," + "NOT SHARED WITH ANYONE" + "," + "NOT SHARED WITH ANYONE" + "," + "NOT SHARED WITH ANYONE" + "," + item.ID + "," + item.Url + "," + item.Name + "," + "Yes");
                }
                else
                {
                    if (asignment != null)
                    {
                        foreach (SPRoleDefinition roleDefinition in asignment.RoleDefinitionBindings)
                        {
                            string groupName = ""; string UserName = ""; string PermissionType = "";
                            if (asignment.Member is SPUser)
                            {
                                //UserName = ((SPUser)asignment.Member).ToString();
                                SPUser user = ((SPUser)asignment.Member);
                                UserName = user.Name;
                                addUserToMyGroup(webElevated, user, selectedLibrary);
                                PermissionType = roleDefinition.Name.ToString();
                            }
                            else if (asignment.Member is SPGroup)
                            {
                                SPGroup grp = (SPGroup)asignment.Member;
                                groupName = grp.Name;
                                foreach (SPUser user in grp.Users)
                                {
                                    PermissionType = roleDefinition.Name.ToString();
                                    addUserToMyGroup (webElevated, user, selectedLibrary);
                                }

                            }
                            if (isSPListItem)
                            {
                                string folderName = "";

                                if (item.Folder != null)
                                {
                                    folderName = item.Folder.ToString();
                                }
                                // SW.WriteLine("1.UserName, 2.GroupName,      3.PermissionType,        4.FileName, 5.FileID, 6.FileUrl, 7.FolderName, 8.HASUnicPermission");
                                SW.WriteLine(item.Name + "," + UserName + "," + groupName + "," + PermissionType + "," + item.ID + "," + item.Url + "," + folderName + "," + "Yes");
                            }
                            else
                            { // list level permissions
                                SW.WriteLine("DTR: " + selectedLibrary + "," + UserName + "," + groupName + "," + PermissionType + "," + "NA" + "," + "NA" + "," + "NA" + "," + "NA");
                            }

                            //  1.UserName, 2.GroupName, 3.PermissionType, 4.FileName, 5.FileID, 6.FileUrl, 7.FolderName, 8.HASUnicPermission
                        }
                    }
                }


            }
            catch (Exception ex12) { }

        }

        public static void addUserToMyGroup(SPWeb elevweb, SPUser user, string selectedLibraryName)
        {
            string dtRoomGrpName = selectedLibraryName + "_ViewOnly";

            try
            {
                elevweb.AllowUnsafeUpdates = true;
                SPGroup grp = elevweb.Groups.GetByName(dtRoomGrpName);
                grp.AddUser(user);
                elevweb.Update();
                elevweb.AllowUnsafeUpdates = false;
            }
            catch (Exception ex)
            {
                // msgError("Error while adding user into site Access: " + x.Message);
            }

        }

No comments:

Post a Comment