如何在线检查文件是否存在于共享点中?

How to check if file exist in sharepoint online?

我正在在线使用共享点并使用代码,我需要检查文件是否存在。请不要我不需要下载文件。如果文件存在,我的代码将使用它的 url,因此无需下载它。

下面的代码尝试下载我不想要的文件:

class Program
{
    static void Main(string[] args)
    {
        string Web = @”https://domain/sitecollection/“;
        string FileName = @”/Sitecollection/Records/file.txt”;

        ClientContext clientContext = new ClientContext(Web);
        Web site = clientContext.Web;

        if (TryGetFileByServerRelativeUrl(site, FileName)) Console.WriteLine(“File exists”); elseConsole.WriteLine(“File does not exist”);
        Console.ReadLine();
    }

    public static bool TryGetFileByServerRelativeUrl(Web web, string serverRelativeUrl)
    {
        var ctx = web.Context;
        try
        {
            File file = web.GetFileByServerRelativeUrl(serverRelativeUrl);
            ctx.Load(file);
            ctx.ExecuteQuery();
            return true;
        }
        catch (Microsoft.SharePoint.Client.ServerException ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(ex.Message);
            if (ex.ServerErrorTypeName == “System.IO.FileNotFoundException”)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                return false;
            }
            return false;
        }
        catch (Exception exp)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(exp.Message);
            return false;
        }
    }
}

请帮助我如何在不下载文件的情况下检查文件是否存在。

使用CAML查询根据文件url进行查询。

    var query = new CamlQuery();
    query.ViewXml = string.Format("<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FileRef\"/><Value Type=\"Url\">{0}</Value></Eq></Where></Query></View>",fileUrl);
    var items = list.GetItems(query);
    ctx.Load(items);
    ctx.ExecuteQuery();
    return items.Count > 0;