SharePoint / PnP-JS-Core

Code moved to https://github.com/pnp/pnpjs. This repository is archived.
Other
379 stars 231 forks source link

.expand is not working #774

Closed hmendezm closed 6 years ago

hmendezm commented 6 years ago

Category

[ ] Enhancement

[ ] Bug

[X] Question

Version

Please specify what version of the library you are using: [ v3.0.5 ]

Expected / Desired Behavior / Question

I need to expand a field Sp.FieldUser (people field) to get title and email. The problem is when I use the .expand("Names") where Names is the field name , I get the error "ProcessHttpClientResponseException: Error making HttpClient request in queryable: [400] Bad Request"

I thought that the error was the field name but when I run the code below I get the name of the field in the list. This means I get the correct name. If I remove .expand and remove Names/title and Names/Email I do not get errors. Do you have any idea what can be the issue?

Array (67)
0 {Title: "Content Type ID", EntityPropertyName: "ContentTypeId"}
1 {Title: "Title", EntityPropertyName: "Title"}
2 {Title: "Approver Comments", EntityPropertyName: "OData__ModerationComments"}
3 {Title: "File Type", EntityPropertyName: "File_x0020_Type"}
4 {Title: "Group", EntityPropertyName: "Group"}
5 {Title: "Display Name", EntityPropertyName: "Display_x0020_Name"}
6 {**Title: "Names", EntityPropertyName: "Names"**}
**CODE TO VERIFY THE FIELD NAMES**
pnp.sp.web.lists
  .getByTitle('[Lists_Title]')
  .fields
  .select('Title, EntityPropertyName')
  .filter(`Hidden eq false and Title eq '[Field's_Display_Name]'`)
  .get()
  .then(response => {
    console.log(response.map(field => {
      return {
        Title: field.Title,
    EntityPropertyName: field.EntityPropertyName
      };
    }));
  })
  .catch(console.log);
**CODE TO GET NAMES TITLE AND EMAIL**
 const web = new Web(this.DeviceRequestUrl);
    // use odata operators for more efficient queries
    return (
      web
      .lists
        .getByTitle(this.DistributionList)
        .items
        .select(
          "ID,Title,Group, " +
          " Names/Title, Names/EMail," +
          " Division,Role,IsDelegate," +
          " LoginName," +
          " DivisionGroup"
        )
        .expand("Names")
        .top(1)
        .get()
        .then(result => {
              console.log(result);
        })
    );
koltyakov commented 6 years ago

Hey @hmendezm,

People filed expand should work just fine:

 pnp.sp.web.lists.getByTitle(_spPageContextInfo.listTitle)
  .items.getById(1)
  .select('Author/Id,Author/Title,Author/Name,Author/EMail')
  .expand('Author')
  .get().then(console.log)

image

Please check response message in network tab:

image

Some of the fields provided in select probably do not exist.

hmendezm commented 6 years ago

Thanks Koltyakov for your reply and time,

As I mentioned in my original port. If I remove .expand("Names") and all references to the "Names" field in the .select, I do not get errors. so this means the rest of the fields are ok. also I verify the fields exist in two ways the script above and by make restfull call in the browser. I know .expand works. It just is not working for this specific custom list and this is a simple list with two people fields and basic fields. Nothing special. Actual I tried with Author ("Created BY") field and I got the same problem.

koltyakov commented 6 years ago

Hm... corrupted list... What is in the error message exactly? I mean not the error "ProcessHttpClientResponseException: Error making HttpClient request in queryable: [400] Bad Request" but the message in response (as shown in the screenshot above).

hmendezm commented 6 years ago

Hi This is the error msg. It looks like the Names filed is not valid although the field is in the list.

{
    "error": {
        "code": "-1, Microsoft.SharePoint.SPException",
        "message": {
            "lang": "en-US",
            "value": "The query to field 'Names' is not valid. The $select query string must specify the target fields and the $expand query string must contains Names."
        }
    }
}
koltyakov commented 6 years ago

Hm... such message usually shows up when select doesn't contain any props from the expanded field or there is no expand by a field when props are in the select. But staring at your initial sample I can't see a reason to cause this exception. Which leads me to the idea of "something wrong with the list" rather than with the lib or a mistake in a code...

hmendezm commented 6 years ago

Ok. I think I found the problem. the problem is the data of the field. There are items where the user is System Account and this does not have all properties. is a way to filter all item where the properties that I need are not present eg. UserName, email, Title?

Thanks for you help

russgove commented 6 years ago

i had a similar issue recently. i have a list with a user field, and a test account with no email.

if i make a rest call to get the list items and expand the user field, and request the user/email, i get an invalid request if it has the test user. Without the test user it works fine.

the test user is an account that is sunched from our local ad

Sent from my iPad

On Apr 5, 2018, at 5:42 PM, H. Mendez notifications@github.com wrote:

Ok. I think I found the problem. the problem is the data of the field. There are items where the user is System Account and this does not have all properties. is a way to filter all item where the properties that I need are not present eg. UserName, email, Title?

Thanks for you help

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

patrick-rodgers commented 6 years ago

Going to close this as answered, please reopen should you need to continue the conversation or the question isn't resolved. Thanks!