Change the link in a FeedItemBuilder

Topics: Customizing Orchard
Feb 14, 2013 at 10:45 PM
I am attempting to use a class the implements IFeedItemBuilder to replace the default link. IN the debugger I can see the XML node is what is expected, seems another builder is appending the specific ContentId url. Is there anyway to protect this value so that id does not get updated further.

Here is the code in the IFeedItemBuilder that is writing out the link.
if (string.IsNullOrEmpty(detailsUrl))
{
    feedItem.Element.SetElementValue("link", this.FullyQualifiedApplicationPath);
}
else
{
    feedItem.Element.SetElementValue("link", detailsUrl);
}
Coordinator
Feb 14, 2013 at 11:55 PM
It may just be a matter of taking the dependency to the other component doing this, so that yours would come second.
Feb 15, 2013 at 1:11 AM
Not really sure how to fix this at this point, if it is a dependency problem not sure how to add it to the class that is causing the issue.

I know that the following class is adding the url, thus making it an invalid URL:
CorePartsFeedItemBuilder (CorePartsFeedItemBuilder.cs) - Code that created the issue below.
context.Response.Contextualize(requestContext => {
                                                        var urlHelper = new UrlHelper(requestContext, _routes);
                                                        var uriBuilder = new UriBuilder(urlHelper.RequestContext.HttpContext.Request.ToRootUrlString()) { Path = urlHelper.RouteUrl(inspector.Link) };
                                                        link.Add(uriBuilder.Uri.OriginalString);
                                                        guid.Add(uriBuilder.Uri.OriginalString);
                                                   });
Feb 15, 2013 at 1:15 AM
Just in case it helps here is the full code of the IFeedItemBuilder that is attempting to set the link.
public class FeedBuilder : IFeedItemBuilder
{
    public void Populate(Orchard.Core.Feeds.Models.FeedContext context) {
        var detailsUrl = string.Empty;
        var imageUrl = string.Empty;

        foreach (var feedItem in context.Response.Items.OfType<FeedItem<ContentItem>>()) {
            if (context.Format == "rss")
            { 
                context.Response.Contextualize(c => {
                    var contentItem = feedItem.Item;
                    foreach (var part in contentItem.Parts) {
                        if (part.PartDefinition.Name.Equals("AdvertisementPart", StringComparison.InvariantCultureIgnoreCase)) {
                            detailsUrl = (part as AdvertisementPart).DetailUrl;
                        }

                        foreach (var field in part.Fields) {
                            if (field.Name.Equals("Image", StringComparison.InvariantCultureIgnoreCase)) {
                                imageUrl = (field as MediaPickerField).Url.Replace("~/", this.FullyQualifiedApplicationPath);
                            }
                        }
                    }
                });

                feedItem.Element.SetElementValue("link", string.IsNullOrEmpty(detailsUrl) ? this.FullyQualifiedApplicationPath : detailsUrl);
                feedItem.Element.Descendants("description").Remove();
                feedItem.Element.Add(this.AdvertisementFeedEntry(imageUrl, detailsUrl));
            }
        }
    }
Coordinator
Feb 15, 2013 at 7:31 AM
Try to set the dependency to the core module where CorePartsFeedItemBuilder is, in module.txt.
Feb 15, 2013 at 2:38 PM
I tried to use a dependency that did not solve the issue. However I noticed if I removed and added the xml nodes to feedItem.Element it seemed to work, I really not sure why this made a difference.

Here is the final code that is working as expected, and thanks for the quick replies:
public void Populate(Orchard.Core.Feeds.Models.FeedContext context)
        {
            context.Response.Contextualize(c =>
            {
                foreach (var feedItem in context.Response.Items.OfType<FeedItem<ContentItem>>())
                {
                    if (context.Format == "rss")
                    {
                        var detailsUrl = string.Empty;
                        var imageUrl = string.Empty;
                        var link = new XElement("link");
                        var contentItem = feedItem.Item;

                        foreach (var part in contentItem.Parts)
                        {
                            if (part.PartDefinition.Name.Equals("AdvertisementPart", StringComparison.InvariantCultureIgnoreCase))
                            {
                                detailsUrl = (part as AdvertisementPart).DetailUrl;
                            }

                            foreach (var field in part.Fields)
                            {
                                if (field.Name.Equals("Image", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    imageUrl = (field as MediaPickerField).Url.Replace("~/", this.FullyQualifiedApplicationPath);
                                }
                            }
                        }

                        if (feedItem.Element == null)
                        {
                            return;
                        }

                        link.Add(string.IsNullOrEmpty(detailsUrl) ? this.FullyQualifiedApplicationPath : detailsUrl);
                        var temp = feedItem.Element.Element("link");
                        if (temp != null)
                        {
                            temp.Remove();
                        }
                        feedItem.Element.Add(link);

                        var description = feedItem.Element.Element("description");
                        if (description != null)
                        {
                            description.Remove();
                        }
                        feedItem.Element.Add(this.AdvertisementFeedEntry(imageUrl, detailsUrl));

                    }
                }
            });
        }
Coordinator
Feb 16, 2013 at 2:47 AM
Glad to see you found a solution.