web-dev-qa-db-ja.com

C#CSOM-ファイルがドキュメントライブラリに存在するかどうかを確認する

私はCSOMを使用してC#でコーディングしています。私のアプリはテンプレートasp.netページを "/ Pages /"ライブラリにアップロードします。ファイルをアップロードする前に、同じ場所に同じ名前のファイルが存在するかどうかを確認する必要があります(ブール値を返す場合があります)。

簡単に調べましたが、見つけたソリューションの大部分はJavascriptの使用に言及しているか、オンプレミス展開に適用されていました。

誰かが私を正しい方向に向けてくれたら幸いです。

9
Junior

ファイルが存在するかどうかを判断するには、次の方法を検討できます。

クエリベース

以下に示すように、CAMLクエリを作成して、URLでリストアイテムを検索できます。

public static bool FileExists(List list, string fileUrl)
{
    var ctx = list.Context;
    var qry = new CamlQuery();
    qry.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(qry);
    ctx.Load(items);
    ctx.ExecuteQuery();
    return items.Count > 0;
}

使用法

using (var ctx = GetSPOContext(webUri,userName,password))
{
     var list = ctx.Web.Lists.GetByTitle(listTitle);
     if(FileExists(list,"/documents/SharePoint User Guide.docx"))
     {
          //...
     }
}

Web.GetFileByServerRelativeUrl 方法

Web.GetFileByServerRelativeUrlメソッド を使用して、指定されたサーバー相対URLにあるファイルオブジェクトを返します。

ファイルが存在しない場合、例外 Microsoft.SharePoint.Client.ServerException が発生します:

  public static bool TryGetFileByServerRelativeUrl(Web web, string serverRelativeUrl,out Microsoft.SharePoint.Client.File file)
    {
        var ctx = web.Context;
        try{
            file = web.GetFileByServerRelativeUrl(serverRelativeUrl);
            ctx.Load(file);
            ctx.ExecuteQuery();
            return true;
        }
        catch(Microsoft.SharePoint.Client.ServerException ex){
            if (ex.ServerErrorTypeName == "System.IO.FileNotFoundException")
            {
                file = null;
                return false;
            }
            else
                throw;
        }
    }

使用法:

 using (var ctx = GetSPOContext(webUri,userName,password))
 {
      Microsoft.SharePoint.Client.File file;
      if(TryGetFileByServerRelativeUrl(ctx.Web,"/documents/SharePoint User Guide.docx",out file))
      {
          //...
      }
 }    
15

クライアントOMを使用している場合、ファイルが存在しないと実際には例外がスローされます。

using(var clientContext = new ClientContext(site))
{
     Web web = clientContext.Web;
     Microsoft.SharePoint.Client.File file = web.GetFileByServerRelativeUrl("/site/doclib/folder/filename.ext");
     bool bExists = false;
     try
     {
         clientContext.Load(file);
         clientContext.ExecuteQuery(); //Raises exception if the file doesn't exist
         bExists = file.Exists;  //may not be needed - here for good measure
     }
     catch{   }

     if (bExists )
     {
           .
           .
     }
}

リソース

7
Harshini Hegde

Linq ToSharePointの使用

    public bool FolderExists(string library, string name) {

        using (var ctx = SPStatic.Context(_sharePointSiteUrl)) {

            List sharedDocs = ctx.Web.Lists.GetByTitle(library);

            var query = ctx.LoadQuery(sharedDocs.RootFolder.Folders.Where(fd => fd.Name == name));

            ctx.ExecuteQuery();

            Folder f = query.SingleOrDefault();

            return f != null;

        }

    }
3
EthR

Web.GetFileByServerRelativeUrl(@vadim上に投稿)の代替はLoad(checkFile, p => p.Exists);であり、コンテキストでは...

using (ClientContext ctx = new ClientContext("https://yoursubdomainhere.sharepoint.com/"))
{
    Web web = ctx.Web;
    Microsoft.SharePoint.Client.File checkFile = web.GetFileByServerRelativeUrl("/sites/Documents/MyFile.docx");

    ctx.Load(checkFile, fe => fe.Exists);
    ctx.ExecuteQuery();
    if (!checkFile.Exists)
    {
        //Do something here
    }
}
3
stinkyfriend