Web.GetFileByServerRelativeUrl throws "Value does not fall within expected range"
Asked Answered
L

1

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

Lightsome answered 21/1, 2014 at 9:56 Comment(2)
since its serverrelative - you should add /sites/documentsiteFroze
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).Stowell
S
28
string relativeUrl = "/sites/documentsite/Documents/images.jpg";
Schooner answered 14/2, 2014 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.