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 23 Jan 2009

Cropping images sounds easy, you get an image, you tell it what size you want it, issue the bitmap.Crop() command and it crops.

No it doesn’t. It crops and gives you a little freebie…random lines on your images.

So how do you resolve it? well its as simple as 3 easy steps (wrapped in some GDI+ black magic).

  1. Create your final bitmap at the correct size.
  2. Create your source bitmap from the source image.
  3. Create a temporary rectangle with dimensions 2px bigger than the final one.

Step 1 is nice and simple - create your bitmap as you would want it to appear (say 150px x 150px). Nothing difficult there.
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);

Step 2 is also nice and easy
System.Drawing.Image imgPhoto = CreateImageFromUrl(sourcePath);

Step 3 Crop your image - heres the science bit…
grPhoto.DrawImage(imgPhoto,
new Rectangle(-2, -2, destWidth + 2, destHeight + 2),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
GraphicsUnit.Pixel);

Why does it work? imgPhoto is the image you are using. The first new rectangle is declared as being 1px larger than the imgPhoto in all dimensions and starts 1px to the left and 1px further up than your cropped image, and the second rectangle is the rectangle the same size as your source so that GDI knows how to scale the images.

The code doesn’t so much as remove them from your crop as leaves them in but makes them outside the bounds of your final image, by doing so, when you do save the image GDI+ ignores them and they aren’t displayed.

I’ll try and get a full working example on here for now but that should give you enough information to remove any weird lines you are seeing on your cropped images.



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

Chances are if you have developed a web part in the past, it needed some user input. And if it needed user input then it needs validation.

When you develop webparts you are unable to do use the designer (thanks Microsoft) so you have to be a real coder and declare, instantiate and add the controls entirely programmatically. Including your validation controls. Ok, you’ve done that. Why doesn’t your validation work?

First, you MUST (and I cant stress that enough) instantiate the validators in your overridden OnInit method, not in the CreateChildControls() method. Secondly, set up a validation group so that you can control what the validators are validating (you dont want to validate the whole page) and lastly, you need to ensure that the EnableClientScript property is enabled.

        regexpval.ControlToValidate = this.txtEmail.ID;
        regexpval.ValidationExpression = @”\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”;
        regexpval.ErrorMessage = “Please Enter a valid email address.”;
        regexpval.Display = ValidatorDisplay.Dynamic;
        regexpval.EnableClientScript = true;
        regexpval.ValidationGroup = validationGroup;

 

If you’ve been diligent then your code will compile, deploy and even let you place it on the page, but for some weird reason it still doesn’t work as you think it should. AAAAARGH!

Its a small and very simple fix, the text box you are validating might be instantiated but it doesn’t have an ID, D’oh!

All it takes is adding a line like this BEFORE you set up the validators:

 

txtEmail.ID = “txtEmail”;



Page 1 of 1 pages

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