0

In a web application, I had a C# function to delete files from a SharePoint library.
In SharePoint 2010, everything went fine. In SharePoint 2013, it always returns an error "File Not Found".
Can someone help me ?
Here is the code :

    public void deleteDocument(string strLink)
    {

        ClientContext ctx = new ClientContext(strServerUrl);
        Web currentWeb = ctx.Web;

        ctx.Load(currentWeb);
        ctx.Credentials = new System.Net.NetworkCredential(myLogin, myPwd, myEnvironnement);
        ctx.ExecuteQuery();

        try
        {
            Microsoft.SharePoint.Client.File spfVar = currentWeb.GetFileByServerRelativeUrl(strCollection + strLink);
            ctx.Load(spfVar);

            spfVar.DeleteObject();

            ctx.ExecuteQuery();

        }
        catch (Exception expt)
        {

        }
    }

If the file is located at the following address :
"http//myServer/sites/documentCollection/Library/document.pdf"

  • strServerUrl="http//myServer"
  • strCollection="/sites/documentCollection"
  • strLink=/Library/document.pdf"

    Thank you for your help.
2

Most probably this error occurs since you are trying to load a file that is going to be deleted:

Microsoft.SharePoint.Client.File spfVar = currentWeb.GetFileByServerRelativeUrl(strCollection + strLink);
ctx.Load(spfVar);  //<- you cant load a file that is is going to be deleted
spfVar.DeleteObject(); //delete file object
ctx.ExecuteQuery();  //submit changes to server

Solution

The solution would be to remove the line:

ctx.Load(spfVar);  
  • If I remove the line, I get another error "Access denied. You do not have permission to perform this action or access this resource."... but if I go manually into SharePoint using the same credentials, I can access the document and delete it !... quite confusing... what did I miss ? – Mister B. Jun 4 '15 at 3:10
  • I realized that my error comes from the GetFileByServerRelativeUrl()... so my new problem is "How do I have to split my document url to obtain the correct serverRelativeUrl ? In the above example I thaught that 'strCollection + strLink' could correspond to the serverRelativeUrl... but it seems that it is not the case... – Mister B. Jun 4 '15 at 6:50
1

I found the solution !
The real problem was the GetFileByServerRelativeUrl that didn't work here. Everything was ok with SP 2010, but impossible to make it work here with SP 2013. So as a workaround, I used a CAML Query. So here is the code that runs ok for me now :

    public void deleteDocument(string strLibrary, string strDocname)
    {
        ClientContext ctx = new ClientContext(strServerUrl);
        Web currentWeb = ctx.Web;

        ctx.Load(currentWeb);
        ctx.Credentials = new System.Net.NetworkCredential(myLogin, myPwd, myEnvironnement);
        ctx.ExecuteQuery();

        try
        {
            List lstLib = ctx.Web.Lists.GetByTitle(strLibrary);
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View>"
                         + "<Query>"
                         + "<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>" + strDocname + "</Value></Eq></Where>"
                         + "</Query>"
                         + "</View>";

            Microsoft.SharePoint.Client.ListItemCollection listItemCollection = lstLib.GetItems(query);
            ctx.Load(listItemCollection);
            ctx.ExecuteQuery();

            // Document Id 
            int intDocId = listItemCollection[0].Id;

            Microsoft.SharePoint.Client.ListItem liDoc = listItemCollection.GetById(intDocId);
            ctx.Load(liDoc);
            ctx.ExecuteQuery();
            liDoc.DeleteObject();
            ctx.ExecuteQuery();

        }
        catch (Exception expt)
        {

        }
    }

If the file is located at the following address :
"http//myServer/sites/documentCollection/Library/document.pdf"

  • strServerUrl="http//myServer"
  • strCollection="/sites/documentCollection"
  • strLibrary="Library"
  • strDocname="document.pdf"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.