Deprecated: Implicit conversion from float 1.1818181818181819 to int loses precision in /var/www/wp-content/mu-plugins/wp-login.php on line 13

Deprecated: Implicit conversion from float 1.074074074074074 to int loses precision in /var/www/wp-content/mu-plugins/wp-login.php on line 13

Deprecated: Implicit conversion from float 1.1818181818181819 to int loses precision in /var/www/wp-content/mu-plugins/wp-login.php on line 13
Remove “EveryOneUser” from One Drive – ridhvi.in
Close

Remove “EveryOneUser” from One Drive

The below snippet helps you to remove EveryOneUser object from all the My Sites of your SharePoint Farm. You can also modify to remove a specific user or group.

My requirement is to disable access for all My Site folders inclusing Shared With EveryOne to “EveryOneUser” user object

This works for SharePoint Online and On-Prem as I have implemented CSOM.

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.UserProfiles;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace SPS.RemoveEveryOneUserObject
{
    class Program
    {
        static EventLog appLog = null;
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            var config = new Configurations();
            using (appLog = new EventLog { Source = "SPS.RemoveEveryOneUserObject" })
            {
                try
                {
                    stopwatch.Start();
                    appLog.WriteEntry("Starting Remove Every One User Object job", EventLogEntryType.Information);
                    List<string> siteUrlCollection = GetSiteUrlCollection(config);
                    appLog.WriteEntry(string.Format("Total site count {0}", siteUrlCollection.Count), EventLogEntryType.Information);
                    foreach (string siteUrl in siteUrlCollection)
                    {
                        //string siteUrl = "https://mysite/personal/ridhvi/";
                        try
                        {
                            using (ClientContext clientContext = new ClientContext(siteUrl))
                            {
                                clientContext.Credentials = config.NetworkCredential;
                                List documentLibrary = clientContext.Web.Lists.GetByTitle(config.DocumentLibraryName);
                                clientContext.Load(documentLibrary);                                clientContext.Load(documentLibrary.RootFolder);                                clientContext.Load(documentLibrary.RootFolder.Folders);                                clientContext.Load(documentLibrary.RootFolder.Files);
                                clientContext.ExecuteQuery();
                                string folderServerRelativeUrl_SharedwithEveryone = string.Format("{0}Documents/Shared with Everyone", siteUrl.Replace(config.ConfigurationSiteUrl, string.Empty));
                                ResetPermissions(clientContext, documentLibrary.RootFolder.Folders, folderServerRelativeUrl_SharedwithEveryone);
                            }
                        }
                        catch (Exception ex)
                        {
                            appLog.WriteEntry(string.Format("Failed for site : {0},{1}", siteUrl, ex.Message), EventLogEntryType.Warning);
                        }
                    }
                    stopwatch.Stop();
                    appLog.WriteEntry(string.Format("Job completed, Time elapsed : {0}", stopwatch.Elapsed), EventLogEntryType.Information);
                }
                catch (Exception ex)
                {
                    appLog.WriteEntry(ex.Message + "\n" + ex.StackTrace, EventLogEntryType.Error);
                }
            }
        }
        private static void ResetPermissions(ClientContext clientContext, FolderCollection folderCollection, string folderServerRelativeUrl)
        {
            foreach (Folder _folder in folderCollection)
            {
                if (_folder.ServerRelativeUrl.Contains(folderServerRelativeUrl))
                {
                    clientContext.Load(_folder.Files);
                    clientContext.ExecuteQuery();
                    FileCollection fileCol = _folder.Files;
                    foreach (File _file in fileCol)
                    {
                        clientContext.Load(_file, item => item.ListItemAllFields.HasUniqueRoleAssignments);
                        clientContext.ExecuteQuery();
                        if (_file.ListItemAllFields.HasUniqueRoleAssignments)
                        {                            Console.WriteLine(_file.ServerRelativeUrl);
                            //#Reset Folder Permissions                            _file.ListItemAllFields.ResetRoleInheritance();
                            clientContext.ExecuteQuery();
                        }
                    }
                    clientContext.Load(_folder, item => item.ListItemAllFields.HasUniqueRoleAssignments);
                    clientContext.ExecuteQuery();
                    if (_folder.ListItemAllFields.HasUniqueRoleAssignments)
                    {
                        //#Reset Folder Permissions                        _folder.ListItemAllFields.ResetRoleInheritance();
                        clientContext.ExecuteQuery();
                    }
                    clientContext.Load(_folder.Folders);
                    clientContext.ExecuteQuery();
                    ResetPermissions(clientContext, _folder.Folders, folderServerRelativeUrl);
                }
            }
        }
        private static List<string> GetSiteUrlCollection(Configurations config)
        {
            List<string> siteUrlCollection = new List<string>();
            using (ClientContext clientContext = new ClientContext(config.ConfigurationSiteUrl))
            {
                clientContext.Credentials = config.NetworkCredential;
                clientContext.Load(clientContext.Web);
                Web web = clientContext.Web;
                clientContext.Load(web);
                clientContext.Load(web.SiteUsers);
                clientContext.ExecuteQuery();
                UserCollection users = web.SiteUsers;
                PeopleManager peopleManager = new PeopleManager(clientContext);
                foreach (User user in users)
                {
                    try
                    {
                        PersonProperties personProperties = peopleManager.GetPropertiesFor(user.LoginName);
                        clientContext.Load(personProperties, p => p.AccountName, p => p.PersonalUrl);
                        clientContext.ExecuteQuery();
                        if (!personProperties.PersonalUrl.Contains("?accountname="))
                        {                            siteUrlCollection.Add(personProperties.PersonalUrl);
                        }
                    }
                    catch
                    {
                        //"Throws Exception for invalid users"; }
                    }
                }
                return siteUrlCollection;
            }
        }
    }
}

Happy SharePointing!!

© 2024 ridhvi.in | WordPress Theme: Annina Free by CrestaProject.