「東雲 忠太郎」の平凡な日常のできごと

2024/02/26(月)03:05

SharePointRestAPIでサイトグループにユーザーを追加するコード

VB.NET(26)

Imports System.Net.Http Imports System.Text Imports System.Threading.Tasks Module Module1     Sub Main()         Dim siteUrl As String = "https://your-sharepoint-site-url"         Dim username As String = "your-username"         Dim password As String = "your-password"         Dim groupName As String = "YourGroupName" ' グループ名         Dim userEmail As String = "user@example.com" ' 追加するユーザーのメールアドレス         AddUserToGroup(siteUrl, username, password, groupName, userEmail).Wait()     End Sub     Async Function AddUserToGroup(siteUrl As String, username As String, password As String, groupName As String, userEmail As String) As Task         Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"))         ' グループ ID を取得         Dim groupId As Integer = Await GetGroupId(siteUrl, credentials, groupName)         If groupId <> -1 Then             ' ユーザーをグループに追加する             Dim endpointUrl As String = $"{siteUrl}/_api/web/sitegroups({groupId})/users"             Dim requestBody As String = $"{{'__metadata': {{'type': 'SP.User'}}, 'LoginName': '{userEmail}'}}"             Using client As New HttpClient()                 client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials)                 client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))                 Dim content As New StringContent(requestBody, Encoding.UTF8, "application/json")                 Dim response As HttpResponseMessage = Await client.PostAsync(endpointUrl, content)                 If response.IsSuccessStatusCode Then                     Console.WriteLine($"User '{userEmail}' added to group '{groupName}' successfully.")                 Else                     Console.WriteLine($"Failed to add user to group. StatusCode: {response.StatusCode}")                 End If             End Using         Else             Console.WriteLine($"Group '{groupName}' not found.")         End If     End Function     Async Function GetGroupId(siteUrl As String, credentials As String, groupName As String) As Task(Of Integer)         Dim groupId As Integer = -1         Dim endpointUrl As String = $"{siteUrl}/_api/web/sitegroups?$filter=Title eq '{groupName}'"         Using client As New HttpClient()             client.DefaultRequestHeaders.Authorization = New System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials)             client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))             Dim response As HttpResponseMessage = Await client.GetAsync(endpointUrl)             If response.IsSuccessStatusCode Then                 Dim responseContent As String = Await response.Content.ReadAsStringAsync()                 Dim groupData = Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent)                 If groupData("value").Count > 0 Then                     groupId = groupData("value")(0)("Id")                 End If             Else                 Console.WriteLine($"Failed to retrieve group information. StatusCode: {response.StatusCode}")             End If         End Using         Return groupId     End Function End Module

続きを読む

このブログでよく読まれている記事

もっと見る

総合記事ランキング

もっと見る