Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit cef5472

Browse files
author
Jim Crowley
committed
New add user to group snippet
1 parent 7461c2b commit cef5472

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

Microsoft-Graph-Snippets-SDK/Assets/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="AddMember" xml:space="preserve">
121+
<value>Add member</value>
122+
</data>
120123
<data name="ClearSelected" xml:space="preserve">
121124
<value>clear selection</value>
122125
</data>

Microsoft-Graph-Snippets-SDK/Groups/GroupSnippets.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,31 @@ public static async Task<bool> DeleteGroupAsync(string groupId)
221221

222222
}
223223

224+
public static async Task<bool> AddUserToGroup(string userId, string groupId)
225+
{
226+
bool userAdded = false;
227+
GraphServiceClient graphClient = AuthenticationHelper.GetAuthenticatedClient();
228+
229+
try
230+
{
231+
User userToAdd = new User { Id = userId };
232+
233+
//The underlying REST call returns a 204 (no content), so we can't retrieve the updated group
234+
//with this call.
235+
await graphClient.Groups[groupId].Members.References.Request().AddAsync(userToAdd);
236+
Debug.WriteLine("Added user " + userId + " to the group: " + groupId);
237+
userAdded = true;
238+
239+
}
240+
241+
catch (ServiceException e)
242+
{
243+
Debug.WriteLine("We could not add a user to the group: " + e.Error.Message);
244+
userAdded = false;
245+
}
246+
return userAdded;
247+
}
248+
224249
}
225250
}
226251

Microsoft-Graph-Snippets-SDK/Groups/GroupStories.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ public static async Task<bool> TryDeleteGroupAsync()
100100
return await GroupSnippets.DeleteGroupAsync(createdGroup);
101101
}
102102

103+
public static async Task<bool> TryAddUserToGroup()
104+
{
105+
//Create a group
106+
string createdGroup = await GroupSnippets.CreateGroupAsync(STORY_DATA_IDENTIFIER);
107+
//Create a user
108+
string createdUser = await UserSnippets.CreateUserAsync(STORY_DATA_IDENTIFIER);
109+
//Add the user to the group
110+
return await GroupSnippets.AddUserToGroup(createdUser, createdGroup);
111+
}
103112
}
104113
}
105114

Microsoft-Graph-Snippets-SDK/MainPage.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ private void CreateStoryList()
106106
StoryCollection.Add(new StoryDefinition() { GroupName = groupsGroupName, Title = ResourceLoader.GetForCurrentView().GetString("CreateGroup"), ScopeGroup = ScopeGroupWorkAdmin, RunStoryAsync = GroupStories.TryCreateGroupAsync });
107107
StoryCollection.Add(new StoryDefinition() { GroupName = groupsGroupName, Title = ResourceLoader.GetForCurrentView().GetString("UpdateGroup"), ScopeGroup = ScopeGroupWorkAdmin, RunStoryAsync = GroupStories.TryUpdateGroupAsync });
108108
StoryCollection.Add(new StoryDefinition() { GroupName = groupsGroupName, Title = ResourceLoader.GetForCurrentView().GetString("DeleteGroup"), ScopeGroup = ScopeGroupWorkAdmin, RunStoryAsync = GroupStories.TryDeleteGroupAsync });
109+
StoryCollection.Add(new StoryDefinition() { GroupName = groupsGroupName, Title = ResourceLoader.GetForCurrentView().GetString("AddMember"), ScopeGroup = ScopeGroupWorkAdmin, RunStoryAsync = GroupStories.TryAddUserToGroup });
109110

110111

111112
var result = from story in StoryCollection group story by story.ScopeGroup into api orderby api.Key select api;

Microsoft-Graph-Snippets-SDK/Users/UserSnippets.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static async Task<IGraphServiceUsersCollectionPage> GetUsersAsync()
8181

8282
public static async Task<string> CreateUserAsync(string userName)
8383
{
84-
string createdUserName = null;
84+
string createdUserId = null;
8585

8686

8787

@@ -107,8 +107,8 @@ public static async Task<string> CreateUserAsync(string userName)
107107

108108
});
109109

110-
createdUserName = user.DisplayName;
111-
Debug.WriteLine("Created user: " + createdUserName);
110+
createdUserId = user.Id;
111+
Debug.WriteLine("Created user: " + createdUserId);
112112

113113
}
114114

@@ -118,7 +118,7 @@ public static async Task<string> CreateUserAsync(string userName)
118118
return null;
119119
}
120120

121-
return createdUserName;
121+
return createdUserId;
122122

123123
}
124124

0 commit comments

Comments
 (0)