How to get Teams Organization Hierarchical data in C# using Graph API - c#

In microsoft teams, there is a tab titled "Organization", which shows something like this:
Is there any way I can get this data in C# by using Graph API?
Right now I have
var users = await graphClient.Users.Request().GetAsync();
which returns an array of all users, and each user has their name and job title. This is not enough to make the org chart, because it does not tell how different users relate. What Graph API call do I need to make in order to get the data to make the org chart?

If you are using Microsoft Graph SDK for C#, you can use the code below to get users with the manager:
var usersWithMgr = await graphServiceClient.Users.Request().Expand("manager").GetAsync();
Result:

I found out you can make a graph call to users to get all users in a domain, then foreach user, make a call as listed here: https://learn.microsoft.com/en-us/graph/api/user-list-manager?view=graph-rest-1.0&tabs=csharp
This will get the manager, which can be manipulated into a hierarchical view.
UPDATE:
var users = await graphClient.Users.Request().Expand("manager")
.Select(u => new { u.DisplayName, u.JobTitle, u.AccountEnabled}).GetAsync();

Related

Get All DriveItems from Microsoft Graph SDK C#

I am using Microsoft Graph SDK for C#. Link is as follows ("https://github.com/microsoftgraph/msgraph-sdk-dotnet").
The query I am using to get all the items from the SharePoint site is as follows.
// Get Graph API Token
GraphServiceClient client = await MicrosoftGraphClient.GetGraphServiceClient();
// Fetch all DriveItems from Sharepoint Site
var results = await client.Sites[sharePointId].Drive.Root.ItemWithPath("SalesPortal/Presentations/" + orgnaization).Children.Request(queryOptions).GetAsync();
This query is working perfectly fine but it is fetching only the first 200 records. I need all the records present in that path.
Thanks in advance.
Per the docs: https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/collections.md#getting-a-collection
The results object you have should have a NextPageRequest property if there are more results to fetch.
Keep making the NextPageRequest, aggregating your results as your go, until it comes back null.

How to get user context from Teams private message using MS bot framework

