Sitefinity CMS

Telerik.Cms.Engine Send comments on this topic.
ContentManager Class
See Also  Members   Example 
Telerik.Cms.Engine Namespace : ContentManager Class


This class is a main API class for working with content in Sitefinity.

Inherits ManagerBase abstract class. Represents an intermediary between web controls of Generic Content based Modules and data providers. Web controls should exclusively work with ContentManager for any data related work.

 

Namespace: Telerik.Cms.Engine
Assembly: Telerik.Cms.Engine (in Telerik.Cms.Engine.dll)

Syntax

Visual Basic (Declaration) 
Public Class ContentManager 
   Inherits ManagerBase(Of ContentProviderBase)
Visual Basic (Usage)Copy Code
Dim instance As ContentManager
C# 
public class ContentManager : ManagerBase<ContentProviderBase> 

Example

The following example demonstrates how to programatically access content in Sitefinity by taking advantage of ContentManager class and it's members.
This is a simple example that shows how to use ContentManager class to replace an occurance of certain word with another one, in all Content objects, such as Generic Content, News items, Blog posts, Events and so on.
C#Copy Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;

using Telerik.Cms.Engine;

namespace Samples.Sitefinity.Content
{
   
public class WordReplacer
   {
       
// This method will replace all occurances of oldValue string with the
       
// newValue string in all Generic Content objects in all Generic Content providers
       
       
public void ReplaceWords(string oldValue, string newValue)
       {
           
// we are going to loop through all the Generic Content providers
           
           
foreach (KeyValuePair<string, ContentProviderBase> provider in ContentManager.Providers)
           {
               
// for each provider we are going to create a new instance of ContentManager
               
               
ContentManager cntManager = new ContentManager(provider.Key);
               
               
// we are going to get all content from the current provider
               
               
IList allContent = cntManager.GetContent();

               
// we are going to loop through all the content in the current provider
               
               
foreach (IContent content in allContent)
               {
                   
// next we are going to get the IContent object that is associated with transaction
                   
// so that we can modify it
                   
                   
IContent contentInTransaction = cntManager.GetContent(content.ID);
                   
                   
// IContent.Content property is an object, not necessarily a string (e.g. it's binary
                   
// for Images and Documents module). We are going to try to cast it string.
                   
                   
string textualContent = contentInTransaction.Content as string;
                   
                   
// replacing will only make sense if content is string
                   
                   
if (textualContent != null)
                   {
                       
// we are going to replace all occurances of oldValue with a newValue
                       
                       
textualContent = textualContent.Replace(oldValue, newValue);
                       
                       
// we are going to change the existing Content property, with the new modified one
                       
                       
contentInTransaction.Content = textualContent;
                       
                       
// finally we are going to save the content
                       
                       
cntManager.SaveContent(contentInTransaction);
                   }
               }

           }
       }
   }
}

Remarks

ContentManager class is used in all modules that are based on Generic Content module, such as News, Events, Blogs, Images and Documents and so on. The respective manager classes of these modules all expose Content property, which is the instance of ContentManager created with a proper provider name. Therefore, you can access the methods of this class by accessing the Content property of respective manager class.

Requirements

Supported in Sitefinity 3.0, 3.1, 3.2

See Also