2

I am using the client object model and I want to access a List using the server relative Url. Folders can be accessed using the web.GetFolderByServerRelativeUrl but no such function exists for lists.

The available API is web.Lists.GetByID and web.Lists.GetByTitle which I can't use (The list title is different from the listname/listpath)

5

Try this:

public List GetListByRelativeUrl(String relativeUrl)
   {
       List existingList;

       Web web = context.Web;
       ListCollection lists = web.Lists;

       IEnumerable<List> existingLists = context.LoadQuery(
               lists.Where( list => list.RootFolder.ServerRelativeUrl == relativeUrl)
               );
       context.ExecuteQuery();

       existingList = existingLists.FirstOrDefault();

       return existingList;
   }

Each list has a root folder - you can find it by using the ServerRelativeUrl property.

1

You can use by this way:

       try
       {
            SP.ClientContext ctx = new SP.ClientContext("yoursiteurl");
            ctx.Credentials = CredentialCache.DefaultCredentials;
            ctx.AuthenticationMode = SP.ClientAuthenticationMode.Default;
            ctx.ExecuteQuery();

            IEnumerable<SP.List> newLists;
            var query = from list in ctx.Web.Lists
                        where list.DefaultViewUrl == "/Shared Documents/Forms/AllItems.aspx"
                        select list;

            newLists = ctx.LoadQuery(query);
            ctx.ExecuteQuery();
            foreach (SP.List l in newLists)
            {
                MessageBox.Show(l.Title);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
1

i know its old, but both solutions are not working in all cases. So its my example that work all the times

        public List GetListByServerRelativeUrl(ClientContext context, string relativeUrl)
    {
        List existingList;
        Web oWebsite = context.Web;
        ListCollection collList = oWebsite.Lists;
        context.Load(
            collList,
            lists => lists.Include(
                list => list.Title,
                list => list.Id,
                list => list.RootFolder.ServerRelativeUrl));
        context.ExecuteQuery();
        existingList = collList.First(list => list.RootFolder.ServerRelativeUrl == relativeUrl);
        return existingList;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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