none
Sharepoint 2013 Check If File Already Exists In Document Library Using CSOM RRS feed

Answers

  • http://www.c-sharpcorner.com/blogs/sharepoint-2013-check-file-already-exists-in-document-library-using-csom

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text.RegularExpressions;

    using Microsoft.SharePoint.Client;

    namespace Rextester

    {

    public class Program

    {

    public static void Main(string[] args)

    {

    ClientContext context = new ClientContext("http://gowtham.sharepoint.com");

    Web web = context.Web; Microsoft.SharePoint.Client.File file; if(TryGetFileByServerRelativeUrl(Web,"/documents/test.docx",out file))

    { //File Already Exists }

    }

    }

    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;

    }

    }

    }

    Tuesday, April 11, 2017 11:40 AM