-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include new solution RemoveSharingLinkAuto
- Loading branch information
Showing
6 changed files
with
323 additions
and
2 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/NovaPointLibrary/Commands/SharePoint/SiteGroup/SPOSiteGroupCSOM.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Microsoft.SharePoint.Client; | ||
using NovaPointLibrary.Commands.Authentication; | ||
using NovaPointLibrary.Solutions; | ||
|
||
namespace NovaPointLibrary.Commands.SharePoint.SiteGroup | ||
{ | ||
internal class SPOSiteGroupCSOM | ||
{ | ||
private readonly NPLogger _logger; | ||
private readonly AppInfo _appInfo; | ||
|
||
internal SPOSiteGroupCSOM(NPLogger logger, AppInfo appInfo) | ||
{ | ||
_logger = logger; | ||
_appInfo = appInfo; | ||
} | ||
|
||
internal async Task<IEnumerable<Group>> GetAsync(string siteUrl) | ||
{ | ||
_appInfo.IsCancelled(); | ||
_logger.LogTxt(GetType().Name, $"Getting all SharePoint Group from site '{siteUrl}'"); | ||
|
||
var clientContext = await _appInfo.GetContext(siteUrl); | ||
|
||
var groups = clientContext.LoadQuery(clientContext.Web.SiteGroups.IncludeWithDefaultProperties(g => g.Users, g => g.Title, g => g.OwnerTitle, g => g.Owner.LoginName, g => g.LoginName)); | ||
clientContext.ExecuteQueryRetry(); | ||
|
||
return groups; | ||
} | ||
|
||
internal async Task<Group> GetAsync(string siteUrl, int groupId) | ||
{ | ||
_appInfo.IsCancelled(); | ||
_logger.LogTxt(GetType().Name, $"Getting SharePoint Group with ID '{groupId}' from site '{siteUrl}'"); | ||
|
||
var clientContext = await _appInfo.GetContext(siteUrl); | ||
|
||
var group = clientContext.Web.SiteGroups.GetById(groupId); | ||
clientContext.Load(group); | ||
clientContext.ExecuteQueryRetry(); | ||
|
||
return group; | ||
|
||
} | ||
|
||
internal async Task RemoveAsync(string siteUrl, Group group) | ||
{ | ||
_appInfo.IsCancelled(); | ||
_logger.LogTxt(GetType().Name, $"Removing SharePoint Group '{group.Title}' from site '{siteUrl}'"); | ||
|
||
var clientContext = await _appInfo.GetContext(siteUrl); | ||
|
||
clientContext.Web.SiteGroups.RemoveById(group.Id); | ||
clientContext.ExecuteQueryRetry(); | ||
} | ||
} | ||
} |
171 changes: 171 additions & 0 deletions
171
src/NovaPointLibrary/Solutions/Automation/RemoveSharingLinksAuto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
using Microsoft.SharePoint.Client; | ||
using Microsoft.SharePoint.Client.Sharing; | ||
using NovaPointLibrary.Commands.SharePoint.Item; | ||
using NovaPointLibrary.Commands.SharePoint.Permision; | ||
using NovaPointLibrary.Commands.SharePoint.Permision.Utilities; | ||
using NovaPointLibrary.Commands.SharePoint.Site; | ||
using NovaPointLibrary.Commands.SharePoint.SiteGroup; | ||
using NovaPointLibrary.Commands.SharePoint.User; | ||
using NovaPointLibrary.Solutions.Report; | ||
using PnP.Core.Model.SharePoint; | ||
using PnP.Framework.Provisioning.Model; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Dynamic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NovaPointLibrary.Solutions.Automation | ||
{ | ||
public class RemoveSharingLinksAuto | ||
{ | ||
public static readonly string s_SolutionName = "Remove Sharing Links"; | ||
public static readonly string s_SolutionDocs = "https://github.com/Barbarur/NovaPoint/wiki/Solution-Automation-RemoveSharingLinksAuto"; | ||
|
||
private RemoveSharingLinksAutoParameters _param; | ||
private readonly NPLogger _logger; | ||
private readonly Commands.Authentication.AppInfo _appInfo; | ||
|
||
private readonly Expression<Func<Web, object>>[] _siteExpressions = new Expression<Func<Web, object>>[] | ||
{ | ||
w => w.HasUniqueRoleAssignments, | ||
w => w.Id, | ||
w => w.RoleAssignments.Include( | ||
ra => ra.RoleDefinitionBindings, | ||
ra => ra.Member), | ||
w => w.Title, | ||
w => w.Url, | ||
}; | ||
|
||
private RemoveSharingLinksAuto(NPLogger logger, Commands.Authentication.AppInfo appInfo, RemoveSharingLinksAutoParameters parameters) | ||
{ | ||
_param = parameters; | ||
_logger = logger; | ||
_appInfo = appInfo; | ||
} | ||
|
||
public static async Task RunAsync(RemoveSharingLinksAutoParameters parameters, Action<LogInfo> uiAddLog, CancellationTokenSource cancelTokenSource) | ||
{ | ||
NPLogger logger = new(uiAddLog, "RemoveSharingLinksAuto", parameters); | ||
try | ||
{ | ||
Commands.Authentication.AppInfo appInfo = await Commands.Authentication.AppInfo.BuildAsync(logger, cancelTokenSource); | ||
|
||
await new RemoveSharingLinksAuto(logger, appInfo, parameters).RunScriptAsync(); | ||
|
||
logger.ScriptFinish(); | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
logger.ScriptFinish(ex); | ||
} | ||
} | ||
|
||
private async Task RunScriptAsync() | ||
{ | ||
_appInfo.IsCancelled(); | ||
|
||
await foreach (var siteRecord in new SPOTenantSiteUrlsWithAccessCSOM(_logger, _appInfo, _param.SiteAccParam).GetAsync()) | ||
{ | ||
_appInfo.IsCancelled(); | ||
|
||
if (siteRecord.Ex != null) | ||
{ | ||
RemoveSharingLinksAutoRecord record = new(siteRecord); | ||
_logger.RecordCSV(record); | ||
continue; | ||
} | ||
|
||
try | ||
{ | ||
await ProcessSite(siteRecord); | ||
} | ||
catch (Exception ex) | ||
{ | ||
RemoveSharingLinksAutoRecord record = new(siteRecord, ex.Message); | ||
_logger.RecordCSV(record); | ||
_logger.ReportError(GetType().Name, "Site", siteRecord.SiteUrl, ex); | ||
} | ||
|
||
} | ||
} | ||
|
||
private async Task ProcessSite(SPOTenantSiteUrlsRecord siteRecord) | ||
{ | ||
_appInfo.IsCancelled(); | ||
|
||
var collGroups = await new SPOSiteGroupCSOM(_logger, _appInfo).GetAsync(siteRecord.SiteUrl); | ||
|
||
ProgressTracker progress = new(siteRecord.Progress, collGroups.Count()); | ||
foreach (var group in collGroups) | ||
{ | ||
if (group.Title.Contains("SharingLinks")) | ||
{ | ||
try | ||
{ | ||
if (!_param.ReportMode) | ||
{ | ||
await new SPOSiteGroupCSOM(_logger, _appInfo).RemoveAsync(siteRecord.SiteUrl, group); | ||
} | ||
|
||
RemoveSharingLinksAutoRecord record = new(siteRecord, "Removed"); | ||
record.AddDetails(group); | ||
_logger.RecordCSV(record); | ||
} | ||
catch (Exception ex) | ||
{ | ||
RemoveSharingLinksAutoRecord record = new(siteRecord, ex.Message); | ||
_logger.RecordCSV(record); | ||
_logger.ReportError(GetType().Name, "Sharing Link", $"{group.Id}", ex); | ||
} | ||
} | ||
progress.ProgressUpdateReport(); | ||
} | ||
|
||
} | ||
} | ||
|
||
public class RemoveSharingLinksAutoRecord : ISolutionRecord | ||
{ | ||
internal string SiteUrl { get; set; } = String.Empty; | ||
|
||
internal string GroupID { get; set; } = String.Empty; | ||
internal string GroupTitle { get; set; } = String.Empty; | ||
internal string GroupDescription { get; set; } = String.Empty; | ||
|
||
internal string Remarks { get; set; } = String.Empty; | ||
|
||
internal RemoveSharingLinksAutoRecord(SPOTenantSiteUrlsRecord siteRecord, | ||
string remarks = "") | ||
{ | ||
SiteUrl = siteRecord.SiteUrl; | ||
if ( siteRecord.Ex != null) { Remarks = siteRecord.Ex.Message; } | ||
else { Remarks = remarks; } | ||
} | ||
|
||
internal void AddDetails(Group groupSharedLink) | ||
{ | ||
GroupID = groupSharedLink.Id.ToString(); | ||
GroupTitle = groupSharedLink.Title; | ||
GroupDescription = groupSharedLink.Description; | ||
} | ||
|
||
} | ||
|
||
public class RemoveSharingLinksAutoParameters : ISolutionParameters | ||
{ | ||
public bool ReportMode { get; set; } = true; | ||
|
||
public SPOTenantSiteUrlsWithAccessParameters SiteAccParam { get; set; } | ||
public RemoveSharingLinksAutoParameters(bool reportMode, | ||
SPOTenantSiteUrlsWithAccessParameters siteParam) | ||
{ | ||
ReportMode = reportMode; | ||
SiteAccParam = siteParam; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/NovaPointWPF/Pages/Solutions/Automation/RemoveSharingLinksAutoForm.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Page x:Class="NovaPointWPF.Pages.Solutions.Automation.RemoveSharingLinksAutoForm" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:designmaterial="clr-namespace:NovaPointWPF.UserControls" | ||
mc:Ignorable="d" | ||
d:DesignHeight="900" d:DesignWidth="800" | ||
Title="RemoveSharingLinksAutoForm"> | ||
|
||
<Grid> | ||
|
||
<StackPanel> | ||
|
||
<designmaterial:FormHeader | ||
x:Name="SolutionHeader"/> | ||
|
||
<designmaterial:ReportModeForm | ||
x:Name="ModeF"/> | ||
|
||
<designmaterial:AdminForm | ||
x:Name="AdminF"/> | ||
|
||
<designmaterial:SiteTenantForm | ||
x:Name="SiteF" | ||
SubsitesVisibility="False"/> | ||
|
||
</StackPanel> | ||
|
||
</Grid> | ||
|
||
</Page> |
51 changes: 51 additions & 0 deletions
51
src/NovaPointWPF/Pages/Solutions/Automation/RemoveSharingLinksAutoForm.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using NovaPointLibrary.Commands.SharePoint.Site; | ||
using NovaPointLibrary.Solutions; | ||
using NovaPointLibrary.Solutions.Automation; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
using System.Windows.Documents; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Imaging; | ||
using System.Windows.Navigation; | ||
using System.Windows.Shapes; | ||
|
||
namespace NovaPointWPF.Pages.Solutions.Automation | ||
{ | ||
/// <summary> | ||
/// Interaction logic for RemoveSharingLinksAutoForm.xaml | ||
/// </summary> | ||
public partial class RemoveSharingLinksAutoForm : Page, ISolutionForm | ||
{ | ||
public RemoveSharingLinksAutoForm() | ||
{ | ||
InitializeComponent(); | ||
|
||
DataContext = this; | ||
|
||
SolutionHeader.SolutionTitle = RemoveSharingLinksAuto.s_SolutionName; | ||
SolutionHeader.SolutionCode = nameof(RemoveSharingLinksAuto); | ||
SolutionHeader.SolutionDocs = RemoveSharingLinksAuto.s_SolutionDocs; | ||
} | ||
|
||
public async Task RunSolutionAsync(Action<LogInfo> uiLog, CancellationTokenSource cancelTokenSource) | ||
{ | ||
SPOTenantSiteUrlsWithAccessParameters siteAccParam = new() | ||
{ | ||
AdminAccess = AdminF.Parameters, | ||
SiteParam = SiteF.Parameters, | ||
}; | ||
|
||
RemoveSharingLinksAutoParameters parameters = new(ModeF.ReportMode, siteAccParam); | ||
|
||
await RemoveSharingLinksAuto.RunAsync(parameters, uiLog, cancelTokenSource); | ||
} | ||
} | ||
} |