Use the Content Manager API in a Web Service

Use the Content Manager API in a Web Service

Posted on November 11, 2009 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

This post as my previous post is inspired by a client support request to extend Sitefinity functionality in a way. I also think that this is a good way to sample how the modules API can be used in web services. Following the logic of group operations functionality added in Sitefinity 3.7 we can now create a simple web service to allow us to duplicate content items in modules. For more information on group operations, please follow this link.

For this example I will be using Sitefinity 3.7 because it already provides some ready scripts that help us get the selected item IDs and pass them to the web service. The use of the bellow sample is also achievable in older versions of Sitefinity, however there you will need to add new button to the grid containing content items and then subscribe to its click event.

Now lets start by creating a regular web service in your Sitefinity website. I have created one in the ~/Sitefinity/Services folder and named the file CustomContentService.asmx. When you create a web service VS automatically generates the code file in your ~/App_Code directory. Bellow is the sample code that we will use to create copies of content items:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using Telerik.Cms.Engine; 
using Telerik.Security.Permissions; 
using System.Web.Script.Services; 
namespace Telerik.Samples.WebServices 
    /// <summary> 
    /// Summary description for CustomContentService 
    /// </summary> 
    [WebService(Namespace = "Telerik.Samples.WebServices")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
    // [System.Web.Script.Services.ScriptService] 
    public class CustomContentService : System.Web.Services.WebService 
        public CustomContentService() 
            //Uncomment the following line if using designed components  
        public void DuplicateContent(Guid[] contentIDs, string providerName) 
            ContentManager manager = new ContentManager(providerName); 
            //create copies of selected content items 
            foreach (Guid guid in contentIDs) 
                manager.GetPermission(guid, CrudRights.Create).Demand(); 
                //get content to be copied 
                IContent content = manager.GetContent(guid); 
                //create new content 
                IContent newContent = manager.CreateContent(content.MimeType); 
                //copy metafields 
                foreach (var metaKey in manager.Provider.MetaKeys.Keys) 
                //copy content 
                newContent.Content = content.Content; 
                //copy parent IDs 
                    newContent.ParentID = content.ParentID; 
                //copy comments 
                    foreach (IComment comment in content.Comments) 
                        IComment newComment = manager.CreateComment(newContent); 
                        newComment.Author = comment.Author; 
                        newComment.Email = comment.Email; 
                        newComment.IpAddress = comment.IpAddress; 
                        newComment.Owner = newComment.Owner; 
                        newComment.Visible = comment.Visible; 
                        newComment.Text = comment.Text; 
                        newComment.WebSite = comment.WebSite; 
                //copy item info 
                    newContent.ItemInfo = content.ItemInfo; 
                //copy thumbnails 
                    foreach (var thumbnail in content.Thumbnails) 
                        catch { } 
                //save newly copied content 


What we need to do now is to register the newly created web service to the script manager, so that we can use its methods. You can do this in the OnPreRender event handler of the ~/Sitefinity/Admin/Modules.aspx.cs:

protected override void OnPreRender(EventArgs e) 
    ScriptManager sm = ScriptManager.GetCurrent(this.Page); 
    ServiceReference reference = new ServiceReference("~/Sitefinity/Services/CustomContentService.asmx"); 
    reference.InlineScript = true


Next we need to add some markup and JS to the control templates for displaying content items in the modules where we need this service. For this you will need to work with the external templates for your version of Sitefinity. Here I will sample how to do this with the Generic Content module. Open the ~/Sitefinity/Admin/ControlTemplates/Generic_Content/ContentItemsList.ascx. First add a link button to the div that holds group operations commands:

<div class="groupActionsPopup"
    <span class="numberOfSelectedItem"><strong id="numberOfSelectedItems">0</strong><asp:Literal ID="lblSelectedItems" runat="server" Text="<%$Resources:ItemsSelected %>"></asp:Literal></span
            <span id="disabledDeleteGroupGrid" class="disabled"><asp:Literal ID="Literal20" runat="server" Text="<%$Resources:Delete %>"></asp:Literal></span>  
            <asp:LinkButton Style="display: none;" ID="btnDeleteGroupGrid" runat="server" OnClientClick="DeleteWithConfirmation(); return false;" Text="<%$Resources:Delete %>"></asp:LinkButton> 
            <asp:Literal ID="lblSelected" runat="server" Text="<%$Resources:Selected %>" /> 
            <span id="disabledDuplicateGroupGrid" class="disabled"><asp:Literal ID="Literal6" runat="server" Text="Duplicate"></asp:Literal></span>  
            <asp:LinkButton Style="display: none;" ID="btnDuplicateGroupGrid" runat="server" OnClientClick="DuplicateSelected(); return false;" Text="Duplicate"></asp:LinkButton> 
            <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:Selected %>" /> 

Then add some JS to be make the button look and behave as the rest of the commands:

function EnableOrDisableGroupOperationButtons(enable) { 
    if (enable) { 
        $("#<%= btnDeleteGroupGrid.ClientID %>").show(); 
        $("#<%= btnDuplicateGroupGrid.ClientID %>").show(); 
    else { 
        $("#<%= btnDeleteGroupGrid.ClientID %>").hide(); 
        $("#<%= btnDuplicateGroupGrid.ClientID %>").hide(); 


Then add a java script function that will handle the on client click event of the duplicate button that we have just added. This function will make the call to our web service passing the selected item IDs:

function DuplicateSelected() { 
    var guidsToPass = []; 
    for (var i = 0; i < selectedContentIDs.length; i++) { 
        if (selectedContentIDs[i] != undefined) { 
            guidsToPass[guidsToPass.length] = selectedContentIDs[i]; 
    //we are using similar logic as in the group operations functionality 
    //call the webservice and rebind grid 
    Telerik.Samples.WebServices.CustomContentService.DuplicateContent(guidsToPass, dataProviderName, DeleteContent_Success, OnFailed); 

All you have to do now is to map the control template in your ControlsConfig file and restart the website:

<!--Provides user interface for displaying a list of content items in the Generic Content module.--> 
<viewSettings hostType="Telerik.Cms.Engine.WebControls.Admin.ContentItemsList`1[[Telerik.Cms.Engine.WebControls.Admin.ContentItemsView, Telerik.Cms.Engine]]" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Generic_Content/ContentItemsList.ascx" /> 


The Progress Team

View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments are disabled in preview mode.
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
More From Progress
New Digital Challenges, New Me: Meet Sitefinity DX 14.0
Read More
Creating Epic Digital Experiences for B2B Companies
Read More
Five Reasons Why Developers Love Sitefinity Marketplace
Read More