14

I have a SP Online site where I store Documents, I have no issues adding/retrieving documents but in the delete flow I get an error during retrieval of a File object.

public static void DeleteDocument()
{
    using (ClientContext ctx = ClientContextFactory.Create("https://my-sponline-site.sharepoint.com/sites/documentsite"))
    {
        Web web = ctx.Web;
        ctx.Load(web);
        ctx.ExecuteQuery();

        string relativeUrl = "/Documents/images.jpg";

        File file = web.GetFileByServerRelativeUrl(relativeUrl);
        ctx.Load(file);
        file.DeleteObject();

        ctx.ExecuteQuery();
    }
}

Full Url of the file is "https://my-sponline-site.sharepoint.com/sites/documentsite/Documents/images.jpg" (No more accessible 2016-12-07)

When I execute this, I get a ServerException :

Value does not fall within the expected range.

The Context is working fine as I'm able to add/retrieve items from the library and the context user is administrator.

I tried adding the web url to the relativeUrl so it would be "/documentsite/Documents/images.jpg" but I get the same error.

I can't seem to figure this out, any suggestions?

Thanks

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
morteng
  • 1,133
  • 2
  • 9
  • 22
  • 2
    since its serverrelative - you should add /sites/documentsite – Marek Kembrowski Jan 21 '14 at 11:56
  • 2
    Seems worth noting that although the file-retrieval wants a server-relative URL, it appears that you still need to use a context created with the URL of the correct site (or else you get the same "Value does not fall within the expected range" error). – mwardm Mar 20 '14 at 11:12

1 Answers1

28
string relativeUrl = "/sites/documentsite/Documents/images.jpg";
Linga
  • 10,379
  • 10
  • 52
  • 104
Tony Wu
  • 1,040
  • 18
  • 28