How can I get the sharepoint groups the current user is a part/ belogns to, without @pnp/sp
js framework in SPFx ?
2 Answers
You can use spHttpClient
built-in with SPFx.
The endpoint you need to consume is /_api/web/currentuser/groups
.
Add the below import statement:
import { SPHttpClient } from '@microsoft/sp-http';
After that you can add the below code:
const queryUrl = `${this.context.pageContext.site.absoluteUrl}/_api/web/currentuser/groups`;
const siteGroupsData = await this.context.spHttpClient.get(queryUrl, SPHttpClient.configurations.v1);
const siteGroups = (await siteGroupsData.json()).value;
siteGroups.forEach((siteGroup) => console.log(siteGroup));
-
-
-
i wrote this method inside the render method of
<webpartname>.ts
file Apr 18, 2019 at 14:54 -
-
First you need to import the library
import { CurrentUser } from '@pnp/sp/src/siteusers';
Then call the function to get the data
private getSPUserData(): void {
sp.web.currentUser.get().then((r: CurrentUser) => {
this.renderData(r['Title']);
});
}
Where "r" is the SPUser Object, from this you can get
- LoginName
- Title (Name)
Then you can user this query to ask fro the user groups
private getSPUserGroup(): void {
sp.web.currentUser.groups.get().then((r: any) => {
let grpNames: string ="";
r.forEach((grp: SiteGroup) =>{
grpNames += "<li>"+grp["Title"]+"</li>"
});
grpNames = "<ul>"+grpNames+"</ul>";
this.renderData(grpNames);
});
}
Hope it can help you. Sya
-
1This does not answer the question. OP specifically asks how to get a user's Group and to do so without the use of pnp. Apr 18, 2019 at 16:56
-