1

I hope that this is the correct place to ask this. I have a site that "spits out" an ATOM feed/seedlist that I want to show in SharePoint as an external content type. So far I know that this is done using the BCS and a WCF service. I'm a little bit stuck though as to how to create the WCF Service to allow the ATOM feed to show in SP, either online or SP2013.

So far I have found WCF Syndication, but I'm not entirely convinced that this is the correct way to go. Or is there maybe an easier way?

1 Answer 1

0

This is the solution I managed to come up with and it works!

This is just a class file added to the WCF Service library

public class Item
{
public string Link { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Author { get; set; }          
public DateTime Updated { get; set; }
public string FeedType { get; set; }

public string Tag { get; set; }
public Item()
{
    //Link = ""; 
    //Title = "";
    //Name = "";                            
    //Content = "";
    //Updated = DateTime.Today;
    //    FeedType = FeedType.RSS;
}
}

/// <summary>
/// A simple RSS, RDF and ATOM feed parser.
/// </summary>
public class FeedParser
{
/// <summary>
/// Parses the given <see cref="FeedType"/> and returns a <see cref="IList&amp;lt;Item&amp;gt;"/>.
/// </summary>
/// <returns></returns>
public IList<Item> Parse(XDocument doc, string feedType)
{
    switch (feedType)
    {
        case "RSS":
            return ParseRss(doc);
        case "RDF":
            return ParseRdf(doc);
        case "Atom":
            return ParseAtom(doc);
        default:
            throw new NotSupportedException(string.Format("{0} is not supported", feedType.ToString()));
    }
}

/// <summary>
/// Parses an Atom feed and returns a <see cref="IList&amp;lt;Item&amp;gt;"/>.
/// </summary>
public virtual IList<Item> ParseAtom(XDocument doc)
{
    try
    { 
        // Feed/Entry
        var entries = from item in doc.Root.Elements().Where(i => i.Name.LocalName == "entry")
                      select new Item
                      {
                          FeedType = "Atom",
                          Title = item.Elements().First(i => i.Name.LocalName == "title").Value,
                          Author = item.Elements().First(i => i.Name.LocalName == "author").Value,                     //must add child element "name"
                          Content = item.Elements().First(i => i.Name.LocalName == "summary").Value,
                          Tag = item.Elements().First(i => i.Name.LocalName == "field").Value,                       //must specify FIELD_TAG
                          Link = item.Elements().First(i => i.Name.LocalName == "link").Attribute("href").Value,
                          Updated = ParseDate(item.Elements().First(i => i.Name.LocalName == "updated").Value),

                      };
        return entries.ToList();
    }
    catch (Exception ex)
    {
        return new List<Item>();
    }
}

/// <summary>
/// Parses an RSS feed and returns a <see cref="IList&amp;lt;Item&amp;gt;"/>.
/// </summary>
public virtual IList<Item> ParseRss(XDocument doc)
{
    try
    { 
        // RSS/Channel/item
        var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
                      select new Item
                      {
                          FeedType = "RSS",
                          Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
                          Author = item.Elements().First(i => i.Name.LocalName == "name").Value,                 //Newly added
                          Link = item.Elements().First(i => i.Name.LocalName == "link href").Value,
                          Updated = DateTime.Parse(item.Elements().First(i => i.Name.LocalName == "updated").Value),
                          Title = item.Elements().First(i => i.Name.LocalName == "title").Value
                      };
        return entries.ToList();
    }
    catch
    {
        return new List<Item>();
    }
}

/// <summary>
/// Parses an RDF feed and returns a <see cref="IList&amp;lt;Item&amp;gt;"/>.
/// </summary>
public virtual IList<Item> ParseRdf(XDocument doc)
{
    try
    { 
        // <item> is under the root
        var entries = from item in doc.Root.Descendants().Where(i => i.Name.LocalName == "item")
                      select new Item
                      {
                          FeedType = "RDF",
                          Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
                          Author = item.Elements().First(i => i.Name.LocalName == "name").Value,                 
                          Link = item.Elements().First(i => i.Name.LocalName == "link href").Value,
                          Updated = DateTime.Parse(item.Elements().First(i => i.Name.LocalName == "updated").Value),
                          Title = item.Elements().First(i => i.Name.LocalName == "title").Value
                      };
        return entries.ToList();
    }
    catch
    {
        return new List<Item>();
    }
}

private DateTime ParseDate(string date)
{
    DateTime result;
    if (DateTime.TryParse(date, out result))
        return result;
    else
        return DateTime.MinValue;
}
}

And here is the existing Service1.cs that I modified

public class Service1 : IService1
 {
public IList<Item> GetData()
{


    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("URL of the seedlist");

    request.Method = "GET";

    request.Accept = "application/atom+xml";

    request.ContentType = "application/atom+xml;type=entry";

    request.Credentials = new NetworkCredential("username", "password");

    ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    XDocument xDoc = XDocument.Load(new StreamReader(response.GetResponseStream()));



    FeedParser parser = new FeedParser();

    var items = parser.Parse(xDoc, "Atom");


    return items;

}
//for testing purpose only, accept any dodgy certificate... 
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
}

public Item GetItemByLink(string url)
{
    var items = GetData();
    foreach (var item in items)
    {
        if(item.Link == url)
        {
            return item;
        }

    }
    return null;

}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.