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


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

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

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

      ListItem item = folder.ListItemAllFields;

This code works fine with SharePoint Online.

  • Use this to get specific file context.Web.GetFileByServerRelativeUrl(relativePath), if you know relative path.
    – Ariwibawa
    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.