<span class="token keyword keyword-async">async</span> <span class="token keyword keyword-function">function</span> <span class="token function">getUserProfiles</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword keyword-const">const</span> accessToken <span class="token operator">=</span> <span class="token string">"<your_access_token>"</span><span class="token punctuation">;</span> <span class="token comment">// Replace with your OAuth access token</span>
<span class="token keyword keyword-const">const</span> endpoint <span class="token operator">=</span> <span class="token string">"https://<tenant-name>.sharepoint.com/_api/search/postquery"</span><span class="token punctuation">;</span>
<span class="token keyword keyword-const">const</span> requestBody <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token string-property property">"request"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token string-property property">"__metadata"</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token string-property property">"type"</span><span class="token operator">:</span> <span class="token string">"Microsoft.Office.Server.Search.REST.SearchRequest"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token string-property property">"Querytext"</span><span class="token operator">:</span> <span class="token string">"*"</span><span class="token punctuation">,</span> <span class="token comment">// or specific query text, like "Department:IT"</span>
<span class="token string-property property">"SourceId"</span><span class="token operator">:</span> <span class="token string">"B09A7990-05EA-4AF9-81EF-EDFAB16C4E31"</span><span class="token punctuation">,</span> <span class="token comment">// People result source</span>
<span class="token string-property property">"SelectProperties"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token string-property property">"results"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token string">"PreferredName"</span><span class="token punctuation">,</span>
<span class="token string">"WorkEmail"</span><span class="token punctuation">,</span>
<span class="token string">"JobTitle"</span><span class="token punctuation">,</span>
<span class="token string">"Department"</span><span class="token punctuation">,</span>
<span class="token string">"PictureURL"</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token string-property property">"RowLimit"</span><span class="token operator">:</span> <span class="token number">10</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword keyword-const">const</span> response <span class="token operator">=</span> <span class="token keyword control-flow keyword-await">await</span> <span class="token function">fetch</span><span class="token punctuation">(</span>endpoint<span class="token punctuation">,</span> <span class="token punctuation">{</span>
<span class="token literal-property property">method</span><span class="token operator">:</span> <span class="token string">'POST'</span><span class="token punctuation">,</span>
<span class="token literal-property property">headers</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token string-property property">"Authorization"</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Bearer </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>accessToken<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
<span class="token string-property property">"Accept"</span><span class="token operator">:</span> <span class="token string">"application/json;odata=verbose"</span><span class="token punctuation">,</span>
<span class="token string-property property">"Content-Type"</span><span class="token operator">:</span> <span class="token string">"application/json;odata=verbose"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">body</span><span class="token operator">:</span> <span class="token known-class-name class-name">JSON</span><span class="token punctuation">.</span><span class="token method function property-access">stringify</span><span class="token punctuation">(</span>requestBody<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword control-flow keyword-if">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token property-access">ok</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword keyword-const">const</span> data <span class="token operator">=</span> <span class="token keyword control-flow keyword-await">await</span> response<span class="token punctuation">.</span><span class="token method function property-access">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword keyword-const">const</span> results <span class="token operator">=</span> data<span class="token punctuation">.</span><span class="token property-access">d</span><span class="token punctuation">.</span><span class="token property-access">postquery</span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">PrimaryQueryResult</span></span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">RelevantResults</span></span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Table</span></span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Rows</span></span><span class="token punctuation">.</span><span class="token property-access">results</span><span class="token punctuation">;</span>
results<span class="token punctuation">.</span><span class="token method function property-access">forEach</span><span class="token punctuation">(</span><span class="token parameter">user</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">log</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token literal-property property">Name</span><span class="token operator">:</span> user<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Cells</span></span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token parameter">cell</span> <span class="token arrow operator">=></span> cell<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Key</span></span> <span class="token operator">===</span> <span class="token string">'PreferredName'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Value</span></span><span class="token punctuation">,</span>
<span class="token literal-property property">Email</span><span class="token operator">:</span> user<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Cells</span></span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token parameter">cell</span> <span class="token arrow operator">=></span> cell<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Key</span></span> <span class="token operator">===</span> <span class="token string">'WorkEmail'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Value</span></span><span class="token punctuation">,</span>
<span class="token literal-property property">JobTitle</span><span class="token operator">:</span> user<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Cells</span></span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token parameter">cell</span> <span class="token arrow operator">=></span> cell<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Key</span></span> <span class="token operator">===</span> <span class="token string">'JobTitle'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Value</span></span><span class="token punctuation">,</span>
<span class="token literal-property property">Department</span><span class="token operator">:</span> user<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Cells</span></span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token parameter">cell</span> <span class="token arrow operator">=></span> cell<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Key</span></span> <span class="token operator">===</span> <span class="token string">'Department'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Value</span></span><span class="token punctuation">,</span>
<span class="token literal-property property">Picture</span><span class="token operator">:</span> user<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Cells</span></span><span class="token punctuation">.</span><span class="token method function property-access">find</span><span class="token punctuation">(</span><span class="token parameter">cell</span> <span class="token arrow operator">=></span> cell<span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Key</span></span> <span class="token operator">===</span> <span class="token string">'PictureURL'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access"><span class="token maybe-class-name">Value</span></span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword control-flow keyword-else">else</span> <span class="token punctuation">{</span>
<span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token method function property-access">error</span><span class="token punctuation">(</span><span class="token string">"Error fetching user profiles:"</span><span class="token punctuation">,</span> response<span class="token punctuation">.</span><span class="token property-access">status</span><span class="token punctuation">,</span> response<span class="token punctuation">.</span><span class="token property-access">statusText</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token function">getUserProfiles</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>