Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint 2010 Client Object Model get a List item from a Url

Is there a way to get a List object by a Url?

I know you can get a list by a title:

ClientContext context = new ClientContext("http://foo");

List list = context.Web.Lists.GetByTitle("title");

context.Load(list);
context.ExecuteQuery();

But I want the user to be able to copy-paste a url from their browser in a textbox (e.g. http://foo/subsite/ListName/Forms/AllItems.aspx) and then extract the List object from that url.

like image 241
G.Smulders Avatar asked Nov 26 '10 11:11

G.Smulders


1 Answers

No, there is no method for obtaining a List object directly from a URL. As you've pointed out, you can get it from the List title, and you can also get it via its ID (see ListCollection.GetById).

My suggestion is to do some URL hacking; SharePoint URLs follow a predictable pattern. If a user is copy/pasting a List URL, they'll most certainly have the URL to a view, which will end in something like /Forms/[ViewName].aspx. Look for "/Forms/" in the path, grab the preceding substring, then use the new end of the path as the list name.

And it's easy enough to do some defensive coding by trying to load the list, catching the ArgumentException that is thrown if the List does not exist, and using that as an opportunity to present an error message to the user.

like image 92
CBono Avatar answered Oct 11 '22 17:10

CBono