November Happy Hour will be moved to Thursday December 5th.

How do I set the UrlSegment equal to the page ID without losing my sanity?

Vote:
 

I have a certain page type where I need the UrlSegment to be totally arbitrary -- it can't have any relation to any property on the page. So, I had a great idea that I would just set the URL segment equal to the Page ID, so the UrlSegment would be a nice, meaningless number.

Sadly, this turned out to be WAY harder than I thought.

I tried to hook the PagePublishing event, but in that event e.Page.PageLink has no ID (it's 0).

I tried to hook the PagePublished event, then set the UrlSegment and write the page back again with ForceCurrentVersion.  Seemed like it would work, but the UrlSegment just never got sent.  The Save method ran fine, but when I went to look at the page, the UrlSegment was derived from the title, like normal.

I've tried to hook into UrlSegment.CreatedUrlSegment too, but there's no valid PageLink in there either.

What event can I hook to set this?

#43012
Sep 08, 2010 22:15
Vote:
 

Disclaimer: This might or might not be a solution as it's just a theory for now. :)

We built a plugin to avoid the "PageName1" type segments when copying a page by hooking into the UrlSegment.CreatedUrlSegment and setting the segment to empty. This will lead EPiServer to recreate the URL segment the next time the page is published, but this time with a more appropriate name.

As it happens, while the page is published with an empty segment, it seem to fall back to... *drumroll* ...the page ID! The segment will however be repopulated every time you save the page again, so you will have to keep clearing the segment or possibly set it to the page ID once that has been established and is available...

Let me know how you get on!

Henrik

#43016
Edited, Sep 09, 2010 1:11
Vote:
 

We done exactly this: set the Url Segment using some custom code by hooking into the PagePublishing event. However I also think we've also discovered the issue you mentioned (Page ID== 0). Though our requirements meant that we could work around the problem in a way that may not be suitable for you.

Basically if the page is published first without saving, then the pageId will be zero, which makes sense as at this point the PageData has never been persisted in the database (and so doesn't have a unique Identifier). What we did was to explicitly disallow this scenario (as you'll see in the code below). As I said before , this may or may not work for you.

public static void Instance_PublishingPage(object sender, PageEventArgs e)
        {
            if (e.Page.Property["PageURLSegment"].Value != null && string.Compare(e.Page.Property["PageURLSegment"].Value.ToString(), e.Page.PageLink.ID.ToString()) == 0)
                return;

            PageVersionCollection versions = PageVersion.List(e.Page.PageLink);

            if (versions.Count == 0)
            {
                e.CancelAction = true;
                e.CancelReason = "This pagetype must be saved first before it is published.  This is for url rewrite purposes."; 
                return;
            }

            e.Page.Property["PageURLSegment"].Value = e.Page.PageLink.ID;
        }

    

 

#43040
Sep 09, 2010 15:48
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.