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 ... >
<Query>
...
<Where>
<Eq>
<FieldRef Name='_ModerationStatus' />
<Value Type='ModStat'>0</Value>
</Eq>
</Where>
...
</Query>
</View>
I passed the query to a XsltListViewWebPart programmatically. I also wrapped the code to populate the XsltListViewWebPart in SPSecurity.RunWithElevatedPrivileges (I have also tried to 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.
<WebPartPages:XsltListViewWebPart
ID="articleWebPart"
runat="server"
IsIncluded="true"
PageType="PAGE_NORMALVIEW"
ShowToolbarWithRibbon="false"
>
</WebPartPages:XsltListViewWebPart>
Any workaround for this case?
*Updated: It seems that the case only happens on all kind of list views. When I tried to view the query results in a custom web part (such as using a repeater to show items), it works fine. And when I tried to display the Approval Status on the list view and view them as the anonymous user, all of the approval statuses of the items are Approved (including pending ones).