Problem :
I need to have an absolute URL of a listitem’s display form – currently I do it the following way:
item.ParentList.ParentWeb.Url.TrimEnd('/') //"http://mydomain.com"
+ "/"
+ item.ParentList.DefaultDisplayFormUrl.Trim('/')//"Lists/WorkflowTasks/DispForm.aspx"
+ "?ID=" + item.ID //"?ID=1"
This gets me the correct URL, meaning http://mydomain.com/Lists/WorkflowTasks/DispForm.aspx?ID=1.
However this fails when my site collection is on a slightly more sophisticated host url, for examplehttp://mydomain.com/sites/secondsite/ – both methods from my code return the url containing the “/sites/secondsite/ part so I end up with http://mydomain.com/sites/secondsite/sites/secondsite/Lists/etc…
How do I code it in a more reliable way?
Solution :
There are a couple of methods for this available in the object model without the need to handle the slashes etc yourself, one method using MakeFullUrl:
var fullUrl = item.ParentList.ParentWeb.Site.MakeFullUrl(item.ParentList.DefaultDisplayFormUrl);
Also
+ "?ID=" + item.ID //"?ID=1"
Resolved.