Accessing ContentItems Through Code Behind

Posted by Community Admin on 04-Aug-2018 15:57

Accessing ContentItems Through Code Behind

All Replies

Posted by Community Admin on 07-Jan-2013 00:00

I am completely new to Sitefinity CMS, so bare with me here...
I am using Sitefinity Version 5.3

I am trying to query a ContentBlock I created in the CMS.
Content > Content blocks > And I added a basic content block with a line of content and also added a tag.

I added the content block to a page.

My goal was to query the content block by the tag name.

I could not find a good example to query the content by tag name except here:
http://www.telerik.com/help/sitefinity/developer-manual/content-items-get.html
However, this appears to be version 3.x?   Because Telerik.Cms.Engine.ContentManager() doesn't seem to exist anymore.

Then I found this documentation to get the contentitem by its ID.  However, I couldn't find a way to actually view the ID.  Is this in a CMS field somewhere, if so Where?
http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/modules/content-blocks/querying-content-items
I also tried to get the current Page ID and use that, from this:
(Guid) Telerik.Sitefinity.Web.SiteMapBase.GetActualCurrentNode().Id
And then query all contentitems within the page, I think I closed that page but that seems like a backwards way to do it anyways.


I then tried getting the contentitem by Title as shown here:
http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/modules/content-blocks/finding-content-items
This code gave an error immediately saying LString couldn't be converted to String (cI.Title = title does not work)  Is there a certain property that I should use?

So obviously I am doing something wrong here, it can't possibly be this hard just to pull a content Item.  Can someone help me out?  Thanks.




Posted by Community Admin on 07-Jan-2013 00:00

So I found the issue I am having sort of.  I have been converting the code examples with the Telerik code converter: http://converter.telerik.com/

This works:

ContentItem contentItem = App.WorkWith().ContentItems().Where(cI => cI.Id == contentItemId).Get().FirstOrDefault();

After Conversion, this doesn't:
Dim contentItem As ContentItem = App.WorkWith().ContentItems().Where(Function(cI) cI.Id = contentItemId).[Get]().FirstOrDefault()

I would assume you probably would want your code examples to work with your converter.  Thanks.

Posted by Community Admin on 08-Jan-2013 00:00

Hello Coty,

 Thanks for using Sitefinity.

The converter is a product independent of Sitefinity and even the RadControls. It is used to give as close a conversion as possible between the two languages but not everything will convert 100% accurately as is the nature of all code converters. This is especially so given the Fluent API used in your example.

As far as getting the ID, you can see both the Content item ID and the PageID in the "Advanced" section when you edit it on the page in the Sitefinity back end. Usually it's formatted as "C00X" with x being the number of the block added. Another method you could use is to query the PageID of the page you want to find your content blocks on and then match them up.

You could get the page by title

public Guid FindPageByTitleNativeAPI(string title)
    PageManager pageManager = PageManager.GetManager();
    PageData page = pageManager.GetPageDataList().Where(pData => pData.Title == title).FirstOrDefault();
    return page.Id;

And then find content items based on that Guid for value DefaultPageId

Guid contentItemId = FindPageByTitleNativeAPI();
 
lblDisplay.Text = TestQuery();
ContentItem contentItem = App.WorkWith().ContentItems().Where(cI => cI.DefaultPageId == contentItemId).Get().FirstOrDefault();

I hope this helps.

All the best,
Patrick Dunn
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 08-Jan-2013 00:00

Thanks for the replay Patrick,  currently I have been using fiddler or google chrome to grab the GUID's from the webservices being used in Sitefinity.

 So to get the ID of the ContentBlock I go to Edit > advanced.

 I see two ID's, one is the "sharedcontentId" that is a GUID. (0cdb53cc-469b-6485-baf1-ff00000f0a6a) and another labeled just "ID" (TD48526A8022).  I don't want to pull information by title because a user could easily update the title and then break the site on accident.  With the Fluent API (which is quite nice I might add) I've been using the GUID's to pull information so I know that works.  Is the other "ID" used for anything that I would need to know?

 If I put a content block on the page and edit it, go to advanced, there is a PageID field that is all 0's (00000000-0000-0000-0000-000000000000).  But for this particular page, I know the GUID because I grabbed it from Fiddler.  I may not be looking in the right place for the PageId?

Small Request: What I would like to see is when I open the page, click Title and Properties the GUID for the page is at the top in a non-editable label.  It would just make things much easier for developers in my Opinion.  I think this addition should be made to all pieces of content as well, so you know exactly which piece of content you are editing by the GUID.

 Thanks again,

Coty

Posted by Community Admin on 10-Jan-2013 00:00

Hi Coty,

Thank you for getting back to us.

The other ID is used strictly for Shared Content Items. In this case they have a unique ID and a shared ID. I will pass your suggestion to the development team. The reason I believe the IDs are not visible in the backend is to prevent confusion and most people get the PageID by accessing it through the API for the current page or by the page title since they are easier to remember.

Thanks for your suggestions!

Kind regards,
Patrick Dunn
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed