CAML Query Where Items are Approved is Ignored RRS feed

  • Question

  • I am working with some custom web parts recently. Those web parts will only display items which have been already approved. To get the items, I use CAML Query and filter the modstat value like:

        <View ... >
                        <FieldRef Name='_ModerationStatus' />
                        <Value Type='ModStat'>0</Value>
    I passed the query to a XsltListViewWebPart programmatically. I also wrapped the code to populate the XsltListViewWebPart in SPSecurity.RunWithElevatedPrivileges (I have also tried remove the elevated privileges, but still no luck).

    The strange thing is when I logged in as any user, the query worked fine and returned correct results (only approved items are displayed on the site). But when I access the site as an anonymous user, I could also see pending items (It feels like the where clause didn't work). Here is the web part on my .ascx page.


    Any workaround on this case?
    Wednesday, December 28, 2016 4:59 AM

All replies

  • If the objective is only displaying approved items why do you not create a view filter on the approved column?

    Also, how did you apply the caml query to XSLTViewWebPart?

    Regards, Huzefa Mala, MCPD, MCT Please mark the post that answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.

    Wednesday, December 28, 2016 5:25 AM
  • Because I applied some custom filter on the list view web part based on other controls on the page, so I need to build the query dynamically. To apply the caml query to XsltListViewWebPart, I did the following:

        articleWebPart.XmlDefinition = "Your Caml Query Here";

    Wednesday, December 28, 2016 6:01 AM
  • Hi Andy,

    Please check if you have assigned CAML Query valid in the code snippet, here is a demo about setting CAML Query in custom web part for your reference:

    Programmatically Adding XsltListViewWebPart Inside Panel in SharePoint


    Best Regards

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact

    Tuesday, January 3, 2017 9:04 AM