1

I have a document library with files and folders. I want to obtain metadata for specific file or folder with known URL. For this purpose I need an access to list item associated with this file or folder. How to find associated list item using CSOM?

I found similar code in JSOM here: JSOM - get listitem by file URL

I m not sure, if it will work in CSOM and how corresponding code will look. I did not find any relevant info in other answered questions. Thank you.

1 Answer 1

1

I have rewritten code in JSOM - get listitem by file URL to work in CSOM as following.

      var site = clientContext.Site;
      clientContext.Load(site);
      clientContext.ExecuteQuery();
      string root = site.PrimaryUri;
      url = url.Replace(root, "");
      url = site.ServerRelativeUrl + url;

      var folder = clientContext.Web.GetFolderByServerRelativeUrl(url);
      clientContext.Load(folder);
      clientContext.ExecuteQuery();

      ListItem item = folder.ListItemAllFields;
      clientContext.Load(item);
      clientContext.ExecuteQuery();

This code works fine with SharePoint Online.

1
  • Use this to get specific file context.Web.GetFileByServerRelativeUrl(relativePath), if you know relative path.
    – Ariwibawa
    Commented Apr 8, 2021 at 17:39

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.