Check if PageExternalURL exists

Vote:
 

I'm creating pages in code and it looks like there is no built in function to check if a PageExternalURL already is used on another page. In edit mode there is a check for this but I'm now trying to find out what the best way would be to test this before saving the page.

I tried with the code below but I don't really know if this would be a satisfying way to go about. I don't seem to get any results in the PageDataCollection I get in return.

static public PageDataCollection FindPagesOfPageType(EPiServer.Core.PageReference pageLink, string pageExternalUrl)
{
    PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();
    PropertyCriteria criteria = new PropertyCriteria();
    criteria.Condition = EPiServer.Filters.CompareCondition.Equal;
    criteria.Name = "PageExternalURL";
    criteria.Type = EPiServer.Core.PropertyDataType.String;
    criteria.Value = pageExternalUrl;
    criteria.Required = true;
    criterias.Add(criteria);

    return DataFactory.Instance.FindPagesWithCriteria(pageLink, criterias);
}

#42774
Sep 01, 2010 8:19
Vote:
 

You should be able to use the GlobalPageValidation.ValidateExternalURL event handler to do this (just create a new PageValidateEventArgs with your new page inside, call the handler with it and then check the EventArgs' IsValid property).

Could be it doesn't work for an unsaved page, I haven't tried. Post a reply if you get it working :)

#42775
Sep 01, 2010 8:31
Vote:
 

All the ValidateExternalURL seems to do is making upper case characters to lower case. I can still save the page with an allready "occupied" PageExternalURL. As a little side note it also allows characters like €%&, while the function used in edit mode only allows a-z, 0-9, -, _ and ~.

#42776
Sep 01, 2010 8:50
Vote:
 

I did a quick test and it seems to do exacly what is expected. This is the test code I used:

var page = DataFactory.Instance.GetDefaultPageData(PageReference.StartPage, 3);
page.PageName = "Test";
page["PageExternalURL"] = "test"; // An already used simple address
var args = new PageValidateEventArgs(page);
GlobalPageValidation.ValidateExternalURL(args);
var isValid = args.IsValid; // false

I also tried an unused but illegal address ("#foo") and that also yields IsValid = false. The EventArgs also carries an errormessage describing the error correctly.

#42777
Sep 01, 2010 9:07
Vote:
 

You are correct! I used it the wrong way. Thanks a million!

#42778
Sep 01, 2010 9:44
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.