TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Gordana Tasic
1.6k
96
13.4k
How to display channels in ListView using Graph API?
Mar 18 2021 11:37 AM
I’m developing a small Windows form app to test Graph API functions. I have two functionalities in the application, user's log in and get channels for specified team. I created a class that contains functions for user login and for returning channels for specified team. I have a ListView on Form in which I want to show all the channels, but when I call a function for returning channels in button event, nothing happens, nothing is displayed in the ListView. Here is the code:
public
static
class
GraphHelper
{
public
static
GraphServiceClient graphClient;
public
static
string
token;
private
static
string
[] scopes =
new
string
[] {
"user.read"
};
public
static
string
TokenForUser =
null
;
public
static
DateTimeOffset expiration;
private
const
string
ClientId =
"599ed98d-4356-4a96-ad37-04391e9c48dc"
;
private
const
string
Tenant =
"common"
;
// Alternatively "[Enter your tenant, as obtained from the Azure portal, e.g. kko365.onmicrosoft.com]"
private
const
string
Authority =
"https://login.microsoftonline.com/"
+ Tenant;
// The MSAL Public client app
private
static
IPublicClientApplication PublicClientApp;
private
static
string
MSGraphURL =
"https://graph.microsoft.com/beta/"
;
private
static
AuthenticationResult authResult;
public
static
GraphServiceClient GetGraphClient(
string
token)
{
if
(graphClient ==
null
)
{
// Create Microsoft Graph client.
try
{
graphClient =
new
GraphServiceClient(
"https://graph.microsoft.com/v1.0"
,
new
DelegateAuthenticationProvider(
async (requestMessage) =>
{
requestMessage.Headers.Authorization =
new
AuthenticationHeaderValue(
"bearer"
, token);
// This header has been added to identify our sample in the Microsoft Graph service. If extracting this code for your project please remove.
requestMessage.Headers.Add(
"SampleID"
,
"uwp-csharp-snippets-sample"
);
}));
return
graphClient;
}
catch
(Exception ex)
{
Debug.WriteLine(
"Could not create a graph client: "
+ ex.Message);
}
}
return
graphClient;
}
public
static
async Task<
string
> GetTokenForUserAsync()
{
if
(TokenForUser ==
null
|| expiration <= DateTimeOffset.UtcNow.AddMinutes(10))
{
PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithAuthority(Authority)
.WithRedirectUri(
"https://login.microsoftonline.com/common/oauth2/nativeclient"
)
.WithLogging((level, message, containsPii) =>
{
Debug.WriteLine($
"MSAL: {level} {message} "
);
}, LogLevel.Warning, enablePiiLogging:
false
, enableDefaultPlatformLogging:
true
)
.Build();
// It's good practice to not do work on the UI thread, so use ConfigureAwait(false) whenever possible.
IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync().ConfigureAwait(
false
);
IAccount firstAccount = accounts.FirstOrDefault();
try
{
authResult = await PublicClientApp.AcquireTokenSilent(scopes, firstAccount)
.ExecuteAsync();
}
catch
(MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
Debug.WriteLine($
"MsalUiRequiredException: {ex.Message}"
);
authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
.ExecuteAsync()
.ConfigureAwait(
false
);
}
TokenForUser = authResult.AccessToken;
}
return
TokenForUser;
}
public
static
async Task<User> GetMeAsync(
string
token)
{
GraphHelper.graphClient = GraphHelper.GetGraphClient(token);
try
{
// GET /me
return
await GraphHelper.graphClient.Me
.Request()
.Select(u =>
new
{
u.DisplayName
})
.GetAsync();
}
catch
(ServiceException ex)
{
Console.WriteLine($
"Error getting signed-in user: {ex.Message}"
);
return
null
;
}
}
public
static
async Task<IEnumerable<Channel>> GetChannels(
string
teamId)
{
graphClient = GetGraphClient(token);
var channels = await graphClient.Teams[teamId].Channels
.Request()
.GetAsync();
return
channels;
}
}
public
partial
class
Form1 : Form
{
public
static
GraphServiceClient graphClient;
public
static
string
token;
public
Form1()
{
InitializeComponent();
}
private
async
void
button1_Click(
object
sender, EventArgs e)
{
token = await GraphHelper.GetTokenForUserAsync();
User graphUser = await GraphHelper.GetMeAsync(token);
label2.Text = graphUser.DisplayName;
}
private
async
void
button3_Click(
object
sender, EventArgs e)
{
var channels = GraphHelper.GetChannels(
"8557483b-a233-4710-82de-e1bdb03bb9a9"
).Result;
foreach
(var ch
in
channels)
{
ListViewItem item =
new
ListViewItem(
new
string
[] { ch.DisplayName, ch.Id});
listView1.Items.Add(item);
}
}
}
Does anyone know how to solve this?
Reply
Answers (
2
)
To Provide index as AA,AB,AC......So on for the list of records in PDF
How to delete unchecked value