0

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).

2
  • Make sure that all files are checked in and published
    – akbar ali
    Dec 28, 2016 at 7:08
  • Yes, they are all checked and published Dec 28, 2016 at 7:22

1 Answer 1

1

Try with adding below code on ascx page.

<WebPartPages:XsltListViewWebPart 
    ID="articleWebPart" runat="server"
    IsIncluded="true" PageType="PAGE_NORMALVIEW"
    ShowToolbarWithRibbon="false">
    <XmlDefinition>
        <View Name="Some View" 
            Type="HTML" 
            Hidden="TRUE" 
            TabularView="FALSE" 
            DisplayName="" 
            Url="View.aspx" 
            Level="255" 
            BaseViewID="1" 
            ContentTypeID="0x" 
            ImageUrl="/_layouts/images/generic.png">
            <Query>
                <Where>
                    <Eq>
                        <FieldRef Name='_ModerationStatus' />
                        <Value Type='ModStat'>0</Value>
                    </Eq>
                </Where>
            </Query>
            <ViewFields>
                <FieldRef Name="ID"/>
            </ViewFields>
            <RowLimit Paged="TRUE">30</RowLimit>
            <Aggregations Value="Off"/>
            <Toolbar Type="Standard"/>
        </View>
    </XmlDefinition>
    <DataFields></DataFields>
</WebPartPages:XsltListViewWebPart>

Update view properties as per your requirement.

9
  • Just tried your code. It is still not working.. Dec 28, 2016 at 11:24
  • Can you please test with simple query like Title eq 'test'? Dec 28, 2016 at 11:26
  • I have tried that with Title eq 'Test' and it returned correct results. It seems like it just ignored conditions for _ModerationStatus field or something like that.. Dec 28, 2016 at 11:30
  • Oh and one more thing, if I tried to do caml query on custom web part, it worked well. It seems that the problem only exists on any kind of list view (WebPartPages:XsltListViewWebPart, SharePoint:ListView, or even a list view web part that I added from the Edit Page menu). Dec 28, 2016 at 11:46
  • Only one alternate solution is there. Add Choice filter webpart above list view webpart. Set default value as 0. Minimize choice filter webpart and save the page. Dec 28, 2016 at 12:12

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.