0

I'm getting an exception when trying to checkout a file within Sharepoint using CSOM (C#). I was hoping someone could shed some light on what might be wrong?

This is a snippet of the code that's running:

public class SharePointClient : IDisposable
    {
        public ClientContext ClientContext { get; private set; }
        private string FullWebUrl { get; set; }
        public Web Web { get; private set; }        
        public SharePointClient(string fullWebUrl)
        {
            ClientContext = new ClientContext(fullWebUrl);
            Web = ClientContext.Web;
            this.FullWebUrl = fullWebUrl;
        }

        public bool CheckoutFileIfExists(string fileUrl)
        {
            var fileRelativeUrl = new Uri(fileUrl).AbsolutePath;
            var fileItem = ClientContext.Web.GetFileByServerRelativeUrl(fileRelativeUrl);
            ClientContext.Load(fileItem);
            try
            {
                if (ClientContext.HasPendingRequest)
                    ClientContext.ExecuteQuery();

                if (fileItem.Exists)
                    fileItem.CheckOut();

                if (ClientContext.HasPendingRequest)
                    ClientContext.ExecuteQuery();
            }
            catch (Exception e)
            {
                if (e.Message.ToLower() == "file not found.")
                    return false;
                else
                    throw e; //this is thrown
            }
            return true;
        }
    }

This is the calling code. URL is a valid document within my Sharepoint enviroment:

    string url = "http://mysharepointsite.com/sites/a/123456/109876/Shared%20Documents/test%20doc.docx.";
    var xx = new SharePointClient(url);
    xx.CheckoutFileIfExists(url);

The exception is

Cannot contact site at the specified URL http://mysharepointsite.com/sites/a/123456/109876/Shared%20Documents/test%20doc.docx. There is no Web named "/sites/a/123456/109876/Shared Documents/test doc.docx/_vti_bin/sites.asmx".

Clearly it's getting the URL wrong and can't find the Web. I'm unsure why this is though as I have verified that I can get a valid ClientContext from my URL & then by accessing its .Web property it should be valid?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.