0

I have a SharePoint list called complex form, which has the following columns Title, FirstName, LastName, About, Country, State, Street, City, Zip, Email, Company, Photo, the photo is an image column this is the method I am using to make the post request: no image is uploaded after making the request after inspecting the network tab the photo is shown as this- Photo:{}

  private AddContact = async (event: any) => {
    event.preventDefault();

    const {
      Title,
      FirstName,
      LastName,
      About,
      Country,
      State,
      Street,
      City,
      Zip,
      Email,
      Photo,
      Role,
      Company,
    } = this.state;

    const assests = await sp.web.lists.ensureSiteAssetsLibrary();
    const fileItem = await assests.rootFolder.files.add(
      Photo.name,
      Photo,
      true
    );
    const img = {
      ServerRelativeUrl: fileItem.data.ServerRelativeurl,
    };
    const listName = "ComplexForm";

    try {
      const response: ItemAddResult = await sp.web.lists
        .getByTitle(listName)
        .items.add({
          Title: Title,
          FirstName: FirstName,
          LastName: LastName,
          About: About,
          Country: Country,
          State: State,
          Street: Street,
          City: City,
          Photo: JSON.stringify(img),
          Zip: Zip,
          Email: Email,
          Company: Company,
          Role: Role,
        });

      if (response.data) {
        const result = await response.data;
        console.log("Item created:", result);
        toast.success("Request created");
        this.setState((prevState) => ({
          ...prevState,

          Title: "",
          FirstName: "",
          LastName: "",
          About: "",
          Country: "",
          State: "",
          Street: "",
          City: "",
          Photo: null,
          Zip: "",
          Email: "",
          Company: "",
          Role: "",
          isModalOpen: false,
        }));
      } else {
        console.error("Error missing response data");
        toast.error("Unable to create contact");
      }
    } catch (error) {
      console.error("Error creating item:", error);
    }
  };

1 Answer 1

0

I’ve moved the image upload process to after the list item is created. Once the list item is successfully created, the image is then added as an attachment to the newly created list item. This should ensure that the image is correctly associated with the list item. Please try this and let me know if it works for you.

private AddContact = async (event: any) => {
  event.preventDefault();

  const {
    Title,
    FirstName,
    LastName,
    About,
    Country,
    State,
    Street,
    City,
    Zip,
    Email,
    Photo,
    Role,
    Company,
  } = this.state;

  const listName = "ComplexForm";

  try {
    const assests = await sp.web.lists.ensureSiteAssetsLibrary();
    const fileItem = await assests.rootFolder.files.add(
      Photo.name,
      Photo,
      true
    );
    const img = {
      ServerRelativeUrl: fileItem.data.ServerRelativeUrl,
    };

    const response: ItemAddResult = await sp.web.lists
      .getByTitle(listName)
      .items.add({
        Title: Title,
        FirstName: FirstName,
        LastName: LastName,
        About: About,
        Country: Country,
        State: State,
        Street: Street,
        City: City,
        Zip: Zip,
        Email: Email,
        Company: Company,
        Role: Role,
        // Update the Photo column with the URL of the uploaded image
        Photo: img.ServerRelativeUrl,
      });

    if (response.data) {
      const result = await response.data;
      console.log("Item created:", result);
      toast.success("Request created");

      this.setState((prevState) => ({
        ...prevState,

        Title: "",
        FirstName: "",
        LastName: "",
        About: "",
        Country: "",
        State: "",
        Street: "",
        City: "",
        Photo: null,
        Zip: "",
        Email: "",
        Company: "",
        Role: "",
        isModalOpen: false,
      }));
    } else {
      console.error("Error missing response data");
      toast.error("Unable to create contact");
    }
  } catch (error) {
    console.error("Error creating item:", error);
  }
};

Get and show image in SPFx webpart from a Image column in a SP List

11
  • Please this adds an attachment to the list how do update the Photo column instead of adding it to an attachment
    – Daniel. F
    Commented Jan 9 at 16:05
  • Apologies for the confusion, I've updated the code to update the Photo field directly with the ServerRelativeUrl of the uploaded image when adding the new item to the list. Commented Jan 9 at 16:08
  • After updating the code i got this error: data.error: {code: "-1, Newtonsoft.Json.JsonReaderException",…}} odata.error : {code: "-1, Newtonsoft.Json.JsonReaderException",…} code : "-1, Newtonsoft.Json.JsonReaderException" message : {lang: "en-US", value: "Error parsing comment. Expected: *, got s. Path '', line 1, position 1."} lang : "en-US" value : "Error parsing comment. Expected: *, got s. Path '', line 1, position 1."
    – Daniel. F
    Commented Jan 9 at 16:15
  • Try passing the image directly Photo: img, instead of Photo: img.ServerRelativeUrl, Commented Jan 9 at 16:17
  • If the above doesn't work can you check the format that your SharePoint list expects for the Photo field. You might need to adjust the img object to match this format. Commented Jan 9 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.