In this post, when I say content Name, I mean Title, it is the same.
The content items in Sitefinity are differentiated by ID, not Name, so it is possible to create multiple content items with the same name. The name is considered as metadata for the content itself. Here is how you can retrieve content items by name:
First, create an instance of the ContentManager
class. It has a method GetContent()
which has 12 overloads depending on how you wish to search for content. One of these overloads takes a IMetaSearchInfo
object as a parameter, this is the one you need. Create an instance of the MetaSearchInfo
class, which implements the IMetaSearchInfo
interface. The constructor of this class takes three values as parameters - the type of metadata, the key of the metadata and the value to search for. In your case, the type of metadata should be ShortText
, this is how the Name of the content is stored. Then the key should be "Name", and the value should be a string, containing the name of the content you want to retrieve. This is the code itself:
|ContentManager manager = new ContentManager(); |
|MetaSearchInfo info = new MetaSearchInfo(MetaValueTypes.ShortText, "Name", "Content1"); |
| IList foundContent = manager.GetContent(info); |
|Guid contentID = (foundContent as IContent).ID; |
Note that the GetContent()
method returns a list of content items, not a single one, so you need to access the first in the list. Now, if you substitute "Content1" with the name of the content, which you pass in the query string, you would be able to get the ID of the content with that name. Just make sure that you are passing a valid name in the query string.
I hope this makes it clearer.
the Telerik team