0

How do I retrieve view data on client-side at https://www.x.com/x1/x2/x3/allitems.aspx that I have access to? The code I've got so far:

ClientContext context = new ClientContext("https://www.x.com");

// The SharePoint web at the URL.
Web web = context.Web;

// Retrieve all lists from the server. 
context.Load(web.Lists);

I get access denied error with above code. How do I force the code to look in the URL to the view (https://www.x.com/x1/x2/x3/allitems.aspx), instead of the URL to the site (https://www.x.com)?

1

I can think of three ways in which this code has gone wrong.

  1. The long URL that you are mentioning is actually a view inside a site collection which is not the root site collection. Meaning that you are trying to access a the root site collection instead of the actual site collection. The easiest way of knowing this would be to check if there is a "sites" keyword in the URL. Read more about managed paths here.
  2. Once you have identified that the site collection URL that you are passing over to the ClientContext is correct, you should try and limit your query only to the list of your concern, instead of accessing all lists.

    ClientContext context = new ClientContext("https://www.x.com/sites/<site_collection_name>");
    
    // The SharePoint web at the URL.
    List listObj = context.Web.Lists.GetByTitle("<list_name>");
    
    // Retrieve all lists from the server. 
    context.Load(listObj);
    context.ExecuteQuery();
    
  3. Based on the URL you have posed, it seems like your list is under a subsite, which would have a URL like: https://< web_application_url >/< subsite_name >/Lists/ < list_name >/AllItems.aspx. If this is the case, you need to open the appropriate web under the context first, before trying to load your list.

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.