Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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 :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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