-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
218 lines (181 loc) · 10.9 KB
/
Program.cs
File metadata and controls
218 lines (181 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using Azure.Identity;
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
namespace GraphApp1
{
class Program
{
public static async Task GraphOp (GraphServiceClient graphClient)
{
// Reading the Teams List
List<string> Teams = new List<string>();
foreach (string line in System.IO.File.ReadLines(@"***\Teams.txt"))
{
if (line.Length > 10 && line.Contains("Guid") == false && line.Contains("---") == false)
Teams.Add(line);
}
foreach (String Team in Teams)
{
Console.WriteLine(Team);
// Get Channels for a Team
var channels = await graphClient.Teams[Team].Channels
.Request()
.GetAsync();
// Going Through all the Channels of a Team
foreach (Microsoft.Graph.Channel C in channels)
{
// Getting the File Folder for each Channel
var FileFolder = await graphClient.Teams[Team].Channels[C.Id].FilesFolder
.Request()
.GetAsync();
// Checking if the Channel Name is the same as the File Folder Name
if (C.DisplayName != FileFolder.Name)
{
Console.WriteLine("Wrong Name " + C.DisplayName + " " + FileFolder.Name);
// Get the Teams Drive ID
var TeamsDrive = await graphClient.Groups[Team].Drive
.Request()
.GetAsync();
Console.WriteLine("Teams Drive ID used here:" + FileFolder.Id);
// Trying to get the List of Childrens for My Channel Folder
try
{
// Get Children Elements of the Channels SPO Folder
var DriveItemsUnderMainFolder = await graphClient.Drives[TeamsDrive.Id].Items[FileFolder.Id].Children
.Request()
.GetAsync();
// Checking if there are any Files under the Folder
if (DriveItemsUnderMainFolder.Count != 0)
{
Console.WriteLine("First Children Id: " + DriveItemsUnderMainFolder[0].Id);
// Going through the Files under the Folder
foreach (DriveItem i in DriveItemsUnderMainFolder)
{
Console.WriteLine(i.Name);
// Getting the Permissions of each Files under the Folder
var DriveItemPermissions = await graphClient.Drives[TeamsDrive.Id].Items[i.Id].Permissions
.Request()
.GetAsync();
// Should be Renamed Here the Channel
// Should get the new Files here
// Going through the Permissions of each Files under the Folder
foreach (Permission j in DriveItemPermissions)
{
// Checking for the Permissions to be Coming from an URL
if (j.Link != null && j.InheritedFrom == null)
{
// Displaying the Shareable Link
Console.WriteLine("This is the Shareable Link: " + j.Link.WebUrl + " " + j.InheritedFrom);
// Type and Scope of the Sharing Link already existing
var type = j.Link.Type;
var scope = j.Link.Scope;
// List were I'll try to get each User with which the URL was shared
List<User> users = new List<User>();
if (j.GrantedToIdentitiesV2 != null)
{
// Users of the Sharing Link already existing
foreach (IdentitySet k in j.GrantedToIdentitiesV2)
{
if (k.User.DisplayName != null)
{
Console.WriteLine("Found an user: " + k.User.DisplayName);
var user = await graphClient.Users[k.User.Id]
.Request()
.GetAsync();
Console.WriteLine("With email address: " + user.Mail);
users.Add(user);
}
}
}
// Create the new Sharing Link
await graphClient.Drives[TeamsDrive.Id].Items[DriveItemsUnderMainFolder[0].Id]
.CreateLink(type, scope, null, null, null, null)
.Request()
.PostAsync();
// Create a List of Users for the new Sharing Link
if (users != null)
{
List<DriveRecipient> recipients = new List<DriveRecipient>();
foreach (User k in users)
{
recipients.Add(new DriveRecipient
{
Email = k.Mail
});
}
// Getting the Permissions for our new URL
var permissionsNewURL = await graphClient.Drives[TeamsDrive.Id].Items[DriveItemsUnderMainFolder[0].Id].Permissions
.Request()
.GetAsync();
// Encoding the URL in Order to get the ShareID
string encodedUrl = null;
foreach (Permission ij in permissionsNewURL)
{
if (ij.Link != null && ij.InheritedFrom == null)
{
string sharingUrl = ij.Link.WebUrl;
string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));
encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/', '_').Replace('+', '-');
}
}
// Creating the URL Roles
var roles = new List<String>();
if (type == "edit")
roles.Add("write");
else
roles.Add("read");
// Updating the URL Permissions
await graphClient.Shares[encodedUrl].Permission
.Grant(roles, recipients)
.Request()
.PostAsync();
}
}
}
}
}
}
catch (Exception)
{
Console.WriteLine("Children Command Failed - Likley no File in the Folder:" + FileFolder.Name);
}
}
else
Console.WriteLine("Correct Name " + C.DisplayName + " " + FileFolder.Name);
}
}
}
static void Main(string[] args)
{
// The client credentials flow requires that you request the
// /.default scope, and preconfigure your permissions on the
// app registration in Azure. An administrator must grant consent
// to those permissions beforehand.
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "****";
// Values from app registration
var clientId = "****";
var clientSecret = "****";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
Console.WriteLine("Start");
GraphOp(graphClient).GetAwaiter().GetResult();
Console.WriteLine("End");
}
}
}