1

Does the SharePoint Client Object Model have any method to access an object (List, Document, etc.) with just a single URL and valid credentials?

Essentially, I want to access an object (http://example.com/sites/Site1/Lib1/Sample.pdf) programmatically like this:

var credentials = new System.Net.NetworkCredential("user", "pass", "domain");
using (var sp = new ClientContext("http://example.com/sites/Site1/") { Credentials = credentials })
{
    var file = sp.Web.GetFileByServerRelativeUrl("/Lib1/Sample.pdf");
    sp.Load(file);
    sp.ExecuteQuery();
}

I want to do this, except without knowing ahead of time which part of the URL corresponds to the site and which part points me to the object. So, I'm wondering if I have to make my own logic for this or if the Client Object Model has something for me to accomplish this.

1 Answer 1

2

There is no "get by URL" method for lists in the client object model that I am aware of.

About the closest thing you have is a GetFolderByServerRelativeUrl on the Web object:

ClientContext.Web.GetFolderByServerRelativeUrl(url)

Depending on what you want to do this might work for you...unfortunately you can't get the parent list from the folder object like you can in the .NET api.

Source - Similar question

Since you can't get a list by URL so I would assume you can't get a document by url either, but you can try doing it this way tho,

var fileItem = Web.GetFileByServerRelativeUrl("server/relative/url");

context.Load(fileItem); //you will probably need to load the ListItemAllFields property here.
context.ExecuteQuery();

var listItem = fileItem.ListItemAllFields; 

Console.WriteLine(listItem["Title"]);

Source - getlistitemstrurl-equivalent-for-clientcontext

Hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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