2

I have a pages library in sharepoint online with 2 views.

Custom view - (I have added a condition in the view based on a content type) - contains 500 items, It has got 100 items where title starts with "abc"

Default view - It has 4 items and all items have title starting with "abc" (Don't know why it is showing only 4 items, there is no filter condition and I am expecting all 504 items to appear in the view) - but shows only 4 items

I want to write a CAML query in CSOM on custom view where title begins with "abc"

<View>
    <ViewFields>
        <FieldRef Name="Title" />
    </ViewFields>
    <Query>
        <Where>
            <BeginsWith>
                <FieldRef Name="Title" />
                <Value Type="Text">abc</Value>
            </BeginsWith>
        </Where>
    </Query>
</View>

Query executes fine but shows only 4 items, looks like it is showing items from default view but I want to show items from custom view.

I can load my custom view and use view query

Microsoft.SharePoint.Client.View view = oList.Views.GetByTitle("Custom View");
                rentalSiteClientContext.Load(view);
                rentalSiteClientContext.ExecuteQuery();
                query.ViewXml = view.ViewQuery
                ListItemCollection collListItems = oList.GetItems(query);
                rentalSiteClientContext.Load(collListItems);
                rentalSiteClientContext.ExecuteQuery();

this is still returning 4 items.

1
  • any help please.. Commented May 30, 2017 at 0:50

1 Answer 1

1

Try this, as you have not posted the complete code so I tried to write a console app.

using Microsoft.SharePoint.Client;    
namespace ConsoleApplication    
{
    class Program    
    {    
        static void main(string[] args)    
        {    
            using (ClientContext ctx=new ClientContext("http://siteurl"))    
            {   
                ctx.Credentials = new System.Net.NetworkCredential("UserName", "Password");    
                Web web = ctx.Web;    
                ctx.Load(web, w=>w.Lists);
                List list = web.Lists.GetByTitle("List");//Load list    
                ctx.Load(list, l => l.Views);    
                ctx.ExecuteQuery();    
                CamlQuery query = new CamlQuery();    
                ListItemCollection itemColl = null;    
                View view = list.Views.GetByTitle("Your view");//enter view name
                ctx.Load(view, vw => vw.ViewQuery);    
                ctx.ExecuteQuery();    
                query.ViewXml = string.Format("<View><Query>{0}</Query></View>", view.ViewQuery); <--- Place actual query here
                itemColl = list.GetItems(query);    
                ctx.Load(itemColl);    
                ctx.ExecuteQuery();    
            }    
        }    
    }    
}
5
  • are you saying to replace string.Format("<View><Query>{0}</Query></View>", view.ViewQuery); with my query? Commented May 29, 2017 at 4:47
  • I am using this query now string.Format("<View><Query>{0}</Query></View>", "<Where />"); but still getting 4 results, it should return all items in that view but it is returning items from default view. My question is where that view is being used in query?? Commented May 29, 2017 at 4:53
  • Can you try using your actual query in place of view.ViewQuery? Commented May 29, 2017 at 6:01
  • yes just for testing I am trying to get all items and I used this query string.Format("<View><Query>{0}</Query></View>", "<Where />"); this should return me all items but it also returns 4 items. Commented May 29, 2017 at 6:16
  • I tried this code its working for me. Try the code on some other list. Commented May 29, 2017 at 7:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.