royjs

Hi,

I have a regular list with version control enabled and on wich anonymous users have right to view published items.

On this list I have some code that runs on the ItemUpdating event, in this code I would like to know if the anonymous users can see this item.

My first idea was to check the HasPublishedVersion of the listitem but I found out that it returns true even if the published version is not the active one. And of course, the anonymous users can only see the active version.

I can see if a version is published but I don't know how to find the active version.

I also tried to use the DoesUserHavePermission method but I can't figure out how to use it for anonymous users. It takes a SPUser as an argument... Is there a SPUser that would act as an anonymous user

Any solution would be appreciated.

Thanks in advance,



Re: SharePoint - Development and Programming Find out if a list item is viewable by anonymous users

DKeeling

You can get the specific version of a file using the Version Collection.

Code Snippet

SPWeb temp = new SPWeb();

SPList myList = temp.Lists["MyList"];

SPListItem myItem = myList.Items[0]; //Or get the actual item

SPListItemVersion ver = myItem.Versions[0]; //The versions are stored most recent first so 0 is the most recent version.

if (ver.ListItem.ModerationInformation.Status == SPModerationStatusType.Approved)

//Yep found it was published.






Re: SharePoint - Development and Programming Find out if a list item is viewable by anonymous users

royjs

Thanks for the answer,

But I need to find out if an item is approved AND is the current version. Sometimes the item is published even if the latest version hasn't been approved.

For example, if you publish a listitem then modify it, the latest version is a draft but the previous approved version is still the current version so the item is viewable by anonymous users.

But the scenario that is giving me a real hard time is this one :

1. The item is approved (this version becomes the current one)

2. The item is modified (previous version is still the current one)

3. The item is approved again (this new version is the current one)

3. The item is set to pending (no current version)

In the end, if you check in the item version history, the item will have 2 versions, one version is approved and the other one is pending but none of them are viewable by an anonymous users cause there is no current version.

So again what I really need to find out is if a listitemversion is the current one or any other way to find out if listitem is viewable by an anonymous user.

Thank you,





Re: SharePoint - Development and Programming Find out if a list item is viewable by anonymous users

DKeeling

Is this just to determine if the page item is visible at all or do you want to specify a previous published version to show

When something is pending, the previous published version of the item should be availible to the readers of the site.

You can loop through the versions

foreach (SPListItemVersion ver in listItem.Versions)

{

//The first time you hit an item with a published set to true, you have found the most recent version that was published.

if (ver.ListItem.ModerationInformation.Status == SPModerationStatusType.Approved)

//This is the one you want.

}

I think I am missing what you are trying to accomplish overall here.






Re: SharePoint - Development and Programming Find out if a list item is viewable by anonymous users

royjs

I want to know if there is a published version available to the readera.

In your last post you said "When something is pending, the previous published version of the item should be availible to the readers of the site.".

But my problem is that sometimes.. it isn't.

If you reproduce the scenario in my last post and check the item versions on the list item, you will see that there is no current version, the previous pubblished version is not available to the readers. And if you log in as a user that doesnt have modify or approve rights you won't see the item. That's this specific scenario that is giving me a hard time, there is a published version but it isn't available to the readers.





Re: SharePoint - Development and Programming Find out if a list item is viewable by anonymous users

DKeeling

I will try and get to testing this out this week, and let you know what I can find out.




Re: SharePoint - Development and Programming Find out if a list item is viewable by anonymous users

royjs

Thank you very much, your help is highly appreciated.




Re: SharePoint - Development and Programming Find out if a list item is viewable by anonymous users

kennardo

Any luck with this I think ver.ListItem always refers to the one list item, so even if you are looping through 5 versions with different publishing levels, ver.ListItem.ModerationInformation.Status will return the same status.


I have used

if (ver.Level == SPFileLevel.Published)

which works, but how do I then access the properties or fields of that version as though it were a ListItem I'd like to be able to go back to the latest published version (like royjs) and then access things like its Title, File, etc.

[Edit, got it]

versionItemTitle = ver["Title"];

Used in combination with the above, lets me go back and find the Title (and other properties) of the most recently published version of a particular list item.

[/Edit]