More in this section

Forums / Developing with Sitefinity / How to use GetContet when you don't know the ID

How to use GetContet when you don't know the ID

6 posts, 0 answered
  1. Priti
    Priti avatar
    17 posts
    Registered:
    20 Nov 2008
    03 Dec 2008
    Link to this post
    Hi,

    I want to search for a specific content item (generic content) using sitefinity API

    I have refered following code snippet from Developers' manual

    // create new instance of ContentManager
    ContentManager contentManager = new ContentManager();

    IList listOfContentItems = contentManager.GetContent();

    if (listOfContentItems.Count > 0)
    {
    foreach (Telerik.Cms.Engine.IContent contentItem in listOfContentItems)
    {
    // Here I am currently using the contentItem.Url property to identify the content item
    }

    But I have two queries here
    1. I am using contentManager.GetContent() method which returns all the content item. So iterating through all the content items for a website will be a performance hit. But at these stage I don't know anything about the content except the Name. So how do I get the specific content item when I don't know the ID

    2. I am using contentItem.Url property to identify the desired content item. Is there any better way to do this?

    Any help will be appreciated.

    Thanks,
    Priti.

  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    04 Dec 2008
    Link to this post
    Hi Priti,

    If you know the name of the content item, you could use the following overload of GetContent method to achieve the required functionality:

    public IList GetContent(  
       params IMetaSearchInfo[] filter 

    Here is an example how to get the content of an item with a specific name:

    private string GetContentByName(string name) 
        { 
            string content = string.Empty; 
            ContentManager cntManager = new ContentManager(); 
     
            List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>(); 
            // add a new filter to the list 
            filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Name", name)); 
            IList list = cntManager.GetContent(filter.ToArray()); 
     
            if (list.Count > 0) 
            { 
                IContent cnt = list[0] as IContent; 
                content = cnt.Content.ToString(); 
            } 
     
            return content; 
        } 

    Do let us know if you need any further assistance.


    Regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Priti
    Priti avatar
    17 posts
    Registered:
    20 Nov 2008
    05 Dec 2008
    Link to this post
    Thanks Pepi for your reply.

    This was the thing that I was looking for. Thanks a lot.

    I have a one more question.
    GetContent method returns IContent.
    But which is the concreat class that implements IContent for Generic Content type content item?

    Thanks,
    Priti.
  4. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    05 Dec 2008
    Link to this post
    Hello Priti,

    Telerik.Cms.Engine.Data.CmsContentBase is the class that implements IContent interface.

    Regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. Priti
    Priti avatar
    17 posts
    Registered:
    20 Nov 2008
    17 Dec 2008
    Link to this post
    Hi Pepi,

    I could get the content using Name as well as Category name by using the code that you have posted. Now I have a further question on this topic.

    I used following code to get all the content items of specific category.

    filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Category", "AboutUs")); 

    Now I have a list of contents from a specific category and I want to display the name of each of the content. How can I get the name?

    I guess the GetMetaData method of IContent will give me the values for meta fields. But how to get the NAME?

    plz help.

    Regards,
    Priti.

  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    20 Dec 2008
    Link to this post
    Hi Priti,

    The example below will display the name of every Generic Content item in a specific category:

               //add you category here 
            string specifiedCategory = "mycategory"
     
            // new instance of contentManager 
            ContentManager contentManager = new ContentManager("Generic_Content"); 
     
            //create a filer  
            List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>(); 
            filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "Category", specifiedCategory, SearchCondition.Equal, JoinType.And)); 
     
     
            // get all items 
            IList listOfAllitems = contentManager.GetContent(0, 0, string.Empty, nullstring.Empty, null, filter.ToArray()); 
     
            foreach (IContent contentItem in listOfAllitems) 
                Response.Write(contentItem.GetMetaData("Name").ToString() + "<br />"); 
        } 

    I hope this helps.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
6 posts, 0 answered