Good morning every SharePoint fellow ! I know this question has surely already been posted, but i could'n't found a clear answer.
Context
I am programming a custom WebPart for Sharepoint online with all the good tools (react, spfx, etc..). I am really new to all this process.
I have a SP Document Library with a custom column called "UserId". This column will help to know if the user has already read the doc.
My custom WebPart will be kind of a "Read/Unread" tool. This WP will read that library and fetch all the items, witch the current user didn't already read. I would like to trigger the items where my current user's ID does not appear in my UserId column. I am quite confident with it, but i am blocking on one simple thing :
My question : How to retrieve the current User's ID?
What i did
So I did create a project, according the tutorial on the crud react video. Now, i would like to get my current user id. I did code a method using pnp in my MyCustomWPWebPart.ts :
import pnp from '@pnp/pnpjs';
public getMyID(){
var myID = "";
pnp.setup({
sp: {
headers: {
"Accept": "application/json; odata=verbose"
}
}
});
pnp.sp.profiles.myProperties.get().then(d => {
var properties = d.UserProfileProperties.results;
var myID = properties[44].Value.split(',')[0].substr(3);// get object 44's value ("CN=ID,OU=..."), split it to get the first one, then get rid of the "CN=" and there you go, a nice little 36char ID
return myID;
});
}
This code works.. but i think i did'nt get it in the right place. And moreover, i don't know how to use it in my MyCustomWP.tsx file. I don't know how i can call my function getMyID.
What I would like
I would like to be able to retrieve this userId in my MyCustomWP.tsx .. and it is quite difficult. What do I do wrong ? Is there any other (simple) way to do it ?
In advance, thank you for your time, I really am a beginner with react and spfx, so i can be really long to understand where everything goes.