admin管理员组

文章数量:1203816

I am using the following function to fetch all users from SharePoint:

const  fetchUsers = (): void => {
    setLoading(true);
    const listUrl = `${Context.pageContext.web.absoluteUrl}/_api/web/siteusers`;
    console.log('List URL is', listUrl);
    console.log('Context.pageContext.web:', Context.pageContext.web);

    Context.spHttpClient.get(listUrl, SPHttpClient.configurations.v1)
      .then((response: SPHttpClientResponse) => response.json())
      .then((data) => {
        console.log('Fetched users:', data.value);
        const users: User[] = data.value.map((user: SPUser) => ({
          id: user.Id,
          name: user.Title,
          email: user.Email,
        }));
        setAllUsers(users); // Store all users once fetched
        setOptions(users); // Populate options initially
        setLoading(false);
      })
      .catch((error) => {
        setLoading(false);
        console.error('Error fetching users:', error);
      });
  };

Although I am successfully fetching all users, I am encountering an issue when submitting the data to a SharePoint list. Specifically, the Attendees column in the list remains empty after submission.

Here’s the payload I am sending:

const payload = {
    Title: formData.title,
    Description: formData.description,
    StartDate: formattedStartDate, // SharePoint date-time format
    EndDate: formattedEndDate, // SharePoint date-time format
    Category: formData.category === "" ? "Other" : formData.category,
    Allday: isDateOnly, // All-day event flag

    // AttendeesId: [9,15] // These are the correct IDs when manually inserted, and the data appears in the list.
    
    AttendeesId: attendees.map(attendee => attendee.id) // IDs fetched using /siteusers endpoint.
};

The problem is that the IDs fetched from the /siteusers endpoint (e.g., [422, 203]) do not match the correct IDs expected by the SharePoint User or Group column (e.g., [9, 15]).

Although I know I can use Microsoft Graph API to fetch user data, I want to know:

  • Is it possible to get the correct user IDs using SharePoint REST API?
  • If yes, can you provide the correct endpoint for this?

Any guidance would be greatly appreciated!

I have already tried using the API endpoint: /_api/web/siteusers

While this fetches user data successfully, the issue is that the IDs returned by this endpoint do not match the IDs required by the SharePoint** User or Group** column.

I want to know:

Is there any approach to get the correct IDs that match the SharePoint User or Group column? Any help or guidance would be greatly appreciated!

本文标签: How to Retrieve All Users with Correct User IDs in SharePoint SPFxStack Overflow