pnp / pnpjs

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

Are Shared folders/files treated differently? #3103

Closed SharonGilmore closed 1 month ago

SharonGilmore commented 1 month ago

What version of PnPjs library you are using

3.x

Minor Version Number

1.0

Target environment

SharePoint Framework

Additional environment details

I'm not sure which version of PnPjs I'm using - I ran the npm install command about 6 weeks ago, so I assume it's whatever would have been the default then?

I'm using nodeJS.

Question/Request

I've created a SharePoint Web Part using SPFx - the Web Part is basically a different way to render a Document Library on a SharePoint site, rather than using the built-in one.

The WP is given the name of a Document Library, and it then reads in the list of files and folders in that library, and displays them. This is mostly working fine, except for one particular folder, which is a Shared folder - the WP doesn't seem to pull in the details of this folder at all, so even though we can see files and folders in the Site Contents page, they're not showing up in the WP.

To get the list of files and folders I do the following.

First, I set up a function called getSP, which sets up the connection:

let _sp: SPFI | undefined = undefined;

export const getSP = (context?: WebPartContext): SPFI => {
  if (context !== undefined) {
    _sp = spfi().using(SPFx(context));
  }
  return _sp;
};

Then in my WP, I have:

const CustomDocLib: any = ({rootFolder}:any) => {
  const sp: SPFI = getSP();
  const spCache = spfi(sp).using(Caching({ store: "session" }));

  const readFolders = async (): Promise<void> => {
    try {
      // Get path of root folder
      await spCache.web.lists
        .getByTitle(rootFolder)
        .select("Title", "RootFolder/ServerRelativeUrl")
        .expand("RootFolder")()
        .then((res) => setRootPath(res.RootFolder.ServerRelativeUrl));

      // Get all items underneath the root folder
      const items: any[] = await spCache.web.lists
        .getByTitle(rootFolder)
        .items.select()
        .expand("Folder, File")();

      // Get base server url
      let baseServerUrl = "";
      if (items[0].Folder) {
        const splitUrl = items[0].Folder.ServerRelativeUrl.split("/");
        splitUrl.pop();
        splitUrl.pop();
        baseServerUrl = splitUrl.join("/") + "/";
      } else {
        const splitUrl = items[0].File.ServerRelativeUrl.split("/");
        splitUrl.pop();
        splitUrl.pop();
        baseServerUrl = splitUrl.join("/") + "/";
      }

      // Parse to get folder structure
      const objectArray = items
        .map((i) => {
          if (i.Folder)
            return i.Folder.ServerRelativeUrl.replace(baseServerUrl, "")
              .split("/")
              .slice(1)
              .map((item: any) => {
                return { item: i, type: "folder", path: item };
              });
          else
            return i.File.ServerRelativeUrl.replace(baseServerUrl, "")
              .split("/")
              .slice(1)
              .map((item: any) => {
                return { item: i, type: "file", path: item };
              });
        })
        .reduce((children, item) => insert(children, item), []);

      setAllFolders(objectArray);
    } catch (error) {
      console.log(error);
      setError(true);
    }
  };

  // Initialise by reading and parsing list of folders and files
  React.useEffect(() => {
    const initialise = async () => {
      await readFolders();
    };

    initialise();
  }, []);

return ...;
});

export default CustomDocLib;

As I said, this seems to work fine for all folders apart from the one shared one - do they work differently in some way? Do I need to do something different to retrieve them? Or is there any other reason why the approach above might not work for all folders?

Thanks!

bcameron1231 commented 1 month ago

Hi. Shared Folders should not behave any differently.

To confirm, if you look in the Network tab response, you're not seeing the folder when you make this call?


    const items: any[] = await spCache.web.lists
      .getByTitle(rootFolder)
      .items.select()
      .expand("Folder, File")();
bcameron1231 commented 1 month ago

Closing this due to inactivity. If you continue to have issues please open a new issue, link to this issue, and provide any additional details available. Thanks!

github-actions[bot] commented 1 month 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.