I am currently implementing a Teams bot that has to get the user name (first name and last name) and the user's email address of the person communicating with the bot via a personal chat.
I am using SDK v4 of the bot framework and tried to implement the approach mentioned here (https://github.com/OfficeDev/BotBuilder-MicrosoftTeams-dotnet). The only parameter returned when fetching the teams context is the Tenant Id. Both channel and team are null (I presume this is because I am in a private chat?).
Since I now have the tenant id from the Teams context, how do I use it to retrieve the user's info?
To retrieve the Teams context I'm calling the following:
ITeamsContext teamsContext = turnContext.TurnState.Get<ITeamsContext>();
Once you retrieve the IDs using the ITeamsContext object, you need to use those Ids to fully populate the Teams object. You can do so using the Operations.FetchTeamDetailsAsync method.
To get a roster of members in the conversation, you'll use the GetConversationParametersForCreateOrGetDirectConversation() method. #epicmethodname.
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema.Teams;
using Microsoft.Bot.Connector.Teams;
...
ConversationList channels = await teamsContext.Operations.FetchChannelListAsync(incomingTeamId);
TeamDetails teamInfo = await teamsContext.Operations.FetchTeamDetailsAsync(incomingTeamId);
var roster = teamsContext.GetConversationParametersForCreateOrGetDirectConversation(turnContext.Activity.From).Members;
List<TeamsChannelAccount> rosterTC = roster.ToList().ConvertAll(member =>
{
return teamsContext.AsTeamsChannelAccount(member);
});
await turnContext.SendActivityAsync($"You have {roster.Count} number of people in this group. You are {from.Name}");
You can find some getting started help, and additional resources here: https://developer.microsoft.com/en-us/office/blogs/preview-release-of-net-teams-bot-builder-v4-sdk/

How to get all users that belong in an AppRole using Azure Active Directory Graph API

I can't for the life of me figure out how to query Azure Active Directory's Graph API to get all users that belong to a particular AppRole.
First I tried something like:
client.Users.Where(u => u.AppRoleAssignments.Any(r => r.Id == "some-guid"));
But that won't compile because AppRoleAssignments is a IPagedCollection so you can't do things like .Any on it.
Then I tried to query all the AppRoleAssignments in the ServicePrincipal for my Application:
var servicePrincipal = await client.ServicePrincipals
.Expand(p => p.AppRoleAssignments)
.Where(p => p.AppId == "my app id guid")
.ExecuteSingleAsync();
But servicePrincipal.AppRoleAssignments stubbornly comes back empty seemingly ignoring my .Expand.
I also tried getting the ServicePrincipal directly by ID and doing an Expand:
var principal = await client.ServicePrincipals
.GetByObjectId("feeaae9c-40a3-48a3-8a01-b87343f5ecfc")
.Expand(p => p.AppRoleAssignments)
.ExecuteAsync();
But that just causes an error (which goes away if you remove the .Expand):
{"odata.error":{"code":"Request_BadRequest","message":{"lang":"en","value":"Invalid object identifier 'feeaae9c-40a3-48a3-8a01-b87343f5ecfc()'."}}}
Which is an odd error because the object id in the error message is suffixed with a '()' that is not added by my code.
Am I missing something obvious? Surely there's an easy way to get all users in an AppRole?
I finally figured out the answer. In order get to AppRoleAssignments off the ServicePrincipal you need to query the list directly rather than trying to expand it off the ServicePrincipal:
await client.ServicePrincipals
.GetByObjectId(servicePrincipalObjectId)
.AppRoleAssignedTo
.ExecuteAsync()
Then you have to manually walk through the users and groups to get a final list of users. This could potentially result in many Graph API service calls depending on how many groups and users there are, so be warned!
Edit: As Dan Kershaw mentions in the comments, roles are only applied to users in groups that are directly linked to AppRoles. Sub-groups do not inherit the roles.
I've put up the full solution in a Gist here because it's really too big to put inline here.

Twitter API resource for Windows 8 metro application

I'm developping an application that uses Twitter API to collect informations about users.
I'm using linqToTwitter in my current project but it does not allow me a lot of thing that I want to do.
For example I need getting a follower list of a searched user.
LinqToTwitter allowed me finding a user who the name is given and who is in the follower list of the authenticate user.
The code is the following:
public List<User> RecupererFollower()
{
var friendship =
(from friend in MainPage.twitterCtxProp.Friendship
where friend.Type == FriendshipType.FollowersList
&& friend.SourceScreenName==MainPage.texte
select friend).ToList();
Followers = (from friend in friendship
select new User //Un utilisateur est créé grâce aux données récupérées précédemment.
{
Name = friend.ScreenName
}).ToList(); //Cette partie constitue la liste de tweets récupérés précédemment.
return Followers;
}
But even this doesn't work because this query requires a specific screenName of a particular user.
I don't want this I want more general functions.
What can I do?
Someone knows other resources for Windows 8 metro application?
The #millimoose comment, "an API client library will probably only give you access to the API itself" is quite accurate. The particular query you're trying to use is documented at Handling Friendships. The documentation also refers to the original Twitter endpoint that it supports, which is Followers List in this case. On this particular API, the Twitter documentation states that either user_id or screen_name is required. The library can't support more than is available.
That said, you'll have to look at what's available and can sometimes accomplish your goal. i.e. There are also Social Graph queries that are very efficient because they return User IDs. With those User IDs, you can do a UserType.Lookup query to gather multiple users at a time. Here are a few links for UserType.Lookup queries:
Get all followers using LINQ to Twitter
How to get tweets from a multiple of friends?

How to retrieve rendered Sharepoint WebPart Data using Client Object Model

I am trying to access data remotely from sharepoint 2010 site using client object model. For some restrictions I am not able to use CAML query even if I use I am not able to filter for proper audiences based on user login (if you can help me regarding the CAML query is also fine :: i do not know how to get current user audience name and all audiences using CAML/client object model. This code sits on the mobile site and calling the share point site as shown in my code). This following code works good but not able to get the content from the webpart. Can someone help regarding this.
using (ClientContext ctx = new ClientContext("https://mysite.com/Pages/Default.aspx"))
{
ctx.ExecutingWebRequest += new EventHandler<WebRequestEventArgs> (clientContext_ExecutingWebRequest);
File home=ctx.Web.GetFileByServerRelativeUrl("/Student/Pages/default.aspx");
//get the web part manager
Microsoft.SharePoint.Client.WebParts.LimitedWebPartManager wpm = home.GetLimitedWebPartManager(Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared);
IEnumerable<Microsoft.SharePoint.Client.WebParts.WebPartDefinition> wpds = null;
//create the LINQ query to get the web parts from
//the web part definition collection
wpds = ctx.LoadQuery(wpm.WebParts.Include(wp => wp.Id,wp => wp.WebPart));
//load the list of web parts
ctx.ExecuteQuery();
//enumerate the results
foreach (Microsoft.SharePoint.Client.WebParts.WebPartDefinition wpd in wpds)
{
string title= wpd.WebPart.Title;
Microsoft.SharePoint.Client.WebParts.WebPart wpart = wpd.WebPart;
????? How to render and receive the data (looking for the same data When you browse the site with the browser)
}
Code continues...
I am also struggling with this issue. It really looks like this is not possible with client object model. Actually i've asked it to some SharePoint staff member at Build Conference 2012.
But, with the SharePoint Designer it's actually possible to download the wanted WebPart. Fiddler may come handy to track down which service will deliver you the bits.
Take a look at this post here on SharePoint StackExchange
Unfortunately the post will not give you any concrete way to solve it.
Wish you good luck!

Categories

Resources