Filter Custom Module Collection By URL
This has to be an easy one...what am I doing wrong?
I have a custom module, that I want to filter by the URL
example URL: www.somesite.com/.../FilterString
I have a usercontrol that loads on the page, I grab the filterstring from the URL:
var myPartner = string.Empty;
var param = Request.RequestContext.RouteData.Values["params"] as string[];
myPartner = (string)param[0];
Now I want to pull my "Partners" which is my custom module and filter by the filter string above.
This is my query:
var PartnersCollection = dynamicModuleManager.GetDataItems(partnerType).Where(p => p.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && p
.UrlName == myPartner && p.GetValue<
bool
>("Active") == true);
If I pull out "p.UrlName == myPartner" the query works fine and pulls all Partners. I just want to filter by the auto generated URL that was created when the Partner was created.
I tried these already:
p.UrlName == myPartner
p.SystemUrl == myPartner
p.Urls[0].Url == myPartner
The first one pulls 0 records and the last 2 cause errors. What am I doing wrong?
Thanks,
Coty
Hey Coty,
I always use this code to get the item from the url:
var urlParameterString = System.Web.UI.ControlExtensions.GetUrlParameterString(
this
,
true
).Substring(1);
if
(urlParameterString ==
"undefined"
)
return
;
// Get the Lawyer item
var dynamicModuleManager = DynamicModuleManager.GetManager();
var lawyerType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Lawyers.Lawyer"
);
var lawyer = dynamicModuleManager.GetDataItems(lawyerType).FirstOrDefault(x => x.Status == ContentLifecycleStatus.Live && x.UrlName == urlParameterString);
Kind regards,
Daniel
Thanks for the response.
I copied your code and I'm still returning Null. Its the UrlName Property that is making it not work. If I go to Content > Partners (that is my custom module) and look at the URL at the bottom it matches the variable I am passing in so I am not sure why it's not working.
Anyone know of any reason the UrlName query above wouldn't return results?
Hey there, I figured out my issue. Actually both our code snippets work above. I am going to add .FirrstOrDefault() to mine though since I am just pulling one item.
This was my final query:
DynamicContent partner = dynamicModuleManager.GetDataItems(partnerType).Where(p => p.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live &&
p.UrlName == myPartner && p.GetValue<
bool
>("Active") == true).FirstOrDefault();
My issue ended up being a carriage return was attached to my variable so it wasn't matching anything. Thanks for your help!
Coty