<p>Vous pourriez envisager les approches suivantes pour déterminer si un fichier existe ou non.</p>
<p>Basée sur une requête</p>
<p>Vous pourriez construire une requête CAML pour trouver un élément de liste par son URL comme démontré ci-dessous :</p>
<pre><code class="lang-auto">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;
}
</code></pre>
<p>Utilisation</p>
<pre><code class="lang-auto">using (var ctx = GetSPOContext(webUri,userName,password))
{
var list = ctx.Web.Lists.GetByTitle(listTitle);
if(FileExists(list,"/documents/SharePoint User Guide.docx"))
{
//...
}
}
</code></pre>
<p>Méthode <code>Web.GetFileByServerRelativeUrl</code></p>
<p>Utilisez la <a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.web.getfilebyserverrelativeurl%28v=office.15%29.aspx">méthode Web.GetFileByServerRelativeUrl</a> pour retourner l’objet fichier situé à l’URL relative au serveur spécifiée.</p>
<p>Si le fichier n’existe pas, l’exception <a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.serverexception.aspx">Microsoft.SharePoint.Client.ServerException</a> sera rencontrée :</p>
<pre><code class="lang-auto"> 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;
}
}
</code></pre>
<p>Utilisation :</p>
<pre><code class="lang-auto"> using (var ctx = GetSPOContext(webUri,userN
(Réponse tronquée)</code></pre>