pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
743 stars 302 forks source link

Trying to get all associated sites for provided hub Id #2978

Closed cardinalpipkin closed 3 months ago

cardinalpipkin commented 3 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

18.0

Target environment

SharePoint Framework

Additional environment details

All webparts are allowed on tenant. Visual Studio Code. SharePoint Online. Creating WebParts and Extensions.

Question/Request

I'm struggling to get any results when using sp.search. I am passing sp.search a hub Id, I want search to then find all the associated sites for that hub Id. I just don't know what the query should be. I've read the documentation thoroughly. This is what I'm using:

  console.log(hubId, 'hubId in data')
  const _sp = getSP();
  const finalArray: any[] = [];
  const bracketHubId = `{${hubId}}`;
  console.log(bracketHubId, 'brackethubId');
  const url = "https://blah blah/_api/search/query?querytext='DepartmentId:{" + bracketHubId + "} contentclass:STS_Site'&trimduplicates=false&selectproperties='Title,Path,DepartmentId,SiteId,SPSiteUrl,WebTemplate,WebId,SPWebUrl,SiteName,RelatedHubsites,IsHubSite,ParentLink'&rowlimit=1000";

  let q = SearchQueryBuilder().text(url).rowLimit(1000).enablePhonetic;
  let qr = await _sp.search(q);
  console.log(qr, 'qr')
  await _sp.search({
    StartRow: 0,
    Querytext: 'contentclass:STS_Site contentclass:STS_Web',
    // Querytext: url,
    // Querytext: '(contentclass:"STS_Site") (contentclass:"STS_Web")',
    // Querytext: `DepartmentId:${hubId} contentclass:STS_Site`,
    // QueryTemplate: `contentclass: STS_site AND DepartmentId:${'{6e0e55c0-119d-2d81-b11f-693376619b8e}'}`,
    // QueryTemplate: `contentclass: STS_site AND DepartmentId:${bracketHubId}`,

    SelectProperties: [
      "Title",
      "SPSiteUrl",
      "WebTemplate",
      "WebId",
      "SPWebUrl",
      "SiteName",
      "RelatedHubsites",
      "IsHubSite",
      "ParentLink",
      "Department",
      "DepartmentId",
      "Path"
    ],
    // RefinementFilters: [`departmentid:string("${bracketHubId}",linguistics=off)`],
    RowLimit: 500,
    TrimDuplicates: false,

  })
    .then((r: SearchResults) => {
      r.PrimarySearchResults.forEach((value) => {
        finalArray.push(value)
      });
      currentResults = r
    })
  return currentResults;
  // return finalArray;
}

I've left all my attempts in as commented out code. I'm fairly sure my query text is not correct.

bcameron1231 commented 3 months ago

Hi. It looks like you may have too many curly braces for your bracketId.

you've added {} around $hubId const bracketHubId ={${hubId}};

and now you're adding another set of {} around it. const url = "https://blah blah/_api/search/query?querytext='DepartmentId:{" + bracketHubId + "}

Which would result in https://blah blah/_api/search/query?querytext='DepartmentId:{{bracketHubId}}' which isn't valid.

cardinalpipkin commented 3 months ago

Thank you so much for your reply, it now seems to work, but I'm getting some associated sites from some hubId's but not others. Some of the hubs definetly do have associated sites. Is there something wrong with the query?

 await _sp.search({
    StartRow: 0,
    Querytext: '(contentclass:"STS_Site") (contentclass:"STS_Web")',
    QueryTemplate: `contentclass: STS_site AND DepartmentId:${bracketHubId}`,

    SelectProperties: [
      "Title",
      "SPSiteUrl",
      "WebTemplate",
      "WebId",
      "SPWebUrl",
      "SiteName",
      "RelatedHubsites",
      "IsHubSite",
      "ParentLink",
      "Department",
      "DepartmentId",
      "Path"
    ],

Here is the receiving function if it's any use:

const getAssociatedSitesRest = (hubSite: string) => {
    GetChildSites(hubSite).then((associatedSites: SearchResults) => {
      console.log(associatedSites, 'associatedSites results')
      setAssociatedSitesRest(associatedSites.PrimarySearchResults)
    });
  };
bcameron1231 commented 3 months ago

It looks right to me. We're just a passthrough to the search API so it may be something from Microsoft's end causing the issue. There has been a service notice in the admin center that Search has had indexing issues since January. Maybe this could be part of the problem?

cardinalpipkin commented 3 months ago

I'm hoping it is and it's not my query.

github-actions[bot] commented 3 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.