More in this section

Forums / Developing with Sitefinity / How to get the right ContentItem

How to get the right ContentItem

12 posts, 0 answered
  1. DPC David
    DPC David avatar
    26 posts
    Registered:
    28 Dec 2009
    15 Feb 2010
    Link to this post
    Hi

    im having Problems getting all informations about an ContentItem. For example i have an .cs File which inherits from ContentView. With the following code i manage to get all MetaData of the current ContentItem

    protected override void SetItemMetadata(System.Web.UI.Control itemContainer, Telerik.Cms.Engine.IContent contentItem)
            {
                // first let's call the base implementation of the SetItemMetadata, so
                // that it fills the values of all fields it can
                base.SetItemMetadata(itemContainer, contentItem);



    Now i wrote an ProviderClass in which i also need all Informations about one ContentItem. I forward already the GUID of the Content to my class and used the ContentManager to access the Information. We use an SQL-Server and i checked with SQL-Server Profiler which Table is used to get the Data(it's sf_GCMetaData) but the Item contains less Info then the Item of SetItemMetaData. So now my question is:
    How can i achive the same Item in a programmatic way? It has to go over more then one DatabaseTable
    Is there a way to get the same ContentItem

    Regards David
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    16 Feb 2010
    Link to this post
    Hello DPC David,

    Please take a look at our Developers Manual - Finding Content items. You can use GetContent method and its 21 overloads.

    Kind 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. DPC David
    DPC David avatar
    26 posts
    Registered:
    28 Dec 2009
    16 Feb 2010
    Link to this post
    Hi

    im sorry maybe discribed the problem in a wrong way. Im getting  a ContentItem but not all Infos are filled like the Content-Property. As i already mentioned, i have adopte the Soulution and now im not sure where to look for the Bug.
    I had found the section "Finding Content" but all the overloads define the result of all contents so that you have only a specific result. In my case some Info is missing.

     ArticlesManager manage = new ArticlesManager("Articles");
     IContent cont = manage.Content.GetContent(itemID);

    cont is not properly filled. I would be very thankfull for a hint.

    Regards David
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    17 Feb 2010
    Link to this post
    Hello DPC David,

    The IContent object has a method SetMetaData that is used when you want to persist some data to sf_GCMetaData table. After setting the values of all metakeys you need to call manager.SaveContent(IContent) to persist the values. There is GetMetaData method used for retriving the data from this table.

    Best wishes,
    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.
  5. DPC David
    DPC David avatar
    26 posts
    Registered:
    28 Dec 2009
    18 Feb 2010
    Link to this post
    Hi

    what happens if i have an ContentItem in diffrent languages and i want to filter my content to only one selected lang.
    Do i have to use the overloads of "Finding Content" and then which filter? I mean the property language in ContentItem.

    Regards David
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    18 Feb 2010
    Link to this post
    Hello DPC David,

    Set CurrentUICulture to a given culture. Get the items using ContentManager. Then if there are any results you can filter them

    sample

    CultureInfo culture;
    culture = CultureInfo.GetCultureInfo("bg");
    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
    var cntm = new ContentManager("News");
    IList l = cntm.GetContent();


    Sincerely yours,
    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.
  7. DPC David
    DPC David avatar
    26 posts
    Registered:
    28 Dec 2009
    18 Feb 2010
    Link to this post
    Thanks a lot!!!!

    that was exactly was i needed, this Forum and Service is great

    Regards David
  8. Scott McNeany
    Scott McNeany avatar
    44 posts
    Registered:
    09 Mar 2010
    12 May 2010
    Link to this post
    This is not working for me. Sometimes it will display a different culture, but it's intermittent. I'm not sure why the

    GetContent() method I'm using seems to be ignoring the CurrentUICulture most of the time and picking up on it some of the time. Below is the code. Is there any content caching that can be enabled/disabled that the API might be using?

    //Test class

     

    public

     

    partial class ContentServiceWebTest : System.Web.UI.Page

     

     

     

     

     

    {

     

    protected void Page_Load(object sender, EventArgs e)

     

    {

     

    DateTime dt1 = DateTime.Now;

     

     

    string ContentExternalSlug = "TestContentItem";

     

    PopulateEnglishContent(ContentExternalSlug);

    PopulateSpanishContent(ContentExternalSlug);

    PopulateFrenchContent(ContentExternalSlug);

    PopulateGermanContent(ContentExternalSlug);

     

    DateTime dt2 = DateTime.Now;

     

     

    TimeSpan t = dt2.Subtract(dt1);

     

     

    this.lblTime.Text = t.Milliseconds.ToString();

     

    }

     

    private void PopulateEnglishContent(string externalSlug)

     

    {

    ChangeCulture(

    "en");

     

     

    this.lblEnglishContent.Text = ContentService.Instance.GetGenericContent(externalSlug);

     

    }

     

    private void PopulateSpanishContent(string externalSlug)

     

    {

    ChangeCulture(

    "es");

     

     

    this.lblSpanishContent.Text = ContentService.Instance.GetGenericContent(externalSlug);

     

    }

     

    private void PopulateFrenchContent(string externalSlug)

     

    {

    ChangeCulture(

    "fr");

     

     

    this.lblFrenchContent.Text = ContentService.Instance.GetGenericContent(externalSlug);

     

    }

     

    private void PopulateGermanContent(string externalSlug)

     

    {

    ChangeCulture(

    "de");

     

     

    this.lblGermanContent.Text = ContentService.Instance.GetGenericContent(externalSlug);

     

    }

     

     

    private void ChangeCulture(string cultureAbbreviation)

     

    {

     

    CultureInfo culture;

     

    culture =

    CultureInfo.GetCultureInfo(cultureAbbreviation);

     

    System.Threading.

    Thread.CurrentThread.CurrentUICulture = culture;

     

    }

    }


    //ContentService.cs

     

    public

     

    string GetGenericContent(string slug)

     

    {

     

    var content = string.Empty;

     

     

     

     

     

     

    var contentManager = new ContentManager();

     

     

    var filterForExternalSlug = new MetaSearchInfo(MetaValueTypes.ShortText, "ExternalSlug", slug, SearchCondition.Equal, JoinType.And);

     

     

    IList contents = contentManager.GetContent(0, Int32.MaxValue, "ExternalSlug", ContentStatus.Published, filterForExternalSlug);

     

     

    if ((contents != null) && (contents.Count > 0))

     

    {

     

    var contentItem = (IContent)contents[0];

     

    content = contentItem.Content.ToString();

     

     

     

    return

     

    content;

     

    }

  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    12 May 2010
    Link to this post
    Hello Scott McNeany,

    Try setting the current culture inside ContentService.cs before you call

    var contentManager = new ContentManager();

    All the best,
    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.
  10. Scott McNeany
    Scott McNeany avatar
    44 posts
    Registered:
    09 Mar 2010
    12 May 2010
    Link to this post
    Ivan,

    Thanks for your help. This still doesn't seem to work correctly when trying to change the CurrentUICulture multiple times on the same HTTP Request. For example, see the below screen captures of my page output. I am just printing the localized text to the screen for each culture and refreshing the screen produces different output each time I refresh.

    Example 1 Output:

    English: 
    We are testing the content localization feature. We should be able to pass a culture to get the correct content. 

    Spanish: 
    We are testing the content localization feature. We should be able to pass a culture to get the correct content. 

    French: 
    We are testing the content localization feature. We should be able to pass a culture to get the correct content. 

    German: 
    Wir prüfen die zufriedene Lokalisationeigenschaft. Wir sollten in der Lage sein, eine Kultur zu führen, um den korrekten Inhalt zu erhalten.

    Example 2 Output:

    English: 
    We are testing the content localization feature. We should be able to pass a culture to get the correct content. 

    Spanish: 
    We are testing the content localization feature. We should be able to pass a culture to get the correct content. 

    French: 
    Nous l'are testing the localization feature content. Nous should be able to pass a culture to get the correctement content.

    German: 
    Nous l'are testing the localization feature content. Nous should be able to pass a culture to get the correctement content.


    Example 3 Output:

    English: 
    We are testing the content localization feature. We should be able to pass a culture to get the correct content. 

    Spanish: 
    Estamos probando la característica contenta de la localización. Debemos poder pasar una cultura para conseguir el contenido correcto.

    French: 
    Estamos probando la característica contenta de la localización. Debemos poder pasar una cultura para conseguir el contenido correcto.

    German: 
    Estamos probando la característica contenta de la localización. Debemos poder pasar una cultura para conseguir el contenido correcto.
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    13 May 2010
    Link to this post
    Hi Scott McNeany,

    I am not able to reproduce this issue. You could take a look at the attached video

    sample code

    <telerik:RadComboBox runat="server" AutoPostBack="true" ID="Combo1">
        <Items>
            <telerik:RadComboBoxItem Text="BG" Value="bg" />
            <telerik:RadComboBoxItem  Text="EN" Value="en" />
        </Items>
    </telerik:RadComboBox>
      <asp:Button Runat="server" ID="Button2" Text="Submit" />

    void Button2_Click(object sender, EventArgs e)
        {
         
            CultureInfo culture;
            culture = CultureInfo.GetCultureInfo(this.Combo1.SelectedValue.ToString());
            System.Threading.Thread.CurrentThread.CurrentUICulture = culture;  
            var cntManager = new ContentManager("Generic_Content");
            IList l = cntManager.GetContent();
            if (l.Count > 0)
            {
                foreach (IContent c in l)
                {
                    
                        Response.Write(c.GetMetaData("Name").ToString() + "-------" + c.Content.ToString() + "<br />");
                }
            }
       }


    All the best,
    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.
  12. Scott McNeany
    Scott McNeany avatar
    44 posts
    Registered:
    09 Mar 2010
    13 May 2010
    Link to this post

    Thanks for the help. I stopped fiddling with this and I'm simply looping through the ContentVersions and checking against the LangID property to get the correct language version. This does not carry much additional overhead (especially when caching the content on subsequent requests).

    Here is my final code.

     

    public

     

    string GetGenericContent(string slug, CultureInfo culture, ContentCachingOptions cacheOption)

     

    {

     

    string content = string.Empty;

     

     

    string cacheString = culture.TwoLetterISOLanguageName + "." + slug;

     

     

    if ((cacheOption == ContentCachingOptions.GetLatest) || ((content = SearchCache(cacheString)) == null))

     

    {

     

    //Set the language Id to the id of the culture passed in.

     

    int

     

    langId = culture.LCID == 9 ? 127 : culture.LCID;

     

     

    var contentManager = new ContentManager();

     

     

    //Filter the query by ExternalSlug for performance.

     

     

     

     

     

    var filterForExternalSlug = new MetaSearchInfo(MetaValueTypes.ShortText, "ExternalSlug", slug, SearchCondition.Equal, JoinType.And);

     

     

    IList contents = contentManager.GetContent(0, Int32.MaxValue, "ExternalSlug", ContentStatus.Published, filterForExternalSlug);

     

     

    if ((contents != null) && (contents.Count > 0))

     

    {

     

    var contentItem = (CmsContentBase)contents[0];

     

     

     

    //The different versions of the item store the language versions.

     

     

     

     

     

    //There should always be 1 published version of each generic content item.

     

     

     

     

     

    foreach (var version in contentItem.ContentVersions)

     

    {

     

    if (version.LangID == langId)

     

    {

    content = version.Content.ToString();

     

    break;

     

    }

    }

    }

    }

     

    //If we're using the cache, then store the value of content here.

     

     

     

     

     

    if ((cacheOption == ContentCachingOptions.UseCache) && (content != null))

     

    {

     

    this.Cache.Add(

     

    cacheString,

    content,

     

    null,

     

     

    DateTime.Now.AddMinutes(60),

     

     

    Cache.NoSlidingExpiration,

     

     

    CacheItemPriority.Normal,

     

     

    null);

     

    }

     

    return content;

     

    }

Register for webinar
12 posts, 0 answered