Querying for Approved (Published) List Item Data
-
16-10-2019 - |
Question
My scenario:
I have a page layout that has a user control on it. This user control queries data contained in other pages within the site collection to show a summary of related information. Anonymous uses do not have read access to the list containing the page data so I need to elevate privileges in order to read it. If I elevate by impersonating the site collection owner I'll get data from draft versions of items which I don't want. So, may question is, can I:
a) Impersonate the Contributor or Viewer privileges without having to create a fake user account and grant it permissions to the site, or
b) Can I write a CAML query that only returns data from the most recently approved (published) versions of each item, or
c) Is there some other strategy I could use that doesn't involve brute forcing my way through the versions of each item in the list using the API?
Edit:
After weighing my options I decided to go with the first option. Creating a fake user account seemed to be the least messy and most efficient solution.
Solution
a) Not without creating a dummy user, no.
b) Yup, not that complex either, just need to check the SPListItem.Level property of each version of a file, and look for the Published level. See here for more info: http://mysharepointwork.blogspot.com/2011/03/get-published-version-of-list-item.html Or here: http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/a8ce6143-43cb-4c25-9e9e-b135e8b5014f.
c) Don't think so. The Publishing infrastructure API is extremely flaky at best.
OTHER TIPS
I answered something similar to this the other day, as it relates to #2 anyway.
Creating Sharepoint Alerts only for published documents
...and don't be concerned that it doesn't have any upvotes or isn't marked as answered. I've used/tested this in the past, so I know it works. :)
Just add:
<FieldRef Name=\"_Level\" /><Value Type=\"Integer\">1</Value>
To your CAML query. Will only return published items...no need to iterate through the the result.
Why not use search? A keyword query would fetch the relevant data (as long as security is set correctly on default content query account)