Deployed web part is showing errors in console but not for non-local workbench

Brass Contributor

I have deployed a web part to a site collection (where the web part is designed to go) and I am getting errors for 3 particular features on the form. I am using sp.web for these features.

  1. I have created a 'Hello User' which uses this function:

 

public _getUser() { 
    sp.web.currentUser.get().then((user) => {

      this.setState({
        CurrentUserTitle: user.Title,
        FullName: user.Title,
        CurrentUser: user.LoginName,
        CurrentUserID: user.Id,
        CurrentUserEmail: user.Email,

      }

 

 

2. A Departments list drop down which populates with an SP list:

 

 

public _getDepartments() {
  sp.web.lists.getByTitle("Departments").items.get().then((items: any[]) => {
    let returnedDepts: IDropdownOption[] = items.map((item) => { return { key: item.Title, text: item.Title }; });
    this.setState({ DepartmentsList: returnedDepts });
  });
}

 

 

3. A Submit button. The function:

 

 

private _onSubmit() {

      sp.web.lists.getByTitle('Data and Report Form').items.add({

        FullName: this.state.FullName,
        UniId: this.state.UniId,
        Email: this.state.UserEmail,
        Phone: this.state.PhoneNo,
        Department: this.state.SelectedDept,
        SubDepartment: this.state.SubDepartment,
        StartDate: this.state.StartDate,
        DiscoveryDate: this.state.DateOfDiscovery,
        Details: this.state.IncidentDetails,
        PersonalData: this.state.PersonalData

      }).then((iar: ItemAddResult) => {
        console.log(iar);
        let list = sp.web.lists.getByTitle('Data and Report Form');
        list.items.getById(iar.data.Id).update({
            Title: 'DIBR'+iar.data.Id, 
        });
        this.setState({
          JobRef: iar.data.Id,
        });
      });
    }

 

The form works perfectly in non-local SharePoint workbench served on the actual site it's meant to be deployed on. When deployed as a package to the site itself and added as a web part it now shows these errors:

HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (Fetch)GET - https://myDomain.sharepoint.com/sites/IncidentReporting/SitePages/_api/web/currentuser

HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (Fetch)GET - https://myDomain.sharepoint.com/sites/IncidentReporting/SitePages/_api/web/lists/getByTitle('Departments')/items

I get a similar message when trying to submit. A I said, these all work on the non-local workbench.

Additional details:

If I add the webpart to the homepage of the modern site as a web part I get these error (different to the ones if I add the webpart to a page):

HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (Fetch)GET - https://MyDomain.sharepoint.com/sites/_api/web/lists/getByTitle('Departments')/items

HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (Fetch)GET - https://MyDomain.sharepoint.com/sites/_api/web/currentuser

as you can see it's missing the actual site in the URL. Why the difference?

Has anyone got an idea why it's behaving differently when deployed? And why is it showing a different error if I place the web part on different areas of the site collection.

T

 

 

 

0 Replies