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.

  • any help please.. – Ali May 30 '17 at 0:50
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();    
            }    
        }    
    }    
}
  • are you saying to replace string.Format("<View><Query>{0}</Query></View>", view.ViewQuery); with my query? – Ali May 29 '17 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?? – Ali May 29 '17 at 4:53
  • Can you try using your actual query in place of view.ViewQuery? – user13408 May 29 '17 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. – Ali May 29 '17 at 6:16
  • I tried this code its working for me. Try the code on some other list. – harshal gite May 29 '17 at 7:35

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.