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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With