+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Exposing OData

Exposing OData

11 posts, 0 answered
  1. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    10 Aug 2010
    Link to this post
    Hello Telerik Team,

    I am trying to expose OData service for Sitefinity News Items but seems that there's a problem.

    I have a website I have created using Sitefinity 3.7 SP3 project template for .NET Framework 3.5.
    In order to use OData classes I changed the target framework to .NET 4.0 and site seems ok.
    Then I have one custom data source to get all news items :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Telerik.Cms.Engine;
    using System.Collections;
    using Telerik.News;
     
    /// <summary>
    /// Summary description for Class1
    /// </summary>
    public class Class1
    {
        #region Private Fields
     
        private List<News> m_News = new List<News>();
     
        #endregion
     
        #region Properties
     
        public IQueryable<News> News
        {
            get
            {
                if (m_News == null || m_News.Count == 0)
                {
                    LoadNews();
                }
     
                return m_News.AsQueryable();
            }
        }
     
        #endregion
     
        #region Constructors
     
        public Class1()
        {
            LoadNews();
        }
     
        #endregion
     
        #region Methods
     
        private void LoadNews()
        {
            ContentManager manager = new ContentManager("News");
            m_News = new List<News>();
            IList listOfItems = manager.GetContent();
     
            foreach (IContent content in listOfItems)
            {
                News news = new News
                {
                    ID = content.ID.ToString(),
                    Title = content.GetMetaData("Title").ToString(),
                    FriendlyUrl = content.GetMetaData("FriendlyUrl").ToString(),
                    Summary = content.GetMetaData("Summary").ToString(),
                    Author = content.GetMetaData("Author").ToString(),
                    Category = content.GetMetaData("Category").ToString(),
                    Source = content.GetMetaData("Source").ToString(),
                    Expiration_Date = DateTime.Parse(content.GetMetaData("Expiration_Date").ToString()),
                    Publication_Date = DateTime.Parse(content.GetMetaData("Publication_Date").ToString())
                };
     
                m_News.Add(news);
            }
        }
     
        #endregion
     
    }
     
    public class News
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public string Summary { get; set; }
        public string Author { get; set; }
        public string Source { get; set; }
        public DateTime Publication_Date { get; set; }
        public DateTime Expiration_Date { get; set; }
        public string Category { get; set; }
        public string FriendlyUrl { get; set; }
    }


    and a simple WCF Data Service that is suppose to expose the atom data.

    using System.Data.Services;
    using System.Data.Services.Common;
    using System.ServiceModel.Activation;
    using System.ServiceModel;
     
    [ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class OData : DataService<Class1>
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
    }

    I tried several combinations with these attributes (I read about them in forum so I wasn't sure if both are needed.)
    It looks like working but the manager is always returning 0 items. The manager is not null. 

    If I try using the data source class in a regular user control everything's fine, so may be something is happening earlier - I am not very familiar with WCF data services. 

    Please advice.

    Thank you very much,

    Nadya
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Aug 2010
    Link to this post
    Hi Nadya,

    You could try the following sample code that works fine at our end.

    Test.svc

    <%@ ServiceHost Language="C#" Debug="true" Service="Test" CodeBehind="~/App_Code/Test.cs" %>

    App_Code/Test.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.Collections;
    using Telerik.Cms.Engine;
     
    // NOTE: If you change the class name "Test" here, you must also update the reference to "Test" in Web.config.
    public class Test : ITest
    {
        public void DoWork()
        {
        }
     
        #region ITest Members
     
        public IList HelloWorld()
        {
           // return "hello there";
            var cntManager = new ContentManager("News");
            return cntManager.GetContent();
        }
     
        #endregion
    }

    ITest

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.Collections;
     
    // NOTE: If you change the interface name "ITest" here, you must also update the reference to "ITest" in Web.config.
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        IList HelloWorld();
    }

    Then call the service from a control or a page

    Test t = new Test();
    var list = t.HelloWorld();

    cntManager.GetContent(); should return you all news items.

    You could try to move LoadNews(); outside of the constructor and call it as a separate method.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    10 Aug 2010
    Link to this post
    Hi Ivan,

    Thank you very much for your quick response. The thing is that I need to expose OData service and as far as I am getting familiar with OData and libraries for it that are part of .NET 4.0 I am not suppose to call the service myself.
    By creating the WCF Data Service and giving it my DataSource as type

     

    public class OData : DataService<Class1>

    it should load data by itself. I see that your way is loading items but Odata is not working. I am thinking of creating xml files for OData as static files and then show them - may be it will be safer. I just cannot explain why items are 0..

    thanks again.

  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    11 Aug 2010
    Link to this post
    Hi Nadya,

    Can you debug the execution of the oData service and see what is happening with your request. It may be that you have some errors in your code. Other than that you should make sure that you are using the oData libraries for .NET 3.5 SP1. As Sitefinity currently does not support .NET 4.0 and if you are going to host your oData services on a Sitefintiy website.

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    12 Aug 2010
    Link to this post
    Hi all,
    I started over and now I am using Odata libraries for .NET 3.5 but the content items are still not coming up - I am really confused.

    I have debugged and as I said my news manager is not null, it is initialized, but GetContent is returning 0 Items - strange!

    using System.Data.Services;
    using System.Data.Services.Common;
    using System.ServiceModel.Activation;
      
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Test : DataService<Class1>
    {
        public static void InitializeService(DataServiceConfiguration config)
        {
             config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
             config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
             config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
              
        }
    }

    This is how my service looks now - it is not much of a difference using 3.5 and 4..0 in this case.

    The DataSource is just Loading News Items using Sitefinity API:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Telerik.Cms.Engine;
    using System.Collections;
    using Telerik.News;
      
    public class Class1
    {
        #region Properties
      
        public IQueryable<News> News
        {
            get
            {
                NewsManager manager = new NewsManager(NewsManager.DefaultContentProvider);
                List<News> m_News = new List<News>();
                IList listOfItems = manager.Content.GetContent();
      
      
                foreach (IContent content in listOfItems)
                {
                    News news = new News
                    {
                        ID = content.ID.ToString(),
                        Title = content.GetMetaData("Title").ToString(),
                        FriendlyUrl = content.GetMetaData("FriendlyUrl").ToString(),
                        Summary = content.GetMetaData("Summary").ToString(),
                        Author = content.GetMetaData("Author").ToString(),
                        Category = content.GetMetaData("Category").ToString(),
                        Source = content.GetMetaData("Source").ToString(),
                        Expiration_Date = DateTime.Parse(content.GetMetaData("Expiration_Date").ToString()),
                        Publication_Date = DateTime.Parse(content.GetMetaData("Publication_Date").ToString())
                    };
      
                    m_News.Add(news);
                }
      
                return m_News.AsQueryable<News>();
            }
        }
      
        #endregion
      
    }
      
    public class News
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public string Summary { get; set; }
        public string Author { get; set; }
        public string Source { get; set; }
        public DateTime Publication_Date { get; set; }
        public DateTime Expiration_Date { get; set; }
        public string Category { get; set; }
        public string FriendlyUrl { get; set; }
        //Thumbnail { get; set; }
    }

    And if i change getting the news with manually entering data the service is working - why NewsManagers is not returning any data ?

    Please help!

    Cheers, Nadya
  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    12 Aug 2010
    Link to this post
    Hi Nadya,

    Where is the webservice hosted? It should be hosted on the Sitefinity website you are getting the data from. For example if we want to pull the data from www.sitefinity.com the webservice should be hosted on this site and should be called in the context of this website in order to be able to use the API with this website's data.

    Best wishes,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    12 Aug 2010
    Link to this post
    Hi Radoslav,

    yes I am having a fully working sitefinity 3.7 SP3 website.
    I am adding as a new class the Class1 in App_Code. Then I am adding to the website new WCF Data Service which is the Test.svc.

    this svc file looks like:

    <%@ ServiceHost Language="C#" CodeBehind="~/App_Code/Test.cs" Factory="System.Data.Services.DataServiceHostFactory" Service="Test" %>

    The code behind is automatically created in App_Code.

    If I use the Class1 property in default.aspx it is returning data.

    Am I missing something?
  8. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    13 Aug 2010
    Link to this post
    Hi all,

    can anybody help - I need to know if this is possible the way I am doing it.

    Thanks.
  9. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    17 Aug 2010
    Link to this post
    Hi Nadya,

    I have created a website and then I have added a new DataService to it. I have then used your code for the data context class and created my data context. I had slightly changed the code for debugging purposes. After the web service has been created I have added a Service Reference to this service. After all of this was done the news manager returns the correct number of items from the news module. I have attached my sample project you can inspect it and debug the constructor of the datasource class to verify that the news manager returns the correct number of items. Not that you might need to fix the service references in ~/App_WebReferences directory as they might be pointing to an incorrect url.

    Greetings,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    18 Aug 2010
    Link to this post
    Hi Radoslav,

    So yes, your example is working but mine wasn't. After testing different situations I found out that it depends if your website is multilingual or not. If it is not it is ok.
    On the other hand when you are having a multilingual website at the moment when data service is called current thread's language is "en-US". And this is no matter what languages you are having for the website. I tested with french but at this point thread's language is United States English.

    So at my situation default language is "en" this is how it is set in the web.config. So my news are having this language (or some other, the point is that there are news with this language). And of course this language that is in the database for these news if different from "en-US" and news items are 0.

    One possible workaround is to include "en-US" in your site's languages so some items are returned.

    Unfortunately we cannot change the current culture to "en", "da", "fr" etc., because Thread's culture could not be neutral.

    So I guess if we want to use this service the way I need it I have all my languages in the web.config to be codes for specific cultures.

    If there is another workaround, please let me know.

    Thank you for your support.

    Have a great day,
    Nadya
  11. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    18 Aug 2010
    Link to this post
    Hello Nadya,

    I have these settings for localization in the sample site I have sent:
    <localization defaultProvider="Sitefinity" persistenceMode="PathPrefix" defaultCulture="en" cultures="en, es, fr, de, bg">

    <cmsEngine defaultProvider="Generic_Content">
      <providers>
        <clear />
        <add name="News" urlRewriteFormat="[Publication_Date]/[Title].aspx" urlDateTimeFormat="yy-MM-dd" urlWhitespaceChar="_" visible="False" defaultMetaField="Title" securityProviderName="" allowVersioning="True" applicationName="/News" versioningProviderName="" allowLocalization="true" commentsModeration="true" connectionStringName="GenericContentConnection" type="Telerik.News.Data.DefaultNewsProvider, Telerik.News.Data" />

    I have then modified my WCF service like this to get all news items for Spanish language for example:
    public Class1()
    {
        m_News = new List<Test.News>();
        NewsManager manager = new NewsManager("News");
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("es");
        IList listOfItems = manager.Content.GetContent();
         
        foreach (IContent content in listOfItems)
        {
            News news = new News
            {
                ID = content.ID.ToString(),
                Title = content.GetMetaData("Title").ToString(),
                Summary = content.GetMetaData("Summary").ToString(),
                Author = content.GetMetaData("Author").ToString(),
                Category = content.GetMetaData("Category").ToString(),
                Source = content.GetMetaData("Source").ToString(),
                Expiration_Date = DateTime.Parse(content.GetMetaData("Expiration_Date").ToString()),
                Publication_Date = DateTime.Parse(content.GetMetaData("Publication_Date").ToString())
            };
     
            m_News.Add(news);
        }
    }

    The CurrentCulture of CurrentThread cannot be nonspecific culture. However the content managers take into account the CurrentUICulture which can be nonspecific.

    Greetings,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
11 posts, 0 answered