Posted by (JavaScript must be enabled to view this email address) on Thu 29 Jan 2009

I’d recently written a SharePoint custom navigation object as the default functionality didn’t meet the customers needs. It tested out perfectly, only displaying pages which the user was able to see and which were also published.

When deployed on the live server though, no matter what I did, it simply wasn’t picking up the fact that some pages had been published, others had expired. It was displaying all content (apart from the hidden pages) in the publishing site.

What was going on

For too long I focused on the way the dates were being handled by my control…but surely the PortalSiteNavigation provider should take care of this?!?!?? I even went so far as to load the corresponding page item for each SiteMapNode in the SiteMapNodes collection then checking the PublishingStartDate and PublishingExpirationDate (Scheduled Start Date and Scheduled End Date) - Imagine the hit on performance when I did that!

After a lot of hair ripping, and swearing I found the issue.

Stsadm extensions had been installed on the server- I had used them a number of times to publish content en-masse for the customer - but it never twigged with me that this might be the root cause of all my issues.

A normally super helpful command gl-publishitems was the culprit. Gary Lapointes code has helped me out a number of times, this is by no means a slight against his code - just an unforeseen issue arising from it.

Each time the gl-publishitems code was run against the WebApplication it was republishing the “expired” content. SharePoint doesn’t actually use the dates at runtime to decide whether content should be displayed or hidden, it uses the dates as part of the Scheduled Unpublish and Scheduled Publish timer jobs to set the publishing status to published or draft.

The second issue was that not only was gl-publishitems republishing my content, but when the ScheduledUnpublish job would run, it would unpublish the content, however there were previous versions available in the system - and again SharePoint only rolls back the current version not all versions when the Scheduled Unpublish job runs.

So how did I resolve it?

By programmatically getting all versions of the “expired” document and setting them to Draft status using the File.Unpublish Command

I’ve included code samples in both C# and VB.Net here:

VB.Net

  1. if (Convert.ToDateTime(Actual) < DateTime.Now) then
  2. rWeb.AllowUnsafeUpdates = true
  3. lItem.ModerationInformation.Status = SPModerationStatusType.Draft ' set the item to draft
  4. lItem.File.UnPublish("System Unpublished") 'unpublish the file
  5.  
  6. 'for each previous version of the file
  7. Dim objVersionColl As SPListItemVersionCollection = lItem.Versions
  8. If objVersionColl.Count > 1 Then
  9. Previous = "resetting previous versions"
  10. 'Navigate to each version of the ListItem
  11. For Each objVersion As SPListItemVersion In objVersionColl
  12. Dim objLstItm As SPListItem = objVersion.ListItem
  13. objLstItm.ModerationInformation.Status = SPModerationStatusType.Draft ' set the item to draft
  14. objLstItm.File.UnPublish("System Unpublished") 'unpublish the file with comment
  15. objLstItm.SystemUpdate(false) 'updates the list item without incrementing the version
  16. Next 'Next objVersion
  17. End If 'End of objVersionColl.Count if
  18. lItem.SystemUpdate(false)
  19. end if

C#

  1. if ((Convert.ToDateTime(Actual) < DateTime.Now)) {
  2. rWeb.AllowUnsafeUpdates = true;
  3. lItem.ModerationInformation.Status = SPModerationStatusType.Draft;
  4. // set the item to draft
  5. lItem.File.UnPublish("System Unpublished");
  6. //unpublish the file
  7.  
  8. //for each previous version of the file
  9. SPListItemVersionCollection objVersionColl = lItem.Versions;
  10. if (objVersionColl.Count > 1) {
  11. Previous = "resetting previous versions";
  12. //Navigate to each version of the ListItem
  13. foreach (SPListItemVersion objVersion in objVersionColl) {
  14. SPListItem objLstItm = objVersion.ListItem;
  15. objLstItm.ModerationInformation.Status = SPModerationStatusType.Draft;
  16. // set the item to draft
  17. objLstItm.File.UnPublish("System Unpublished");
  18. //unpublish the file with comment
  19. objLstItm.SystemUpdate(false);
  20. //updates the list item without incrementing the version
  21. }
  22. //Next objVersion
  23. }
  24. //End of objVersionColl.Count if
  25. lItem.SystemUpdate(false);
  26. }

A word of warning

The code I posted here does not differentiate between major and minor versions. You can only UnPublish Major versions so you will need to handle that yourself.

I used SystemUpdate(false) instead of Update as it doesn’t increment the version count - I would recommend you use File.Update instead.

REMEMBER - ALWAYS TAKE A BACKUP!



Posted by (JavaScript must be enabled to view this email address) on Fri 19 Dec 2008

You’ve spent the past four days writing the best code in the world to interface with MOSS 2007 and its publishing features. You run it and Argghhhhh! Whats going on, all your publishing data is not there!

Chances are you have done something like this to get the properties:
VB


dim startDate as DateTime = Convert.ToDateTime(item.Properties(“PublishingStartDate”));

C#


DateTime startDate = Convert.ToDateTime(item.Properties[“PublishingStartDate”]);

 

Instead, try accessing the properties like this:
VB


dim startDate as DateTime = Convert.ToDateTime(item(“PublishingStartDate”));

 

C#


DateTime startDate = Convert.ToDateTime(item[“PublishingStartDate”]);

Now get back to that killer code and get it fixed!



Page 4 of 4 pages « First  <  2 3 4

About our Blog

Brantas Limited specialise in Dynamics CRM, SharePoint and System Integration using the Microsoft Platform. We are all experienced developers in various fields with our own specialities complementing those of our team.

We have been working with SharePoint since 2003, including Installation and Administration, Migration, Development and Support.

RSS Feed