4

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.

3 Answers 3

5

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.

2
  • Thanks for the response. If no one comes up with something more elegant in the next couple days, I'll mark it as the answer. Commented Aug 1, 2011 at 19:08
  • For what it's worth I've looked into this fairly extensively too, using SP2013 Online. The dummy user approach appears to remain the only sensible one assuming you don't want to filter out versions where the latest version is unpublished (i.e. if the _level = 1 doesn't fit).
    – Timbo
    Commented Feb 10, 2015 at 9:14
6

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.

1
  • 2
    Thanks for the answer, Rob. Unfortunately, it doesn't fit my scenario. I want a query that will return data from the last major version of each item. For example, if Item A is currently at version 3.2, I want version 3.0 to be returned in the result. Commented Aug 1, 2011 at 19:05
1

Why not use search? A keyword query would fetch the relevant data (as long as security is set correctly on default content query account)

3
  • Will the search API only return the data for published items for members of the Owners or Approvers group? Commented Aug 1, 2011 at 23:21
  • Not sure what you mean with the Owners or Approvers group, but it will never return drafts (unless elevation messes up something) Commented Aug 2, 2011 at 12:03
  • +1 This does appear to be a viable option not listed in the accepted answer. It does involve a reasonable amount of search schema / managed property management, and would result in some content staleness while the search crawl takes place.
    – Timbo
    Commented Feb 10, 2015 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.